What are the commands for viewing files in Linux?

小老鼠
Release: 2023-05-29 13:28:42
Original
26652 people have browsed it

There are five commands for viewing files in Linux, namely: 1. find command, which can find any desired file; 2. locate command, which cannot find the latest changed files; 3. whereis command, Only binary files, man files and source code files are searched; 4. which command, which checks whether a certain system command exists and which position the command is executed; 5. type command, which distinguishes whether a certain command is executed by the shell itself. It is provided by an independent binary file outside the shell.

What are the commands for viewing files in Linux?

The operating environment of this tutorial: linux5.18.14 system, Dell G3 computer.

When using a computer, you often need to find files. In Linux, there are many ways to do this. Here are five commands summarized for you.

Five commands for viewing files in Linux:

1. find

find is the most common and powerful search command. You can use It finds any file you're looking for.

The usage format of find is as follows:

$ find <指定目录> <指定条件> <指定动作>
Copy after login
- <指定目录>: 所要搜索的目录及其所有子目录。默认为当前目录。

- <指定条件>: 所要搜索的文件的特征。

- <指定动作>: 对搜索结果进行特定的处理。
Copy after login

If no parameters are added, find will search the current directory and its subdirectories by default, and will not filter any results (that is, return all files). They are all displayed on the screen.

Usage examples of find:

Search for all files in the current directory (including subdirectories, the same below) whose file names begin with my.

$ find . -name 'my*'
Copy after login

Search for all files in the current directory whose file names begin with my and display their detailed information.

$ find . -name 'my*' -ls
Copy after login

Search in the current directory for all ordinary files that have been updated in the past 10 minutes. If the -type f parameter is not added, ordinary files and special files directories are searched.

$ find . -type f -mmin -10
Copy after login

2. locate

The locate command is actually another way of writing "find -name", but it is much faster than the latter because it does not search a specific directory, but Search a database (/var/lib/locatedb), which contains all local file information. The Linux system automatically creates this database and updates it automatically once a day, so the latest changed files cannot be found using the locate command. To avoid this situation, you can use the updatedb command to manually update the database before using locate.

Usage examples of the locate command:

Search for all files starting with sh in the etc directory.

$ locate /etc/sh
Copy after login

Search for all files starting with m in the user's home directory.

$ locate ~/m
Copy after login

Search for all files starting with m in the user's home directory, and ignore case.

$ locate -i ~/m
Copy after login

3. whereis

thewhereis command can only be used to search for program names, and only searches binary files (parameter -b), man description files (parameter -m) and source code files (parameter -s). If parameters are omitted, all information is returned. Examples of using the

whereis command:

$ whereis grep
Copy after login

4. which

which command is used to search for the location of a system command in the path specified by the PATH variable. And return the first search result. That is to say, by using the which command, you can see whether a certain system command exists and where the command is executed. Examples of using the

which command:

$ which grep
Copy after login

5. The type

type command is not actually a search command. It is used to distinguish whether a certain command is provided by the shell. , or provided by a separate binary file outside the shell. If a command is an external command, then using the -p parameter will display the path of the command, which is equivalent to the which command.

Usage examples of type command:

The system will prompt that cd is the shell’s built-in command (build-in).

$ type cd
Copy after login

The system will prompt that grep is an external command and display the path of the command.

$ type grep
Copy after login

After adding the -p parameter, it is equivalent to which command

The above is the detailed content of What are the commands for viewing files in Linux?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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