Last Updated: February 25, 2016
·
8.763K
· sajnikanth

shell: Find and copy files

Here are some examples to get started with the find command. Do check out the man page for other options.

To find all files in a location:

find . -type f

To find all directories in a location:

find . -type d

To find the number of files in a location:

find . -type f | wc -l

To find the number of "jpg" files in a location:

find . -type f | grep "jpg" | wc -l

To find files of size between 1 MB and 2 MB in a location:

find . -type f -size +1M -size -2M

Now that we are able to look for things we want, let's say you want to copy all files of size between 1 and 2 MB to another location:

find . -type f -size +1M -size -2M -exec cp {} ~/my_files/ \;

Here {} contains the results of the find. The last ; is also required and has to be escaped.