If we talk about Linux users, users can’t depend on the graphical user interface to perform the tasks. Therefore users must have a strong grip on the terminal commands. Searching for files and directories is a difficult task, especially when you have a bunch of files and directories in the operating system. In Linux, the find command is a utility and quite a useful tool for looking for specific files or directories.
This article will demonstrate the finding of files and directories using the find command in Linux. The content for this post is.
- What is find Command in Linux?
- How to Search Files Using the find Command in Linux?
- How to Search Directories Using find Command in Linux?
- Bonus Tip: How do you find and Remove Files or Directories?
Let’s start this guide.
What is the find Command in Linux?
In Linux, files or directories are searched using the “find” command. It provides various features, such as searching files using various parameters, including modification time, creation date, permissions, extensions name, etc. Let’s see how it works in Linux:
$ find [Path] -type [TypeOptions] [-Options] [Expression]
The components of the syntax of the find command are provided below:
- The “find” keyword represents the command.
- Put the “Path” to search the files.
- Write “-type” and type options to describe searching for files or directories.
- Type expressions that you want to find.
There is another syntax of the find command which is described below:
$ find [Path] [-Options] [Expression]
The keywords of the syntax are:
- Type the “find” keyword to search files and directories.
- Add the path where you want to search the specific files or directory.
- Type expressions that you want to find.
On Linux, both syntaxes are applicable and used to locate the directories and files in Linux.
Below are the Options that can be used with the “find” command:
|Options for [-type] argument||Function|
|f||To search for regular files.|
|d||To search the directories.|
|b||search for blocked devices.|
|s||To search for Sockets.|
|c||Search for character devices.|
|l||Search for symbolic links.|
Let’s practice using the examples.
How to Search Files Using find Command in Linux?
To search any files in Linux, open the terminal by pressing “Ctrl+Alt+T” from the keyboard. A few examples are discussed below.
Example 1: Search a File by Name
The command provided below will search for the file named “hardwareinformation.txt” at the path “/home/itslinuxfoss”:
$ find /home/itslinuxfoss -type f -name hardwareinformation.txt
The “find” command will search the file in the given path by its name. Once it finds the file, it will display it as shown in the above image.
Note: The above-stated command can be executed using the below command:
$ find /home/itslinuxfoss -name hardwareinformation.txt
Example 2: Search File by Extensions
The “find” utility can be used to find the files by extensions. For instance, the command provided below will find all the files ending with “.txt.g”:
$ find /home/itslinuxfoss -type f -name '*.txt.gz'
The above command will search for files with extension name and will display all the files matching that extension name.
Note: Alternatively, the below command can also give the same results:
$ find /home/itslinuxfoss -name '*.txt.gz'
Example 3: Search File by Modification Time
The “find” command can be used to look for any file with modification time, for this “mtime” option can be helpful. “mtime” displays all the files that were modified in the previous days. User can type the argument -1, -2, -3… as several days; let’s check it in the example:
$ find /home/itslinuxfoss -type f -name hard.txt -mtime -2
In the above command, “hard.txt” file is displayed because this file was modified in the past two days.
Note: Alternatively, the following command can also be used to get the same output:
$ find /home/itslinuxfoss -name hard.txt -mtime -2
Example 4: Search File(s) by Permissions
The find is used to search the files with specific permissions. Users can use the “perm” option with the find command to search files by its permission. Let’s execute the command and the file by permission:
$ find ./itslinuxfoss -type f -perm 664
In our case, the “index.html” file has 664 permissions.
Note: Alternatively, you can use the below command to get the same results:
$ find ./itslinuxfoss -perm 664
Example 5: Search Empty Files
The find command can also be used to look for the empty files in the system. You can use the “empty” option with the find command to search for empty files.
$ find ./itslinuxfoss -type f -empty
The above command will search only empty files from the given path. Once found, the empty files will be displayed as shown in the above image.
Note: The below command will display the empty files and directories in the location.
4 find ./itslinuxfoss -empty
It is observed that two empty files and one empty directory are printed with their paths.
How to Search Directories Using find Command in Linux?
The functionality of the “find” command is not limited to files only. However, it offers methods to find directories in Linux. Let’s see how the “find” command works with directories:
Example 1: Search Directories by Name
The “find” command is also helpful in searching the directories. To search the directory by its name, use the type “d” option:
$ find ./itslinuxfoss -type d -name LinuxFoss
The above command will search for the directory with the name mentioned in the command. Once it is found, the directory will be displayed in the terminal.
Example 2: Search Empty Directories
The “find” can also be used to search for empty directories. Use the “empty” option in the find command to display all the empty directories:
$ find ./itslinuxfoss -type d -empty
The above command displays all empty directories in the given location.
Example 3: Search Directories With Modification Time
Similarly, users can search directories with modification time.
To do so, the “mtime” option is used with the find command, “mtime” option represents the number of days in which the directory was modified:
$ find ./itslinuxfoss -type d -name 'directory 1' -mtime -1
The above command has displayed the directory modified in the last day.
Bonus Tip: How do you find and Remove Files?
To search and remove any file in Linux, run this command in the terminal:
$ find ./TestDirectory -type f -name 'networkinfo.txt' -delete
The delete option will delete the file permanently, and the user won’t be able to recover it. So, be careful while using the delete option.
That’s all from this post!
In Linux, files, and directories are searched through the “find” command. It provides different patterns to search the files or directories by their name, extensions, etc. In this article, different methods to find files and directories have been demonstrated with examples. Apart from that, the deletion method while searching any files or directories has also been illustrated in this post.