Tuesday, June 24, 2014

Ubuntu: Recursively delete multiple files with specific format

Ubuntu: Recursively delete multiple files with specific format

Sometimes we have to delete files of specific format (common extensions, ends with ~, etc) which were spanned over a group of directories. To do so, we follow certain methods.

Lets do it by an example.
In my case, I've o create the temporary files, i.e., the ones ending with ~.

Method 1: Delete them manually
Method 2: Goto each such directory and run the following command
rm *~

Method 3:
rm *~
rm */*~
rm */*/*~
rm */*/*/*~

First two methods need more time and to use third method, we must know the depth of recursion.

Following method will solve our problem.

Method Friendly: Run the following command from the parent directory from which you are deleting the files.
find . -name '*~' -type f -delete
This will delete all files ending with ~, from the current directory and also all its sub-directories.