The Linux “find” command is one of the most powerful and versatile tools in a system administrator’s arsenal. It can be used to search for files based on a variety of criteria, such as name, size, date, and permissions, and perform various actions on those files, such as delete, copy, or execute.
In this article, we’ll explore ten advanced usage examples of the “find” command that demonstrate its full capabilities.
Linux ‘find’ Command Uses Examples
Here are the 10 advanced usages examples of the Linux `find` command.
- Search for files based on size:
To search for files that are larger or smaller than a certain size, use the “-size” option. For example, to find all files that are larger than 100 MB, use the following command:
find /path/to/search -size +100M
You can also specify the size in kilobytes (K), megabytes (M), or gigabytes (G). To find all files that are smaller than 50 MB, use the following command:
find /path/to/search -size -50M
- Search for files based on age:
To search for files that were modified within a certain time period, use the “-mtime” option. For example, to find all files that were modified in the last 7 days, use the following command:
find /path/to/search -mtime -7
The number after “-mtime” represents the number of days. A positive value means files modified more than that number of days ago, and a negative value means files modified within that number of days.
- Search for files based on type:
To search for files of a certain type, such as regular files, directories, or symbolic links, use the “-type” option. For example, to find all symbolic links in a directory, use the following command:
find /path/to/search -type l
The “l” in the command above represents symbolic links. Other possible values for “-type” include “f” for regular files and “d” for directories.
- Search for files based on name:
To search for files based on their name, use the “-name” option. For example, to find all files with a “.txt” extension, use the following command:
find /path/to/search -name "*.txt"
- Execute a command on matching files:
The “find” command can be used to execute a command on each matching file. To do this, use the “-exec” option. For example, to delete all files with a “.bak” extension, use the following command:
find /path/to/search -name "*.bak" -exec rm {} \;
The “rm {}” in the command above represents the command to be executed, and the “;” at the end of the line signals the end of the command. The “{}” in the command will be replaced with each matching file in turn.
- Search for files with specific permissions:
To search for files with specific permissions, use the “-perm” option. For example, to find all files that are readable and writeable by the owner and readable by everyone else, use the following command:
find /path/to/search -perm -644
The number after “-perm” represents the permissions, with the first digit representing the owner’s permissions, the second digit representing the group’s permissions, and the third digit representing everyone else’s permissions. In this case, the permissions are 644, which means read and write for the owner (6), read for the group (4), and read for everyone else (4).
- Search for files with specific ownership:
To search for files owned by a specific user or group, use the “-user” and “-group” options. For example, to find all files owned by the user “john”, use the following command:
find /path/to/search -user john
And to find all files owned by the group “admin”, use the following command:
find /path/to/search -group admin
- Search for files with specific timestamps:
The “find” command can also be used to search for files based on timestamps other than modification time. For example, to search for files based on the access time, use the “-atime” option. To find all files that were accessed in the last 7 days, use the following command:
find /path/to/search -atime -7
Similarly, to search for files based on the creation time, use the “-ctime” option.
- Search for files and exclude specific directories:
To exclude certain directories from the search, use the “-not” and “-path” options. For example, to search for all “.txt” files, excluding those in the “/tmp” directory, use the following command:
find /path/to/search -name "*.txt" -not -path "/tmp/*"
- Use find with the “grep” command for efficient text search:
The “find” and “grep” commands can be used together to perform a text search within matching files. For example, to search for all “.txt” files that contain the word “example”, use the following command:
find /path/to/search -name "*.txt" | xargs grep "example"
The “xargs” command is used to pass the matching files to the “grep” command, which will search for the word “example” in each file.
These are just a few of the many advanced usage examples of the “find” command in Linux. By combining different options and using the command with other tools, you can perform powerful and complex file searches and manipulations. Whether you are a system administrator or a software developer, the “find” command is a tool that should be in your toolbox.