find command in Linux is a highly versatile tool used for searching
through the file system hierarchy. It offers a variety of "tests" or "
expressions" that can be used to specify which files and directories should be
included in the search results based on their attributes. These tests cover a
wide range of file properties from type and size to timestamps and permissions.
There isn't a direct command in
find that lists all possible tests, but you
can get a comprehensive list of tests along with their descriptions by
find man page or the
find info page. To access the man page,
you can use the following command:
For a more detailed and structured documentation, you can use:
Both will provide detailed information about the tests and other features
Common Tests in the Find Command
Here’s a table of some common tests available in the
find command and what
each test does:
|Searches for files with a filename that matches the given pattern. Patterns must be quoted to avoid shell expansion.
-name, but the search is case-insensitive.
|Filters the search to a particular type of file (e.g.,
f for regular files,
d for directories).
|Finds files of a specific size. You can use
+ to find files larger than the specified size, or
- for smaller.
|Searches for files owned by the specified user.
|Searches for files belonging to the specified group.
|Finds files that were last modified a certain number of days ago.
|Finds files that were last accessed a certain number of days ago.
|Finds files that had their metadata (like permissions) changed a certain number of days ago.
|Finds files with certain permission settings.
|Executes a command on the files found.
-exec, but the specified command is run from the subdirectory containing the matched file.
-exec but prompts for confirmation before executing the command.
|Finds empty files and directories.
|Searches for files with a directory path that matches the given pattern.
|Finds files with the whole path matching the specified regular expression.
|Finds files that are newer than the specified file.
|Limits the search to a specific directory depth.
|Specifies the minimum depth of directories to start searching.
Examples Using Common Tests
Here are some examples of how these tests can be used with the
Find Files by Name
find /home -name "*.txt"
This will find all
.txt files in the
/home directory and its
Find Files by Type and Size
find /var/log -type f -size +1M
This command looks for regular files in
/var/log that are larger than 1MB.
Find Files Modified in the Last Week
find /etc -mtime -7
This finds files in
/etc that were modified within the last 7 days.
Find Files by Permission
find / -perm 0644
This command searches the entire file system for files with
Find and Delete Empty Files
find /tmp -empty -delete
This will find and delete all empty files within the
Find Files Owned by a Specific User
find /home -user username
username with the actual username to find all files owned by this
These tests make the
find command an extremely powerful tool for file system
management and scripting in Linux. By understanding and using these tests
effectively, you can manipulate and organize your files with a great deal of
precision and efficiency.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.