Linux must-know commands----------Super detailed analysis of regular operations (view, statistics, backup, vi editor)

The role of common subdirectories

/root: ------------- The
home directory of the system administrator root /home:------------- The home directory of ordinary users
/boot: --- ---------- System kernel startup file
/dev:------------- Device file
/etc:------------- Configuration file
/bin: ------------ Commands executable by all users
/sbin: ------Administrative commands executable by administrators
/usr: - ----------- application
/var:------------- log files, etc.

View file content (cat, more, less, head, tail)

View the contents of the file --------------------------cat (preferably used to view small files)

style:

  • cat [options] file name

Case:

  • cat /etc/hosts

View the content of the file --------------------------more (both large and small files can be used)

style:

  • more [Options] File name

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 one screen
  • Press q to exit

View the content of the file ----------------------------less (adapted to all files can also be searched)

style:

  • less [option] file name

Interactive operation method

  • Page Up Page Up Page Down Page Down Page Down
  • Press "/" key to find content, "n" next content, "N" previous content
  • Other functions are basically similar to the more command

View file content------------------head, tail

head command

  • Purpose: View part of the content at the beginning of the file (default is 10 lines)

style:

  • head -n file name (n is the number of lines viewed)

Case:

  • head /etc/passwd (view the first 10 lines by default)
  • head -5 /etc/passwd (check the first 5 lines)

tail command

  • Purpose: View part of the content at the end of the file (default is 10 lines)

style:

  • tail -n file name (n is the number of lines viewed)
  • tail -f file name (dynamically watch the tail increase)

Case:

  • tali /etc/passwd (view the last 10 lines by default)
  • tali -5 /etc/passwd (view the last 5 lines by default)

Count and retrieve file content (wc, grep)

The content of the statistical file --------------------------wc (Word Count)

style:

  • wc [options] target file

Common command options

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

Case:

  • wc -l 123.txt
  • wc -w 123.txt
  • wc -c 123.txt

Statistics file content--------------------------grep

style:

  • grep [Options] Search condition target file

Common command options

  • -i: Ignore case when searching
  • -v: reverse search, output lines that do not match the search conditions

Search condition setting

  • The string to find is enclosed in double quotes
  • "^……" means beginning with..., "...$" means ending with...
  • "^$" means a blank line

Additional case :

  • Filter the # beginning of /etc/yum.conf and blank lines
  • grep -vE “^*#| ^$” /etc/yum.conf (|There is no blank line after it)

Backup and restore documents (gzip/gunzip, bzip2/bunzip2, tar)

This command is used to compress and decompress files-------------------- gzip, bzip2

  • gzip * ----------------------------Compress all files in this directory

  • gzip -9 file name------------------compress the compressed package -9 means high compression ratio

  • gzip -d file name.gz--------------decompress the compressed package

  • bzip2 -9 File name ----- Compress the file into a file name. bz2 -9 means high compression ratio

  • bzip2 -d file name.bz2-----------file name.bz2 to decompress

  • zip -r opt.zip/opt---------------compress the /opt/ directory into an opt.zip compressed file and place it in the /opt directory

  • unzip -n opt.zip -d /tmp------Unzip the compressed file opt.zip in the specified directory /tmp, if the same file already exists, the unzip command is required to not overwrite the original file

(If it is a minimal installation, there is no bzip2, it needs to be installed)

  • yum -y install bzip2 (install bzip2)
  • yum -y remove bzip2 (uninstall bzip2)
  • yum -y install unzip (install bzip2)
  • yum -y install zip (install bzip2)

Archive directories and files -------------------- tar

Common command options

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

Case:

  1. Now you need to pack /opt into 123.tar.bz2, and put the packed file in /mnt
tar jcvf /mnt/123.tar.bz2 /opt/
  1. Now you need to extract 123.tar.bz2 to the /mnt/123 directory
tar jxvf /mnt/123.tar.bz2 -C/mnt/123
  1. Now you need to pack /opt into 123.tar.gz, and put the packed file in /mnt
tar zcvf /mnt/123.tar.gz /opt/
  1. Now you need to unzip 123.tar.gz to /mnt
tar zxvf /mnt/123.tar.gz -C/mnt/123

Switch between vi text editor working mode and different working modes

The role of a text editor

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

The most commonly used text editor in Linux

  • vi: The default text editor for UNIX-like operating systems
  • vim: vim is an enhanced version of the vi text editor (generally referred to as vi editor)

Three working modes

  • Command mode, input mode, last line mode

Switch between different modes

Insert picture description here
Three modes of vi:

  1. Command mode (vi+file name, enter the command mode. To exit the command mode, you can enter q in the last line mode) You can use the following commands:
  • dd: delete a line
  • u: Withdrawal
  • yy: copy a line
  • p: repost the copied line to the line below the line where the cursor is
  • P: Paste the copied line on the line above the line where the cursor is
  • x: Delete the character where the cursor is (press and hold, first delete the character behind the cursor, and then delete the front)
  • r: replace the character at the cursor
  • zz: save and exit
  1. Input mode (i, l, a, A, enter the editing mode. Exit the input mode, press Esc to return to the command mode) in this mode, you can modify the content of the file just like in the Notepad.
  • a: The text will be inserted after the cursor position (append)
  • A: Text will be inserted at the end of the line where the cursor is
  • i: Insert text before the cursor position (inset)
  • l: Insert text before the first non-blank character in the line where the cursor is
  • o: Insert text at the beginning of the line below the cursor
  • O: Insert text at the beginning of the line above the cursor
  • g: Press 2 to move the cursor to the first line of the text
  • G: Press 1 to move the cursor to the last line of the text
  1. Last line mode (input ":" in the command mode to enter the last line mode. Exit editing mode, press Esc to return to command mode) You can use the following commands:
  • :Q --------------Exit command mode
  • :W -------------Save the modified content
  • :Wq -----------Save and exit
  • :Q! ------------Exit without saving

Basic operation of the last line mode

Text content replacement

command Features
: s /old/new Replace the first character "old" string found in the current line with "new"
: s /old/new/g Replace all the strings "old" found in the current line with "new"
:#,# s/old/new/g Replace all strings "old" with "new" in the range of line number "#,#"
:% s/old/new/g Replace all strings "old" with "new" in the entire file
: 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

Guess you like

Origin blog.csdn.net/weixin_48190875/article/details/107214196