linux file search command

File search, search, and view commands under Linux 1. The most powerful search command: find the command to find various files 2. Find the file in the file information: locate 3. The directory where the search command is located and the alias information: which 4. Search The directory where the command is located and the path of the help file: whereis5, search for lines matching the string in the file and output: grep6, paging to display a file or any output: more7, paging to display a file and you can go back: less8, specify how many before the display Line file content: head9. Specify how many lines after the file is displayed: tail10. View a file: cat11. View how many characters and lines and bytes are in the file content: wc12. Sort file content: sort

*1. The most powerful search command: find command to find various files*

1. Search and find according to the file or directory name [Search directory] [-name or -iname] [Search characters]: The difference between -name and -iname is case-sensitive and the other is case-insensitive eg: search in 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 capitalized 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, search to init___) 2. Search eg according to file size : find files larger than 100M in the root directory find / -size +204800 where +n means larger than, -n means smaller than, n means equal to 1 data block == 512 bytes 0.5KB, that is 1KB block is equal to 2 100MB == 102400KB204800 block three, according to the owner and group search ①, query belonging group home directory at the root of the file find / home -group root ②, owner home directory query to root The file find /home -user root Four, according to the time attribute searchfind [Path] [Options] [Time] There are three options: -amin access time -cmin file attribute is changed -mmin file content is modified time: +n, -n, n respectively indicate more than n minutes and less than n minutes And n minutes eg: Find files and directories whose attributes have been modified within 5 minutes in the /etc directory. Find /etc -cmin -5 5. Search according to file type or i-node  **\ -type Find according to file type: * f Indicates a file, d represents a directory, l represents a soft link eg: find the file type in the /home directory is find /home -type d   *\ -inum according to the i-node search* eg: find the i-node under the /tmp directory for 400342 File or directory find /tmp -inum 400342 VI.     There are two parameters for combination condition search : ①, -a means that two conditions are met at the same time (and) ②, -o means that two conditions are met either (or) Example : Find files larger than 80MB and smaller than 100MB in the /etc directory find /etc -size +163840 -a -size -204800

*2. Find the file in the file information: locate*

Syntax: locate [file name] -i is case-insensitive. Note: This is different from the find command. Find is a full search, while locate is a search in the file database. Therefore, the execution of the locate command is much faster than the execution of the find command. But there is a problem here. The file database needs to be constantly updated. If we do not update the file database for newly created files, they cannot be found using locate. updatedb manually updates 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 hcfInsert picture description here

*3. The directory and alias information of the search command: which*

Function description: Search the directory where the command is located and the alias information. Syntax: which [command] eg: which ls  Insert picture description here

*4. The directory where the search command is located and the path of the help file: whereis*

Function description: Search the directory where the command is located and the path of the help document. Syntax: whereis [command] eg: whereis ls  Insert picture description here

*5. Search for the line matching the string in the file and output: grep*

Function description: Search for lines matching the string in the file and output the syntax: grep -iv [specified string] [file] -i is case-insensitive -v excludes the specified string eg: search in the /root/install.log file The line containing the mysql string, and output grep mysql /root/install.log This search tool checks the target file line by line according to the pattern specified by the user, and prints the matched line. grep searches for matching characters in the file String, is to search the content in the file, this command is used more

*6. Display a file or any output result in pages: more*

Description: Pagination to display a file or any output result for viewing plain text files (longer) Format: more[options] file

*7. Display a file by page and you can turn back: less*

Less is similar to more, but you can browse files at will with less, and more can only move forward, but not backward, and less will not load the entire file before viewing.

*8. Display the first few lines of file content: head*

head[Required parameter][Select parameter][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 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 the number of lines after the display file: tail*

tail[Required parameter][Select parameter][File] It is used to display the content of 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 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 entire file content at once. The cat command is used to view plain text files (shorter) cat [options] [file]... Insert picture description here

*11. Check how many characters, lines, and bytes of the file content: wc*

Description: By default, the wc command will print the number of line breaks, the number of words, and the number of characters. Usage: wc [options] [file]Insert picture description here

*12. Sort file content: sort*

Usage: sort [option] [file]  Insert picture description hereeg: sort -b h.txt

The above is the Linux-related knowledge shared by Liangxu Tutorial Network for all friends.

Guess you like

Origin blog.csdn.net/manongxianfeng/article/details/113115886