Home > System Tutorial > LINUX > body text

How to find a file in Linux

WBOY
Release: 2024-02-12 15:20:16
forward
578 people have browsed it

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.

如何在 Linux 中查找一个文件

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
Copy after login

The following is an example of using the find command to find a specific file based on the file name:

# find -name test.file
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

If you are using CentOS, run the following command to install locate:

# yum install mlocate
Copy after login

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
Copy after login

Syntax for using locate to find files:

# locate test.file
Copy after login

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
Copy after login

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!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!