The find command is an incredibly powerful tool, and putting effort into learning it can be rewarding. It will help you in performing various system administration tasks efficiently, like disk space management, recursive file operations, and backups. Linux find command searches for files and folders based on the criteria you define and allows you to perform an action on the search results. The syntax of find is like this: where

directory-to-search is the starting point from where find starts looking for files. The search includes all subdirectories under this directory. criteria (test) tells which files to search for action tells what to do with each file found matching the criteria.

Tests

Search by name

Here is a simple example. The following command searches for the file a.txt in the current directory: Where

. refers to the current directory -name test specifies the criteria to be matched

This search with -name test is case-sensitive and would ignore file A.txt. To ensure that your search is case-insensitive use -iname test: To search for all .jpg image files in the current directory, use the wildcard pattern *.jpg: You can use the directory name in which to search. For example, to search for all .jpg images in /home directory: If you see too many permissions denied errors, you can add 2>/dev/null at the end of the command. This redirects error messages to /dev/null device, and gives a cleaner output:

Search by file type

Using the -type test you can search files by type. File types can be: For example, using the test -type d will list only directories:

Search by file size

You may need to search for large files and delete them. In the following example, the test -size is followed by the string +1G. This would search for all files larger than 1 GB. The + sign means search files larger than the following number. A minus symbol (-) can be used to indicate smaller than. Using no sign would mean match size exactly. The number is followed by the file size unit. Units can be:

Search empty directories and files

Use -empty test to find empty directories and files like this:

Search by file modification time

You can search for all files and directories based on create or modification time with -cmin test. To search for all files modified in the last 60 minutes (less than 60) use -60 like this: For files modified anytime prior to the last 60 minutes, use +60.

Search by access time

You can search for files by last access time, with -atime test. For example, the following command searches for files not accessed in the last 180 days: These could be moved to a backup device if disk space is running short.

Search by user name

With -user username test you can search all files and directories belonging to a user. For example, the following command searches for all files and directories owned by user ubuntu in /home directory:

Search by mode

Wish to search files set to a specific mode, that is, have a specific set of permissions? Use -perm test. The following example searches for files with permissions set to 777:

Operators

You can use the following three logical operators to combine multiple tests in a single find command:

-and -or -not

For example, the following command searches for files larger than 100MB owned by user me: The following command looks for files larger than 100MB owned by user me or by user vagrant: You need to place a backslash character in front of the parentheses so as to prevent the shell from trying to interpret them.

Actions

find gives you the search results and then the choice to perform an action on them. Here are some predefined actions: So, if you wish to search for all empty files and delete them, this is how you can do it: Caution: Before you use the delete action it is always safe to run the command once with -print action and confirm the results. The -exec action is special. It allows you to execute a command of your choice on the search results.  It’s like this: Here

command is the command you want to execute on the search results, like rm, mv or cp. {} represents the search results. The command ends with a semicolon escaped with a backslash.

So, the command to search and delete all empty files can be written like this: Here is another example using -exec action. The following command copies all .png image files to backup/images directory:

Conclusion

You can use the Linux find command to search for files based on name, last access date, last modification date, user(owner) name, group name, size, permissions, and various other criteria. With these search results, you can perform actions on them like deleting, copying, or moving them to a different location. Once you master the find command, it can be of great help and can simplify system administration tasks for you. And the key to mastering it is practicing it and using it!