Delete all but some directories

I think, like me, you would have faced a lot of situations, where you wanted to delete all the files or directories in a location, leaving only the required files/directories. So, I have a directory containing lots of files/directories and I want to delete most of them except some 5/10 of them, how to I do it.

I finally wrote a small script to do that. First save list of files that you do not want to delete in file called “listnames” and then execute the below script. This will give you the rm commands that you need to execute. If you want you can execute the rm command from the script, but to be able to review, I just have the commands echoed.

#!/bin/bash -
#          FILE:
#         USAGE: ./
#       OPTIONS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Amit Agarwal (aka)
#      REVISION:  ---

all_runs=$(echo *)
while read line
    all_runs=$(echo $all_runs |sed 's/'"$line"'//')
all_runs=$(echo $all_runs |sed 's/'"$0"'//')
all_runs=$(echo $all_runs |sed 's/'"listnames"'//')
echo rm -rf $all_runs


Enhanced by Zemanta

The power of find command in Linux – advanced.

Generally whoever uses Linux, would know about the find command. Find the man page here.

There are also lots of blogs, tutorials and other articles on find command on the web, so why write another one. Because it\’s worth every word spent on it 🙂
find is a very powerful command, let\’s see how (options for find command from man page and usage):

depth — Process each directory\’s contents before the directory itself.
maxdepth — Descend at most levels (a non-negative integer) levels of directories below the command line arguments.
xdev — Don\’t descend directories on other filesystems.
executable — Matches files which are executable and directories which are searchable (in a file name resolution sense).
This takes into account access control lists and other permissions artefacts which the -perm test ignores.
iname — Like -name, but the match is case insensitive.
nogroup — No group corresponds to file\’s numeric group ID.
nouser — No user corresponds to file\’s numeric user ID.
fls file — True; like -ls but write to file like -fprint.
ok command — Like -exec but ask the user first (on the standard input);
print0 — True; print the full file name on the standard output, followed by a null character
(instead of the newline character that -print uses).
printf format — True; print format on the standard output, interpreting \’\’ escapes and \’%\’ directives.

There are lot of other options but these are worth mention not used on regular basis. The printf option requires
special mention as the number of options that it supports is quite large and can reallyl help you in the formatting
the data that you get, as well as help you get the data that you want.

1) Get names of files bigger than 5M

find . +5M -printf \”Name : %20f Size : %s\\n\”

2) delete files (filenames contain spaces or quotes and so on)

find . -name \”name*\” -print0 |xargs -0 rm -f

Updated 28th May: Realinged for issues with post going into the sidebar 🙁

Linux find command – Find file and directories faster and easier


This command is very powerfull when used with combination of filters and pipes and RE.

I will give some example:

find . -type f -->> List all files
find . -type f -exec rm {} \\\; -->> Delete all files
find . -type d -exec rm {} \\; -->> Will through some common errors 🙂
find . -name \"*name*\"  --> find files containing name in the filename
find . -atime 12 --> Find files accessed 12 days ago

Similarly there is ctime for file status change

find . -type d -depth 2 --> find all the directories in the depth 2 of the tree.
find . -tyde d -ok rm -rf {} \\; --> Delete all the directories but only after user confirmation

There could be numerous such examples so I will leave it to you do digg the man page for find and then experiment :))

Enhanced by Zemanta