Search This Blog

2012-10-29

Deleting Large Amount of Files in *nix

You may find at some point that a process or user has somehow managed to put thousands of files in a directory on your *nix system.

Trying to remove this with rm will not work as there are limitations on the amount of arguments you can specify with these commands.

For example trying to remove thousands of files will give you the following:
rm -rf *
-bash: /bin/rm: Argument list too long

Using xargs to Delete Files

Sometimes this can be bypassed using something like:
find . -name '(file_name_search_pattern)' | xargs rm
OR
ls | grep '(file_name_search_pattern)' | xargs rm

If these ways do not work, move onto the find methods.

Using find to Delete Files

There are two methods that are generally used for removing a large list of files in unix

On newer systems
find . -name '(file_name_search_pattern)' -delete 

Older Systems
Which do not support the -delete flag
find . -name '(file_name_search_pattern)' -exec rm {} \;

Notes:
If either of the above mentioned methods do not work or begin to crash, you can run either of them in a loop with a limitation on the amount of files you pass to them.

No comments:

Post a Comment