The Linux find command is a powerful tool that can be used to locate files and directories instantly. While there are many ways to use find, most people will only use it for one or two tasks. In this article, we’ll cover five common uses of the Linux find command so you can get familiar with its many helpful features.
Search for a specific file in the current directory
The first version of this command is what you would use if you wanted to find a specific file in the current directory. Like other commands, it uses a syntax that’s simple enough to remember:
find . -name ‘filename’ # Finds all files with “filename” in their name.
- Note: The asterisk (*) stands for any number of characters.*
Search for files containing a specific text
- name: This flag is used to search for file names.
- type: This flag is used to search for file types.
- size: This flag is used to search for files based on their size.
- exec: This option allows you to run a command on each file found by the find command. In this case, it would run cat on every file that matched your criteria, which would print its contents in standard out (stdout).
- ok: If you include this option with the -exec flag, find will not return an error if there are no matches for your query but will instead print “no matches found” instead of throwing an error. This can be useful when you have multiple steps in one command and want to avoid running through them all unnecessarily when there aren’t any matches made during one step but not another
Find the latest or oldest modified files or directories
- Use the ls -ltr command to find the latest or oldest modified files and directories.
- Or use the -newer or -ancient options. These are particularly useful for finding files based on their modification times, but they can also be used with other types of options as well.
- You can also use mtime with ls -ltr, which stands for “modification time” and is exactly what you’d expect: it will show you only those items that have been modified since your last run with this command.
- If you want to find all items created within the last two weeks (and thus not including any older than two weeks), use cmin with ls -ltr: cmin stands for “create minutiae” because it will show only those items that were created within a specified time frame. Note that this option can only be used when combined with another one like lt;rm/.
- Similarly, cmax also limits results by age but does so in reverse—it shows only those items created before a certain date/time combination rather than after it (e.g., using cmax).
Find a file by its permissions number or string
In this example, we’re searching for a file with the permissions string of “rwxr-xr-x”.
> find / -perm -4000 -fprint
The first column is the permission number (permission=3), the second column is a flag from 0 to 3 and indicates if a file can be executed or not (EXECUTE=1). The third column gives us information about whether you can delete your files or not (DELETE=1). The fourth column tells us if our directory has any subdirectories in it or not (DIRECTORY=1) and finally, we get some very useful information about how many links are pointing towards our directory and what those links are pointing at.
Find files by size
Find files by size
The -size option uses the following format:
find . -size {{size}}{{unit}}
The “-” before the word “size” means that we want to find all files within our current directory. The {{size}} is a placeholder for the size of your file in bytes (1 byte = 8 bits). If you want to search for files that are larger than 20MB in size, then you would type:
find . -size 20971520{{units}}
The Linux find command can be used in many helpful ways.
The find command is a powerful tool. You can use it to search for files by size, permissions, owner and group, name, and much more.
This article will show you how to use the find command with some of these options:
- -size SIZE Find files smaller than SIZE (bytes)
- -size +SIZE Find files greater than or equal to SIZE (bytes)
- -size -SIZE Find files larger than or equal to SIZE (bytes)
- -type TYPE Search for certain types of files only. Use c for character devices like /dev/zero; b for block devices; f for plain file; d directory; l symbolic link; p named pipe; s socket; u setuid file; g setgid file
Conclusion
The Linux find command is a powerful tool for finding files and directories. It can be used in many helpful ways, including searching for files that have been modified recently or finding out which files are the largest in a directory. With these five examples, it’s easy to see how this command can be useful in everyday work!