Home >System Tutorial >LINUX >How to find a file in Linux
For novices, using the command line in Linux can be very inconvenient. Without a graphical interface, it is difficult to browse between different folders and find the files you need. In this tutorial, I will show you how to find specific files in Linux.
The first step is to connect to your Linux via SSH. There are two ways to find files in Linux. One is to use the find command, and the other is to use the locate command.
find command
Use the Linux find command to search the directory tree using different search criteria such as name, type, owner, size, etc. The basic syntax is as follows:
# find path expression search-term
The following is an example of using the find command to find a specific file based on the file name:
# find -name test.file
The command will search the entire directory tree to find a file named test.file and provide its storage location. You can try it using an existing filename on your Linux.
The find command can sometimes take several minutes to find the entire directory tree, especially if there are many files and directories in the system. To significantly reduce time, you can specify the directories to search. For example, if you know that test.file exists in /var, there is no need to search other directories. In this case, you can use the following command:
# find /var -name test.file
find can also search files based on time, size, owner, permissions and other options. To learn more about these options, you can view the manual of the Linux find command.
# man find
locate command
To use the locate command in Linux, you first need to install it.
If you are using Ubuntu, run the following command to install locate:
# apt-get update# apt-get install mlocate
If you are using CentOS, run the following command to install locate:
# yum install mlocate
locate is a faster way than find because it looks for the file in the database. To update the search database, run the following command:
# updatedb
Syntax for using locate to find files:
# locate test.file
Just like the find command, locate also has many options to filter the output. To learn more you can check the manual of Linux Locate command.
# man locate
I hope this article can help you, thank you.
The above is the detailed content of How to find a file in Linux. For more information, please follow other related articles on the PHP Chinese website!