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.
Written by Sajnikanth Suriyanarayanan
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Find
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#