File search, find and view commands under Linux
2018-10-12 14:32:43 yellow little _cf read the number 9975
File search, find, and view commands under Linux
1. The most powerful search command: find command to find various files
2. Find file in file information: locate
3. The directory and alias information where the search command is located: which
4. Search The directory where the command is located and the path to the help file: whereis
5. Search for the line that matches the string in the file and output: grep
6. Paging to display a file or any output results: more
7. Paging to display a file and can turn back: less
8. Specify how many lines of file content before displaying: head
9, specify how many lines of content after displaying file: tail
10, view a file: cat
11, view the file content how many characters and lines and bytes: wc
12, sort file content: sort
1. The most powerful search command: find command to find various files
1. Search
find based on file or directory name [Search Directory] [-name or -iname] [Search Character]: The difference between -name and -iname is case sensitive, and one is case insensitive
eg: search under the / etc directory The file or directory named init
①, find / etc -name init (precise search, the name must be init to search)
②, find / etc -iname init (precise search, the name must be init or have capital letters) Searched)
③, find / etc -name * init (fuzzy search, file or directory name ending with init)
④, find / etc -name init ??? (fuzzy search,? Means a single character, that is, init___ is found )
Second, search
eg according to the file size : Find the file larger than 100M in the root directory
find / -size +204800
where + n means greater than, -n means less than, n means equal to
1 data block == 512 bytes 0.5KB, also That is, 1KB is equal to 2 data blocks
100MB == 102400KB204800 data blocks
3. Search according to the owner and the group to which it belongs
①,
find the file whose home group is root
in the home directory. root File
find / home -user root
Fourth, search according to time attributes
find [path] [options] [time]
There are the following three options: -amin access time
-cmin file attribute is changed
-mmin file content is modified
time: + n, -n, n means more than n minutes, within n minutes And n minutes
eg: Find files and directories whose attributes have been modified within 5 minutes under the / etc directory
find / etc -cmin -5
V. Search by file type or i-node
-type Search by file type :
f means file, d Indicates a directory, l indicates a soft link
eg:
find / home -type d
-inum under the / home directory where the file type is a directory, find
eg: find a file or directory
find / tmp with an inode 400342 under the / tmp directory inum 400342
VI. Combined condition search
There are two parameters here:
①, -a means that both conditions are met (and)
②, -o means that both conditions are met (or)
Example: find greater than in / etc directory For files with 80MB and less than 100MB,
find / etc -size +163840 -a -size -204800
2. Find files in the file: locate
Syntax: locate [file name] -i is not case-sensitive
Note: This is different from the find command, find is a full disk search, and locate is to search in the file database. Therefore, the execute of the locate command is much faster than that of the find command. But there is a problem here, the file database needs to be constantly updated. If our newly created file does not update the file database, it cannot be found using locate.
updatedb Manually update the database, but for newly created files in the / tmp directory, the file database cannot be updated, because the / tmp directory is not included in the file database.
eg: locate hcf
3. The directory and alias information where the search command is located: which
Function description: Search the directory and alias information where the command is located
Syntax: which 【command】
eg: which ls
4. The directory where the search command is located and the path of the help document: whereis **
Function description: the directory where the search command is located and the path of the help document
Syntax: whereis [command]
eg: whereis ls
5. Search for matching lines in the file and output: grep
Function description: Search for the line matching the string in the file and output
Syntax: grep -iv [Specify the string] [File]
-i Case-insensitive
-v Exclude the specified string
eg: find in the /root/install.log file The line containing the mysql string and output
grep mysql /root/install.log
This search tool performs line-by-line matching check on the target file according to the pattern specified by the user, and prints the matched line.
Grep searches for matching characters in the file The string is to search the content in the file, which is used more after this command
6. Paging to display a file or any output results: more
Description:
Paginate a file or any output results
Used to view plain text file (longer) format
Format:
more [options] file
7. A file is displayed on the page and can be turned back: less
less is similar to more, but you can use less to browse files at will, and more can only move forward, but not backwards, and less will not load the entire file before viewing.
8. Display the first few lines of file content: head
head [required parameters] [selected parameters] [file] is
used to display the number of lines at the beginning of the specified file
Command parameters:
-n 10 Display the first 10 lines
-n -10 Normal output but not display the last 10 lines
eg: display new.txt The first two lines of content
head -n 2 new.txt
head -2 new.txt
9. Specify how many lines to display after the file: tail
tail [necessary parameter] [select parameter] [file] is
used to display the number of lines at the end of the specified file
Command parameters:
-n 10 display the next 10 lines
-f continuously refresh the displayed content
eg: display the last two lines of content in new.txt
tail -n 2 new.txt
tail -2 new.txt
eg: specify to display
tail from the second line -n +2 new.txt
10. View a file: cat
Description: Display the contents of the entire file at once. The
cat command is used to view plain text files (shorter).
Cat [options] [file] ...
11. See how many characters, how many lines, and how many bytes are in the file: wc
Description: The wc command prints the number of line breaks, words, and characters by default.
Usage: wc [options] [file]
12. Sort file content: sort
Usage: sort [options] [file]
eg: sort -b h.txt