Updated April 17, 2023
Introduction of Linux Find File by Name
In the Linux operating system, we are able to search or find the file and directory in the directory hierarchy based and perform the user requirement actions on each matched of the search file. In the find file command, we are able to search the file as well as we can also get the file location. With the help of the find command, we are able to find the file or directory in different ways like the file or directory permissions, type of the files, update date, size of the file/directory. We can use the find command with different combinations like sed, grep, awk, etc.
Syntax:
Syntax of Find File Command
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]
- find: We can use the “find” keyword in the syntax or command. It will take different arguments like OPTION, path, expression, etc. As per the provided arguments, it will find the requested file from the current environment.
- [-H] [-L] [-P]: The options like -H, -L, and –P, it will control the treatment of the symbolic links.
- expression: We can provide the different flags as the expression that is compatible with the “find” command.
- path: We can provide the specific file path to the “find” command. It will help to search the file with a file location.
How Linux Find File Command Works?
In every operating system, there are a lot of ways to search the file on the system environment. Generally, the file searching functionality is by default present in the operating system. But if in case the desktop version is not available or only the CLI environment is present then it will be a big issue to find the respective or specific file. To overcome this condition, we can use the find command to search or find a specific file in the current operating system.
The find command majorly works on the option, path of the file or directory and expression. Once these inputs will provide to the find command, it will search the respective file or directory with the location path as well.
-D debugoptions in find command.
Sr No | Option | Description |
1 | help | It will print the debugging options in the find command |
2 | tree | It will print the expression tree in the original and optimized format |
3 | stat | It will display the print messages as files are examined by the system calls like the stat and lstat. |
4 | opt | It will display the diagnostic information of optimizing the expression tree. |
5 | rates | It will display the summary information indicating how often each predicate succeeded or failed. |
Few bugs in the linux find command.
- In the “find” command, there are few security problems inherent and it is not fixed yet. The same security problem specifies by “POSIX standard”. The action will inherently insecurely and the “-execdir” will use the same.
- The environment variable in the find command i.e. “LC_COLLATE”. It will impact on the “-ok” action
Examples to Implement Linux Find File Command
Following are the examples are given below:
1. By Name
The find file by name is the most common way to practice the find command in the Linux operating system. We need to use the “-name” option with the find command.
Note: While searching the file name, make sure the file name will correct. Because it is case sensitive.
Command:
find / -name "file.txt"
Explanation: As per the above command, we are finding the file with its name “file.txt”. We will get all the list of file with a similar name (on the “/” location).
Output:
2. By File Extension
In the find command, we are having the functionality to find the file by its extension.
Command:
find / -name "*.txt"
Explanation: We are able to print all the list of file with the “.txt” extension. It will not consider or print the file name with the upper case file name.
Output:
3. By File Name with “-i” Option
In the Find File Command, we can search the file by its file name. But the search file is case sensitive. To avoid this condition, we can use the “-i” option to avoid the case sensitive search.
Command:
find / -iname "file.txt"
Explanation: As per the above command, we are using the “-i” option with the find command. It will print all the files with the upper case as well as the lower case file name.
Output:
4. Capture the Search File Name in New File
In the Linux environment, we are having the functionality to find the file by its name and capture the entire file name in the receptive file. We can use the redirection operator to execute the find command.
Command:
find / -iname "file.txt" > /root/data/search_file.txt
cat search_file.txt
Explanation: We can redirect the find command output to the “/root/data/search_file.txt” file. We will get all the list of find files in the “/root/data/search_file.txt” file.
Output:
5. Search File Name with Size
In the Find File Command, we can find the file name with the size. We can use a different option like “name” and “size” in the find command.
Command:
find / -name "file.txt" -size +4M
Explanation: In the find command, we can find the file as per the size. As per the above find command, we are finding all the “file.txt” files having more than 4MB size.
Output:
6. Find File Name with File Type
In the Find File Command, we search the file name with its type. We can search the different type as a regular file, directory, symbolic link, character devices, block devices, etc.
Command:
find /dev/ -type b -name "sda*"
Explanation: We are using “b” option in the find command for block devices. We are finding all the block device information with its name only i.e. “sda”.
Output:
Conclusion
We have seen the uncut concept of “Linux Find File Command” with the proper example, explanation, and command with different outputs. The Find File Command by name is used to find the file with its location. In the CLI environment, the find command is very important for finding the respective files on the environment.
Recommended Articles
We hope that this EDUCBA information on “Linux Find File by Name” was beneficial to you. You can view EDUCBA’s recommended articles for more information.