Everything is considered a file in the Linux system. Searching through these files is a simple task that every user has to do. Though there are multiple ways to do this task, searching with the Find command is the most efficient and convenient method to find your target file/directory.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux System queries.
In this context, we shall look into the Find command, its options, and uses that go beyond the basic syntax.
Find command takes the following format:
$ find [file/directory path] [options] [expression]
How to Browse Files/Directories by Name ?
To search for a specific file by name with the '-name' option, run the following command:
$ find /home -type f -name <filename>.txt
Similarly, You can find the directory by name using the following command:
$ find /home -type d -name <directory name>
Note: '-type' option in the above commands specifies whether we are searching for a file or directory.
Find command search is case-sensitive by default. To search without case sensitivity, use the '-iname' option.
$ find /home -type d -iname pictures
You can narrow down files using their extensions pattern with the following command:
$ find /home -type f -name "*.txt"
To search for a specific type of file, use the '-type' option in your Find command:
$ find /home -type d
You can limit the search of the files by providing a size limit with the '-size' option. The '+' and '-' sign is the format used for "more than" and "less than" operators:
$ find /home -type f -size +5M
You can search files by specifying modification, access, and creation time of the file with the '-mtime', '-atime', and '-ctime' options. Following command searches for files that have modified timestamps of the last two days:
$ find /home -type f -name "*.txt" -mtime -2
$ find /home -type f -name "*.txt" -atime +4
$ find /home -type f -name "*.txt" -ctime -2
To find files with specific permission, use the '-perm' option. Following command finds files with read-only restriction:
$ find /home -type f -perm /u=r
Similarly, the following command uses the -perm option to find executable files:
$ find /home -type f -perm /a==x
To search files/directories by their user and user group, run the find command with '-user' and '-group' options:
$ find /home -type f -user mintlinuxapt
$ find /home -type f -group mintlinuxapt
You can find all the empty files/directories in your system with the following commands:
$ find /home -type f -empty
$ find /home -type d -empty
You can search and delete files simultaneously using the '-delete' or '-exec rm' options in your Find command:
$ find /home -type f -name "*.pdf" -delete
$ find /home -type f -name "*.pdf" -exec rm -f {} \;
Moreover, you can combine the above commands with '-and', '-or' and '-not' options and get more functionality. For Example, the following command searches for a file with two conditions:
$ find /home -user mintlinuxapt -and -size +5M
This article covers how you can use the Find command to search and perform different operations efficiently. In fact, the Linux find command is a powerful tool that enables system administrators to locate and manage files and directories based on a wide range of search criteria. It can find directories and files by their name, their type, or extension, size, permissions, etc.