The find command in Linux/Unix is a crucial and frequently employed tool for searching files and directories. It offers an array of arguments and options to facilitate these searches.
While it's possible to locate files and directories in Linux through a graphical user interface (GUI), the GUI can become less efficient, particularly with larger sizes, leading to extended loading times. Consequently, seasoned System Administrators opt for the command-line search due to its reliability, speed, and robust capabilities.
In Linux, the 'find' command is a versatile tool that enables the search for files and directories based on various criteria such as names, creation dates, modification dates, owners, and permissions. Once you've become proficient with these commands, retrieving data from the Terminal becomes a straightforward task.
This article delves into 20 uses of the Linux Find command.
When utilizing the find command, it's essential to adhere to the following syntax.
find [path...] [options] [expression]
Path: This indicates the directory in which we intend to search.
Options: This is the section where we specify our search criteria, such as by name or size, to define what we want to find.
These options offer numerous methods to tailor and refine your search criteria when utilizing the find command in Linux.
Remember to refer to the find command's manual page (man find) for in-depth details on each option and any additional options available.
Option |
Description |
-name |
Search Files by Name |
-iname |
Case-Insensitive File Search by Name |
-type |
Search for Files by Type (e.g., 'f' for Regular Files) |
-size |
Find Files by Size |
-mtime |
Find Files by Modification Time |
-atime |
Find Files by Access Time |
-ctime |
Locate Files by Change Time |
-user |
Find Files by Owner |
-group |
Locate Files by Group |
-perm |
Locate Files by Permissions |
-empty |
Find Empty Files or Directories |
-not |
Reverse the following expression or conditions |
-and |
Combine multiple expressions or conditions using logical AND |
-or |
Merge multiple expressions or conditions using logical OR |
-exec |
Run a command on every file that meets the criteria. |
-execdir |
Run a command on the directory where the file is located. |
|
Display the file paths of the matching files (default behavior). |
-delete |
Remove the files that match the criteria. |
-maxdepth |
Specify the maximum depth for the search. |
-mindepth |
Specify the minimum depth for the search. |
-follow |
Follow symbolic links while searching. |
-regex |
Find files using a regular expression pattern. |
-printf |
Display specific file details using a format specifier. |
1. Listing All Files in the Current Directory and Subdirectories
The find command lets you list all the files in the current directory & its subdirectories.
$ find
2. How to search for a file with a specific name?
$ find . -name "test1.txt"
Note: In this example, we employed the '-name' argument to locate a file named "test1.txt," and the dot "." denotes the current working directory. To determine your current working directory, you can use the 'pwd' command.
3. How to perform a case-insensitive search for a file with a specific name?
$ find . -iname test1.txt
Note: In the example above, we utilized the 'iname' option to perform a case-insensitive search for a file named "test1.txt." It will list uppercase and lowercase instances of a file named "test1.txt" in the present working directory.
4. How to search for all directories using the 'type' argument?
$ find /home/suman/ -type d
Note: When using the '-type' argument to search for files, it provides the following options:
- 'd' for directories or folders
- 'f' for normal files
- 'l' for symbolic links
- 'c' for character devices
- 'b' for block devices
- Using the '-type' argument, you can categorize the search output by file, directory, symbolic link, or block devices. In the above example, I utilized '-type d' to display all directories under "/var/test," excluding files, symlinks, and other types.
5. How to Find Files with a Specific Format/Extension?
$ find . -type f -name "*.ppt"
Note: In this example, it will display all files with the ".ppt" extension from the current working directory. In practice, you can employ this command to find files with a specific extension.
6. How to search for a file by name and delete it?
$ find . -name linuxteck.txt -delete
Note: The '-delete' argument makes it easy to delete a file, but it's important to note that it doesn't ask for confirmation. This can be risky, primarily as it doesn't allow you to double-check before deleting files or folders. It's recommended for superusers, while regular users may prefer using the delete command with a confirmation option to double-check before deleting.
7. How to delete a file with confirmation using the execute (-exec) option?
Note: In this example, the command above will prompt for confirmation, asking whether you want to delete "linuxteck.txt" or not. If you press 'y', it will delete the file. Using '-exec' provides more control over the actual command, allowing you to pass arguments like rm or mv. This method is recommended when you need to delete files.
8. Search for files in multiple directories in Linux.
You can search for files in multiple directories by specifying their paths separated by spaces. For instance, to find files named file.txt in the /home/user1 and /home/user2 directories, you can use the following command:
$ find /home/suman /home/drashti -name temp.txt
This command will scan for files named file.txt in the /home/suman and /home/drashti directories.
9. Locating Files with Name in Linux
To locate all accessible files with path names containing "find," use the following command:
$ find . -name '*find*' -print
10. Search for Files by Size in Linux
To locate files based on their size in Linux, you can use the following command:
$ find . -size 414c -print
The above command will locate files that are precisely 414 bytes in size.
To locate files within a specific size range, you can use the following command:
$ find / -size +100M -size -200M
The will command displays the files that are in the range of 100-200MB in size.
11. How to find files accessed more than 5 days ago?
$ find / -atime 5
Note: Using the '-atime' (Access Time) option, you can identify the files that were last accessed, whether they were read or written. The provided command will list all files accessed 5 days ago from the current time.
12. How to find files modified more than 1 day ago?
$ find / -mtime 1
Note: With the '-mtime' option for Modification Time, you can identify the most recently modified files. The provided command will list files modified 1 day ago from the current time. You can use plus (+) and minus (-) signs before or after the number of days to specify a range.
Search Files Changed In Last N Minutes
Searching for Files Changed in the Last 5 Minutes
$ find /home/suman -cmin -5
13. Search Specific Directory and Path
To search in a specific directory or path, use the following command:
$ find ./Desktop
14. Inverting the Match
We can exclude files from the search by looking for files that do not match a given name or pattern.
$ find ./testing -not -name "*.png"
In the above example, we found all files that do not have a .png extension. The exclamation mark (!) can be used instead of "not."
$ find ./testing ! -name "*.png"
15. Combine Multiple Search
Combine multiple conditions using logical operators like -a (AND) and -o (OR) to fine-tune your search.
$ find -name '*.ppt' -o -name '*.txt'
The above command will display files with either .ppt or .txt extensions.
16. Locating Hidden Files
In Linux, hidden files typically start with a dot ("."). You can conveniently locate hidden files using the following command:
$ find ~ -type f -name ".*"
To search for all hidden files in a user's home directory, execute the following command:
$ find $HOME -type f -name ".*"
17. Find files with specific permissions
To search for files with specific permissions, you can use the -perm option with the find command. For example, to find all files in the /home folder with permissions set to '0777', use the following command:
$ sudo find /home -type f -perm 0777
To find all executable files, you can use the following command:
$ find /bin -maxdepth 2 -perm /a=x
18. Search Read-Only Files
To locate read-only files, use the following command:
$ find /opt -maxdepth 1 -perm /u=r
19. Find files belonging to a specific user
You can use the following command to find all single files that belong to the specific user:
$ find . -user username
You can also include the file names in the search results along with the user criteria.
$ find . -user suman -name '*.ppt'
20. Limiting the Find Command
To restrict the find command from descending into directories on other file systems, you can use the -xdev option. This option instructs find to stay on the same file system and not cross-mount points. This is useful when you want to search for files within a specific file system and avoid traversing into separate partitions or devices.
The below command will search for all files larger than 100MB in the root (/) file system, excluding other mounted file systems, and redirect error messages to /dev/null.
$ find / -xdev -size +100M 2>/dev/null
Conclusion:
The "find" command is an indispensable tool for searching and managing files in Linux. Its versatility and wide array of options make it essential for Linux users and system administrators. Whether you need to perform basic file searches or tackle more complex tasks, the examples showcase the power and efficiency of the "find" command. Feel free to explore these commands and unlock the full potential of your Linux command line capabilities.