Summary of linux files and file content search commands

In the linux environment, we often want to search for a file or its contents, but there are many search commands, what are the differences between these commands, and how should we choose and use them?

The following summarizes some common file search and content search commands, save them for future use.

file search

whereis

Display the command path and the path of the help manual file related to the command. You can only query the command, source file and man file according to the parameters.

Common parameters

  • -b : find executable files
  • -m : Find the help file under the man path
  • -s : find source code files
whereis pwd #查找pwd命令的相关文件
whereis -m pwd #查找pwd命令的帮助文件
复制代码

locate

Find files from the built-in index database /var/lib/mlocate/mlocate.db of Linux. The query speed is super fast, but it may take some time for newly created files to be added to the index database. You can execute the updatedb command to force an index update. This command usually returns a large number of matches, which can be matched precisely by regular expressions using the -r option.

Centos does not have this command by default, it needs to be installed yum install mlocate, and then execute updatedb to initialize the file library.

Format:locate [选项] 文件名

Common parameters

  • -i: ignore case
  • -l: Only output a few lines, for example, output 5 lines is -l 5;
  • -r : the display mode that can be followed by regular expressions
  • -c: Do not output the file name, only count the number of files found;

Common Use Cases

locate pwd # 查找pwd命令相关的所有文件
locate /etc/sh # 查找etc目录下以sh开头的文件
locate -c /etc/sh #查找etc目录下以sh开头的文件数
locate -l 4 passwd # 是输出前4行

复制代码

find

The function of find is very powerful. It searches from the file system. By default, it searches from the root directory. The speed is very slow. Generally, it searches for qualified files in the specified directory.

Format:find + 查找的路径 + 参数 + 限定条件

Common parameters:

  • -name: search by name
  • -type: install file type lookup ("f" identifies file, "d" indicates folder)
  • -size: Find by size
  • -perm: search based on permissions
  • -mtime: search based on modification time
  • -atime: search based on access time

Common Use Cases

find ./ -name test.sh # 查看当前目录下名为test.sh的文件
find ./ -name '*.sh' # 查找当前目录下后缀为sh的文件。
find /etc -type f # 查找etc目录下的所有文件
find /etc -type d # 查找etc目录下的所有文件夹
find /home -size 10M #  查找home目录下等于10M的文件
find /home -size +10M # 查找home目录下大于10M的文件
find /home  # 查找home目录下小于10M的文件
find /home -size +5M -size -10M # 查找home目录下大于5M小于10M的文件
find /home -perm 777 # 查找home目录下权限为777的文件或目录
find / -mtime -3 #查找3天内修改过的文件
find / -atime -3 #查找3天内访问过的文件
复制代码

To sum up, these three commands are all for searching files, whereisand locateare based on the built-in database of the system, so the efficiency is very high, while find traverses the hard disk to find real files, so the resource consumption is relatively large and the speed is slow. whereisTo find general files, use and first locate. If neither of them can be found, or you need to use more conditions to search and then use find.

content lookup

grep

grep is a powerful text search tool in the linux system, which can filter matching lines or data from text files or pipeline data streams. String or regular expression matching can be used.

Format:grep + 参数 + 模式匹配 + 查找的文件

Common parameters

  • -v: Display unmatched text (check matching lines)
  • -i: case insensitive
  • -n: Display matching lines and line numbers
  • -c: display the number of matching lines
  • -o: only output matching content
  • -w: exactly match the filtered string

Common Use Cases

grep "cpu" cpuinfo # 输出文件中包含cpu的行
grep -v "cpu" cpuinfo # 输出文件中不包含cpu的行
grep -n "cpu" cpuinfo # 输出文件中包含cpu的行及行号
grep -c "cpu" cpuinfo # 输出文件中匹配cpu字符串的数量
grep -o "cpu" cpuinfo # 只输出匹配的内容cpu
grep -w "cpu" cpuinfo # 只匹配cpu这个单词的行
复制代码

vim

If you want to find the position of a certain string, you can also use the search function of the vi/vim command:

Methods as below:

1. Enter "/string" in command mode, such as "/cpu"

2. Enter "n" to find the next one, and enter "N" to find the previous one.

 

Guess you like

Origin blog.csdn.net/NHB456789/article/details/130365371