This blog post will teach you how to use find command in Linux to search for files and directories based on name, size, date, and more
find -name snow.png
- Search for files named snow.png in the current directory.
find -iname snow.png
- Case-insensitive search for files named snow.png, snow.png, SNOW.PNG, etc. in the current directory.
find / -name '*.txt'
- search for files anywhere on the system that end in
.txt
- Wild cards should always be quoted to avoid unexpected results
- search for files anywhere on the system that end in
find /etc -name '*pass*'
- Search for files in
/etc/
that contain pass in their name
- Search for files in
find /home -user joe -group joe
- Search for files owned by the user joe and the group joe in /home/
finding files based on their name will, unlike locate, look for files that are named the exact string passed to the find command. That is to say, if a find command was used like:
[user@userX ~]$ find / -name .png
find would only return the files that were named .png, not files that contained in their name the string .png. Fortunately, you can use shell wild cards with find, but they should be quoted. As an example:
[user@userX ~]$ find / -name '*.png'
would find all files on the system that have .png as the end of their name. Related to -name is the -iname option which works the same way as -name but performs a case-insensitive search.
The -regex option in find does not work quite the way one would expect. -regex applies the regular expression to the name of the file, including the absolute path to the file. So in the above example, the regular expression '.W.*\.png'
will math all files that have a capital W and .png in their names. If we had instead used 'W.*\.png'
, we would get no results because the full path to our file name will always begin with a / and our provided regular expression indicates we are only interested in files whose full path name begins with a W, something that can never be true.