Not able to delete files when the number of files is very large

When we have to do operations on a huge number of files then find command comes to our rescue. There can be situations where you are not able to delete/remove files from a directory using rm command as file volume is huge. In this post, we are going to see the use of find command to remove, move and copy huge file volumes.

1) How to copy large number of files from current directory to other directory?

# find . -maxdepth 1  -type f -exec cp -p {} test_dir/ \;

Here, -maxdepth 1: Only files from current directory. -p: Preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all.

2) How to move large number of files from current directory to other directory?

# find . -maxdepth 1  -type f -exec mv {} test_dir/ \;

Note: In both above cases, it will not override files that already exist with the same names in the destination directory i.e test_dir. It will copy/move hidden files as well from the source directory.

3) How to remove large number of files from current directory?

# find . -type f | xargs rm