This is the 19th day of my participation in the August More Text Challenge

In Linux, we often need to search for a file or the contents of a file. However, there are many commands for searching. What are the differences between these commands and how to select and use them?

Here is a summary of some common file search, content search commands, save for use.

File search

whereis

Displays the command path and the help file path related to the command. You can query only the command, source file, and man file based on parameters.

Commonly used parameters

  • -b: Searches for executable files
  • -m: searches for help files in the man directory
  • -s: searches for source code files
Whereis -m PWD # Find the help file for the PWD commandCopy the code

locate

From Linux built-in index database/var/lib/mlocate/mlocate db lookup file, query speed so fast, but the new file may need some time to join the index database, you can perform updatedb command is updated on a mandatory index. This command usually returns a large number of matches, which can be accurately matched by regular expressions using the -r option.

Centos does not have this command by default. You need to install yum install mlocate and run updatedb to initialize the file library.

Format: locate [option] File name

Commonly used parameters

  • -i: case is ignored
  • -l: indicates that only a few lines are output. For example, -l 5 indicates that five lines are output.
  • -r: Displays the regular expression
  • -c: only the number of found files is counted.

Common Use Cases

Locate PWD # Find all files related to the PWD command locate /etc/sh # Find the files starting with sh in the etc directory locate -c /etc/sh # Find the number of files starting with sh in the etc directory locate -l 4 passwd # Is to print the first four linesCopy the code

find

The find function is very powerful. It searches from the file system. By default, it searches from the root directory.

Format: find + search path + Parameter + qualification

Common parameters:

  • -name: Searches by name
  • -type: searches for the installation file type (f indicates a file, d indicates a folder)
  • -size: Searches by size
  • -perm: searches by permission
  • -mtime: searches based on the modification time
  • -atime: searches by access time

Common Use Cases

Sh # find./ -name '*. Sh '# find./ -name '*. Sh' # find./ -name '*. Sh '# find. Find /etc/type f # find /etc/type d # find /home/size 10M # Find /home # find /etc/type f # find /etc/type d # find /home/size 10M # find /home Find /home # find /home/size + 5m-size-10m # Find /home/size + 5m-size-10m # Find /home/size + 5m-size-10m # Find /home/size + 5m-size-10m Find / -mtime-3 # find / -atime-3 # Find / -atime-3 # Find files accessed in 3 daysCopy the code

To sum up, these three commands all search for files. Whereis and Locate search for files based on the built-in database of the system, so it is highly efficient. Find traverses hard disks to find real files, so it consumes a lot of resources and is slow. To find a common file, use whereis and locate first. If neither of them can be found, or you need to search with more criteria before using Find.

Content to find

grep

Grep is a powerful text search tool in Linux that can filter matching lines or data from text files or pipeline data streams. Matches can be made using strings or regular expressions.

Format: grep + Parameter + pattern matching + Search file

Commonly used parameters

  • -v: displays mismatched text (check for matching lines)
  • -i: case insensitive
  • -n: Displays the matching line and its number
  • -c: displays the number of matched lines
  • -o: outputs only matched content
  • -w: exactly matches the filtered character string

Common Use Cases

Grep -v "CPU" cpuinfo # Line containing CPU in the output file grep -n "CPU" cpuinfo # Line containing CPU in the output file grep -c "CPU" cpuinfo # Print the number of matching CPU strings in the file. Grep -o "CPU" cpuinfo # Print only matching CPU stringsCopy the code

vim

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

The method is as follows:

1. In command mode, enter/string, for example, / CPU.

2, enter “n” to find the next, enter “n” to find the previous.