The Linux find command is a powerful tool that allows system administrators to search for and manage files and directories that match a set of criteria in the filesystem.
You can not only use the find command to search for files on your system, but you can also use external commands to perform actions on those files, such as deleting them with the rm command, checking their permissions with the ls command, copying them with the cp command etc.
Basic syntax of the find command
The find command syntax if very simple to understand, here is the basic find command syntax:
$ find [path] [options] [action]
• [path]: It defines the directory where to begin searching.
• [options]: It defines the criteria of filtering e.g. searching a file/folder by its name, permission, time, or date.
• [action]: It defines what actions to perform with the file.
Find Practical examples
Now that you understand what the find command is and it’s functionality, let's explore some practical examples illustrating its usage.
1. Find files by name:
Finding files by name is most likely the most common and basic application of the find command. To find a specific file by name, use the -name option followed by the file name you want to find.
To find a file named log.txt in the home directory, for example, use the following command:
$ find /home/traw -name log.txt
The preceding search is case sensitive; to make your -name option case insensitive, prepend i to it so it becomes -iname. The example below will match log.txt, LOG.txt, Log.txt etc.
$ find /home/traw -iname log.txt
2. Find files on in the parent directory
Find searches for files in specified directories (parent directory) and sub-directories within the parent directory by default.
You can control this by passing the -maxdepth option to the find command, which instructs it to search the specified number of directories down.
In this example, the find command will only search one directory down and discard the sub-directories.
$ find /home/traw -maxdepth 1 -iname log.txt
3. Find files by extension
Searching for files by extension is similar to searching for files by name, with the exception that you will use the * wildcard meta character.
For example, to find all python scripts inside the
user's home directory, you would type:
$ find ~/ -name "*.txt"
It's important to note that if you don't want the shell to interpret the asterisk * symbol , you escape it with a backslash or place it in quotations.
4. Negating your search
At times , you may want to find files that do not match a certain criteria; to do so, use the find command's -not option. Here's an example of how to find all files that do not match the pattern "*.txt".
$ find ~/ -not -name "*.txt"
5. Find files by type
You may need to search for specific file types such as regular files, directories, or symlinks on occasion. Everything in Linux is a file.
To search for files based on their type, use the -type option and one of the file descriptors listed below:
• b - block (buffered) special
• c - block (buffered) special
• d - a directory `f`: a regular file
• l - a symbolic link
• p - a named pipe (FIFO)
• s - a socket
• D - door (Solaris)
Here's an example of searching for all directories within a specific directory while excluding files, symbolic links, character devices, and so on:
$ find /home/traw -name docs -type d
6. Find files by size
Use the -size parameter along with the size criteria to find files based on file size. To specify the file size, use the following suffixes:
• b - 512-byte blocks
• c - bytes
• k - Kilobytes
• G - Gigabyte
• M - Megabytes
• w - two-byte words
The following command will find all files in the home directory that are exactly 600k in size:
$ find /home/traw -size 600k
You can also use the find command to look for files that are larger or smaller than a certain size.
In the following example, we look for files that are less than 600k in the effective user's home directory.
Take note of the minus - symbol preceding the size value:
$ find /home/traw -size -600k
If you want to search for files larger than a certain size, you must use the plus + symbol. Here is an example searching for files larger than 600k:
$ find /home/traw -size +600k
The find command also allows you to look for files within a size range. The following command will search for all the files with size between 5K and 20K:
$ find /home/traw -size +5k -size -20k
7. Find files by their permissions
To find files based on their permissions, you use the -perm option.
For example, to find all files with permissions of exactly `755` inside the `/home/traw` directory, you would use:
$ find /home/traw -perm 755
The numeric mode can be prefixed with minus / or slash -.
When the prefix is slash /, at least one category (user, group, or others) must have at least the respective bits set in order for a file to match.
Consider the following an example:
$ find /home/traw -perm /111
The above command will match all the files with execute permissions set for either user, group, or others.
If minus - is used as the prefix, then for the file to match, at least the specified bits must be set.
The following command will search for files that have read, write and executable permission for the owner and group and are executable by other users:
$ find /home/traw -perm -771
8. Find files by owner or group
Use the -user and -group options to find files owned by a specific user or group.
For example, to find all files and directories owned by the user traw use the following command:
$ find /home/traw -user traw
9. Find files by modification date
The find command can also look for files based on when they were last modified, accessed, or changed.
When searching by size, use the plus and minus symbols to indicate "greater than" or "less than."
This will allow you to find files that were modified within a specified range of time.
Assume you modified one of your markdown files a few days ago but have forgotten which one. You can easily filter all files in your home directory that end in .md and were modified within the last five days:
$ find /home/traw -mtime 5 -name “*.md”
10. Find and delete files
You can use the -delete option to delete every file that match a certain criteria. It is important to note that the -delete option will not delete not-empty directories.
Here is an example, to delete all files ending with .py from the /home/traw/, you would use:
$ find /home/traw -name “*.py” -delete
Warning
Use this option only when you are certain that the result matches the files you want to delete. Before using the -delete option, it is always a good idea to print the matched files
11. Find empty files
The find command also allows you to locate empty files by using the -empty option. Here is a quick example demonstrating that:
$ find /home/traw -type f -empty
11. Perform actions on each matched result
The `find` command has a useful option for calling external programs to perform specific actions on the returned files that matched a certain criteria.
Here is an example of listing the permissions and other metadata of every file that the find command finds:
$ find ~/ -type f -name "*.sh" -exec ls -lah {} + $
# or
$ find ~/ -type f -name "*.sh" -exec ls -lah {} \;
A breakdown of the -exec option:
exec ls - this tells find to execute the ls command on every filename that matches the search string.
-lah - displays all files, including hidden files, their permissions, and other file metadata, such as sizes, in human readable format.
{} - The “{}” placeholder represents each filename and must be the last item in the parameter list.
; - To indicate the end of the parameter list, a semicolon ";" is used. It must be escaped with a backslash "\" otherwise the shell will interpret it. You can also use the + instead of the ; to indicate the end of the parameter list.
Summing up
That concludes the find command. If you take the time to experiment with the find command, you will discover that there are numerous possibilities. You'll save a lot of time if you get good at commands like this. Be sure to refer to the find command man pages whenever you get stuck.
That it for today's digest! Thank you for reading! If you enjoyed this guide and found it useful, please follow us like and share with others.