Linux CentOS7 commonly used commands 2 (files and directories)

One, Linux directory structure

1. The starting point of all partitions, directories, files, etc.
2. In the entire tree-shaped directory structure, an independent "/" is used to indicate
3. Common directory structure

/root Administrator's host (home) directory
/home/xxx Home directory of ordinary users other than the root user
/bin Store binary files, all user-executable commands. Actually a soft link, link to /usr/bin
/sbin Store binary files, only administrative commands that can be executed by the administrator. Soft link to /usr/sbin
/boot System kernel, startup file directory
/dev Store device files (CD-ROM, hard disk, etc.)
/etc Store configuration files for system programs and most applications (rpm, yum installation)
/where Store files that can be changed, including various log files
/lib Store the dynamic link shared library file of the system program (similar to the DLL file in windows). Soft link to /usr/lib
/usr Store system user tools and programs
/media Removable media mount points, such as USB flash drives, CD-ROM drives, etc.
/proc File to store the information of the mapping system
/ mnt Directory for temporarily mounting storage devices
/opt The directory where the third-party application is installed
/tmp Temporary files stored in the system

Two, command learning

(1) View the file command cat

1. Directly display the content of the entire file (it is recommended to view dozens of lines of file content, large files are not recommended)

 cat [选项]  文件名…

Suitable for short files.
Example: [root@localhost /]# cat /etc/sysconfig/network
2. Common options

  • -n: Number of lines of all output
  • -b: Do not number blank lines
  • -s: Replace all consecutive blank lines with one blank line

(2) View the content of the file more

1. Full-screen display of file content in pages

 more [选项] 文件名… 

2. Interactive operation method

  • Press Enter to scroll down line by line
  • Press the space bar to scroll down one screen
  • Press the b key to scroll up
  • Press q to exit

Note: It will automatically exit after
turning down to the last page. When combined with pipeline operation (for example: ls -R /etc | more), it is not possible to page up.
(3) View file content less
1. Same as the more command, but with more extended functions many

less [选项] 文件名… 

2. Interactive operation method

  • Page Up page up, Page Down page down
  • Press the "/" key to find the content, "n" the next content, "N" the previous content
  • Scroll up and down line by line through the ↑ and ↓ arrow keys
  • Other functions are basically similar to the more command

After turning down to the last page, it will not automatically exit. When combined with pipe operation, it can be turned up.

(4) View the file content head and tail commands

1.head command
Purpose: view part of the content at the beginning of the file (default is 10 lines)

 head -n 文件名…   #n为行数

2.tail command
Purpose: view a small part of the content at the end of the file (default is 10 lines)

 tail -n 文件名…
 tail -nf 文件名  #动态更新文件尾部n行的内容

(5) Command wc for the content of statistical files

1. Count the number of words in the file (Word Count) and other information
wc [Options]… Target file…
2. Common command options

  • -l: count the number of rows
  • -w: count the number of words
  • -c: count the number of bytes

Note: The wc command without any options uses the three options -lwc at the same time by default

(6) Command grep to retrieve and filter file content

1. Find and display the line containing the specified string in the file

grep [选项]  … 查找条件 目标文件 (要查找的字符串以双引号括起来)

2. Common command options

  • -i: not case sensitive when searching
  • -v: Display all lines that do not contain matching files (reverse query, reverse matching)
  • -n: display matching line and line number
  • -c: Only output the total number of matched rows (not the number of matched rows)
  • -e: Realize the matching of multiple search conditions, logical or relationship
  • -E: Support the use of extended regular expressions, which is equivalent to using the egrep command
  • -o: Exact match, which means "match only"

3. Search condition setting
The string to be searched is enclosed in double quotation marks

  • "^..." means beginning with..., "...$" means ending with...
  • ②"^$" means a blank line.
    Example: Use two methods to find the file ifcfg-ens33. Is the IP address of the ens33 network card statically configured or dynamically obtained?
grep -i"bootproto" etc/sysconfig/network-scripts/ifcfg-ens33 
cat /etc/sysconfig/network-scripts/ifcfg-ens33 | grep -ie“dhcp” -ie“static”

(7) Compression commands gzip, bzip2

1. Make compressed files, unzip compressed files

 gzip [-9] 文件名…  #gzip制作的压缩文件默认的扩展名为“.gz”,原始文件不再保留
 bzip2 [-9] 文件名… #bzip2 制作的压缩文件默认的扩展名为“.bz2”,原始文件不再保留

Compressed files in gzip -d .gz format
bzip2 -d compressed files in .bz2 format
2. Common command options

  • -9: Option can increase the compression ratio
  • -d: used to decompress and compressed files, equivalent to using gunzip, bunzip2 commands
    gunzip file name.gz
    gzip -d file name.gz
    bunzip2 file name.bz2
    bzip2 -d file name.bz2

