How to Find a File in Linux

For newbies, using the command line in Linux can be quite inconvenient. Without a graphical interface, it is difficult to browse between different folders to find the required files. In this tutorial, I will show how to find specific files in Linux.

The first step to do is to connect to your Linux via SSH. There are two ways to find files in Linux.

One is to use the find  command

The find command uses the Linux find command to search a 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 by filename:

# find -name test.file

The command will search the entire directory tree for a file named test.file and will provide its location. You can try this with an existing filename on your Linux.

The find command can sometimes take several minutes to find an entire directory tree, especially if there are many files and directories in the system. To significantly reduce the time, you can specify directories to search. For example, if you know that test.file exists in /var, there is no need to search other directories. For this, you can use the following command:

# find /var -name test.file

Find can also search for files based on options such as time, size, owner, and permissions. To know more about these options, you can check the manual for **Linux find command**.

# man find

One is to use the 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 up the file in the database. To update the search database, run the following command:

# updatedb

Syntax for finding files using locate:

# locate test.file

Like the find command, locate also has many options to filter the output. To know more you can check the manual of Linux Locate command.

# man locate

 

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/130315522