Linux – Find Command Examples and Tutorial

The find command is a powerful and versatile tool that can do much more than search for files. There are many ‘tests’ that can be added to make the search as specific as needed. Complex searches can then be combined with ‘actions’ to be performed on files that match. Here are a few examples:

***  ALWAYS perform searches first with NO COMMANDS on the end (like -delete or -exec). There is NO UNDOING a bad command so make sure you know what files will be affected.  ***

  • Find files by name (such as copies). As with many commands, the argument right after find tells it where to start looking. Using . says to start looking from the terminal’s current directory. The -name test uses unix-style wildcards and must match a complete file or directory name. Placing a * on each side of your query will find it in the middle of a name. The last part is a test for file type. The most common flags for -type are f for file, d for directory, and l for symbolic link.
  • Find empty folders and delete them. Here were are replacing the -name test with a simple check for -empty because we are looking for empty directories. We can then specify -type d to find only directories. The last part is a call to the action -delete. Use this carefully and remember, always search first and only use actions after you know what will be affected.
  • Find iPhone screenshots and move them to a screenshots folder. This example uses the -exec action flag, which can be used to perform almost any command on the found files. This flag really shows off the incredible usefulness of the find command. This time, were are not searching from current directory, but telling find to start from the “~/iPhonePics” folder. For the -name test, we are looking for any files ending in “.png”; so in this case we need only one wildcard to fill out the start of each filename: “*.png”. The -exec action has extra requirements for a complete command. Brackets {} are where each found file path will be inserted into the command. Also, the command must be finished off with \; to let find know that’s the end. In this case, the mv command will be performed on each file and told to move them from anywhere in “iPhonePics” to my “~/Screenshots” folder.

I hope these three varied examples have given you inspiration on how to add the Linux find command into your tools. I am using this on Ubuntu 14.04 and version 4.4.2 of GNU findutils. Whatever version or Linux system you have it should work about the same. Please ask in the comments below if you have any questions! Have a great day!

Leave a Reply