(8) Archive command tar (compression, decompression recommended)

1. Develop and release archive files
tar [Option] …Archive file name source file or directory
tar [Option] …Archive file name [-C target directory]
2. Common options for tar :

  • -c: Create a package file in .tar format
  • -x: Unzip the package file in .tar format
  • -C: Specify the target folder to be released when decompressing
  • -f: indicates the use of archive files
  • -p (lowercase): preserve file and directory permissions when packaging
  • -P (uppercase): keep the absolute path of files and directories when packaging
  • -t: list the files in the package
  • -v: output detailed information
  • -j: call bzip2 program to compress or decompress
  • -z: call gzip program to compress or decompress

bzip2:
compression: tar jcvf file name.bz2 source file or directory
decompression: tar jxcf file name.bz2 -C target directory
gzip:
compression
: tar zcvf file name.bz2 source file or directory decompression: tar zxcf file name.bz2 -C Target directory
Example: cd /etc/

 tar -jcvf usershow.tar.bz2 passwd shadow
 tar jxvf usershow.tar.bz2 -C /opt/~

(9) text editor vi command

1. The role of a text editor

  • Create or modify text files
  • Maintain various configuration files in the Linux system

2. Commonly used text editors in Linux

  • vi: The default text editor for UNIX-like operating systems
  • vim: vim is an enhanced version of the vi text editor

3. Command mode: After starting the vi editor, enter the command mode by default. This mode mainly completes related operations such as cursor movement, string search, and deletion, copying, and pasting of file content.
4. Input mode: the main operations in this mode It is to enter the content of the file, you can modify the body of the text file, or add new content. When in the input mode, the last line of the vi editor will appear "-- INSERT --" status prompt message
5. Last line mode: In this mode, you can set the vi editing environment, save the file, exit the editor, and check the content of the file. Perform operations such as search and replacement. When in line mode, the last line vi editor appears colon ":" prompt
6. command mode to input mode:
① A: Insert after the current cursor position
② i: Inserts before the current cursor position
③ o: Insert a new line content below the line
where the cursor is ④ O: Insert a new line content above the line where the cursor is
⑤ A: Insert content at the end of the current line
⑥ I: Insert content at the beginning of the current line
Insert picture description here

Command mode operation

Operation type Operation keys Features
Page move Page Up key or ctrl+B Scroll up a whole page of content
Page move Page Down key or ctrl+F Scroll down a whole page of content
Quick jump within the line Home button or ^ button, number 0 button Jump to the beginning of the line
Quick jump within the line End key or $ key Jump to the end of the line
Quick jump between lines 1G or gg#G, M Go to line 1 of the file content
Quick jump between lines G Jump to the last line of the file
Quick jump between lines #G Jump to line # in the file (where "#" is replaced with a specific number)
Quick jump between lines M Jump to the middle of the current page
Show line number : set no Show line number in editor
Show line number :set nonu Cancel line number display
delete x or Delete key Delete a single character at the cursor
delete dd Delete the line where the current cursor is located (with cut function)
delete #dd Delete the content of # line starting from the cursor
delete d^ Delete all symbols from before the current cursor to the beginning of the line
delete d$ Book all characters except the current cursor to the end of the line
delete dw Delete the entire word at the cursor
Replacement character R或shift+r Replace the character at the current cursor
copy yy Copy the entire content of the current line to the clipboard
copy #yy Copy the content of the # line starting from the cursor
Paste P Paste below the line where the cursor is
Paste p Paste above the line where the cursor is
Find /word Search the string "word" backwards from the current cursor
Find ?word Search forward from the current cursor
Find n Locate the next matching search string
Find N Locate the last matching search string
Revoke u Press once to cancel the most recent operation: repeat the u key to resume multi-step operation
Revoke U Used to cancel all edits made to the current line
Save and exit :w Save the modified content
Save and exit :W new file name Save the new file name as another file
Save and exit :q drop out
Save and exit :q! Force quit
Save and exit ZZ or: wq,: x Save the current file content and exit the vi editor
Open new file :e other file name Open a new file for editing
Read file content :r Other file names. Read the contents of other files in the current file
File content replacement s/old/new Replace the first string "old" found in the current line with "new"
File content replacement s/old/new/g Replace all the strings "old" found in the current line with "new"
File content replacement :#,# s/old/new/g Replace all strings "old" with "new" in the range of line number "#, #"
File content replacement :% s/old/new/g Replace all strings "old" with "new" in the entire file
File content replacement s/old/new/c Add the c command at the end of the replacement command, and the user will be prompted for confirmation for each replacement action
File content replacement :8,11 m 4 Cut the contents of lines 8-11 below line 4
File content replacement :8,11 co 4 Copy the contents of lines 8-11 below line 4

Guess you like

Origin blog.csdn.net/weixin_53567573/article/details/113528086