How to Use the Linux “find” Command to Locate Files

We have all lost files. You think a file is in a certain directory and yet when you go looking for it, you can’t find it. Most desktop-based Linux distributions have a way to find files from the desktop, but you can also find files from the command line using the find command.

“find” searches through a directory tree looking for files that match the criteria supplied. If you just type find from the command line, the program will list every file in the current directory and all of its sub-directories. This probably isn’t that useful when looking for specific files, so the simplest way to filter the results is to use the -name option:

find -name photo.png

find-name-photo

find will search the current directory and all its sub-directories for any files called “photo.png”. When it finds a match, it will print out the path. If there are multiple files called “photo.png” in other directories, then all the matches will be printed.

The above command uses some built-in defaults of the find command. The full command is actually:

find . -name photo.png -print

“.” means search from the current directory downwards, and -print tells find to display the path of the matching files. These can be omitted, however. If you want to search from a different starting point or you want to specify a different action, then you will need to include them.

For example, if you want to find where your Linux distro keeps the “gzip” command, then you would use:

sudo find / -name gzip -print

find-name-gzip

The “/” tells find to start searching from the root directory and it is best to use “sudo” as this gives find the privileges needed to read in every directory. As can be seen, there are several different files called “gzip” on the system but the first is in “/bin/”, which is where the gzip command is stored.

It is also possible to perform case insensitive matches on the filename by using -iname rather than -name, for example:

find -iname photo.png

find-iname-photo

Notice how “photo.png” and “Photo.png” matched the criteria given to find.

But find isn’t limited to only searching for files based on their name. You can also search based on file permission, size, type and when the file was last accessed. For example, to find all the directories with a file permission of 777 (something that is not generally recommended) use:

sudo find / -type d -perm 777

Where -type d tells find to only match directories (not files) and -perm specifies the permissions, in this case 777.

To search for files greater than a certain size, use the -size option like this:

find . -size +25M

That will find all the files over 25MB in the current directory and its sub-directories.

So far we have only printed the paths of the files found, however it is possible to perform actions on these files. For example, to delete all the .mp4 files on your system which are larger than 1GB you would use:

find . -name "*.mp4" -size +1000M -print -exec rm -f {} \;

The -name option tells find that we are only interested in files with the .mp4 extension. The -size options means find will only match files over 1GB and the -exec rm -f {} \; part tells find to run (execute) the rm command on any files find. Note that the -f part of the rm command will mean you won’t be prompted before the file is deleted.

Obviously you should use this command with extreme care. The best thing to do is to omit the -exec rm -f {} \; part and see which files will be deleted and then run the full command to actually delete them.

There are lots of tricks which can be performed with find; here is a short list of useful command combinations:

  • Find all directories with 777 permissions and change the permissions to 755:
    sudo find / -type d -perm 777 -print -exec chmod 755 {} \;
  • Find all the empty files in the /tmp directory and delete them:
    find /tmp -type f -empty -print -exec rm -f {} \;
  • Find all the files which have been accessed in the last 50 days:
    find / -atime 50
  • Find all the files which are larger than 50MB but smaller than 100MB:
    find / -size +50M -size -100M

The find command is swift and powerful, so always make sure you use it with care, especially when you are using the -exec option. If you have any problems with the examples shown above, please feel free to use the comments section below to ask a question, and we will see if we can help.