Theory: Linux directory and file management

One, Linux directory structure

■ Tree directory structure
■ Root directory

  • The starting point of all partitions, directories, files, etc.
  • In the entire tree-like directory structure, use an independent "/" to indicate

■ Common subdirectories

  • /root /bin /boot /dev /etc
  • /home /var /usr /sbin

■ 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 files
■ /dev: device files
■ /etc: configuration files
■ / bin: commands executable by all users
■ /sbin: administrative commands executable by administrators
■ /usr: application programs
■ /var: log files, etc.

2. View and retrieve files

2.1, view the content of the file cat command

■ Display the content of the file

cat [选项] 文件名...

2.2, view file content more command

■ Full-screen display of file content in pages

more [选项] 文件名...

■ 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

2.3, view file content less command

■ Same as the more command, but with more extended functions

less [选项] 文件名...

■ Interactive operation method

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

2.4, view the file content head, tail commands

■ head command

  • Purpose: View part of the content at the beginning of the file (default is 10 lines)
格式:head -n 文件名…

■ tail command

  • Purpose: View a small part of the content at the end of the file (default is 10 lines)
格式:tail -n 文件名…
或
格式:tail -f 文件名…

The difference between tail -f and tail -n is that "-f" is for dynamically viewing log information. After tail -f is entered, it will always check the data accumulation situation at the end of the log.

2.5, statistical file content wc command

In the process of maintaining the Linux system, in addition to viewing the content of the file, sometimes it is necessary to perform statistics on the content of the file or find the text content that meets the conditions.
■Statistics on the number of words in the file (word count) and other information

wc [选项]...目标文件

■ Common command options

  • -I: count the number of rows
  • -W: count the number of words
  • -c: count the number of bytes

2.6, retrieving and filtering file content grep command

■ Find and display the line containing the specified string in the file

grep [选项]...查找条件...目标文件

■ 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 blank line

Three, backup and restore documents

3.1, compression commands grep, bzip2 commands

■ Make compressed files, unzip compressed files

gzip -9 文件名       对压缩包进行压缩
bzip2 -9 文件名     对压缩包进行压缩
gzip -d 文件名 .gz            对压缩包进行解压缩
bzip2 -d 文件名 .bz2       对压缩包进行解压缩

■ Common commands:
-9: high compression ratio
-d: decompression

3.2, archive command tar command

■ Make archive files, release archive files

tar [选项]...归档文件名 源文件或目录
tar [选项]...归档文件名 [-C 目标目录]

■ Common command options
-c: create a tar format package file

-C: Specify the target folder to be released when decompressing

-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 compress or decompress

-f: file name, the specified file name is archived

For example: Now you need to compress /opt in the package format 123.tar.bz2, and place the packaged file in /mnt.
tar jcvf /mnt /123.tar.bz2 /opt/

Four, vi text editor

4.1, text editor vi command

■ The role of the text editor

  • Create or modify text files
  • Maintain various configuration files in 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)

4.2. Working mode of vi editor

■ Three working modes

  • Command mode, input mode, last line mode

■ Switch between different modes
Insert picture description here

4.3. Basic operations in command mode

Command mode (vi file name, you 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: Paste the copied line on 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 located (press and hold, delete after the cursor, and then delete the front)

r: replace the character at the cursor

ZZ: save and exit (shift+z+z)

4.3. Basic operations in input mode

Input mode (i, l, a, A enter the input mode. Exit the input mode, press ESC to return to the command mode), in this mode, you can modify the content of the file like in a notepad.
a: The text will be inserted after the cursor position (append)

A: Insert text at the end of the line where the cursor is

i: Insert text before the cursor position (insert)

I: 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 (open)

O: Insert text at the beginning of the line above the cursor

g: Press twice, the cursor moves to the first line of the text

G: Press once, the cursor moves to the last line of the text

4.4 Basic operations in the last line mode

The last line mode (input in the command mode: enter the last line mode. To exit the input mode, press ESC to return to the command mode)
you can use the following command:
:q to exit the command mode

:W save the modified content

: Q! Exit without saving

: Set nu display line number

:/Abc Find the character abc, press n to find the next, press N to find the previous

: S /oid/new Replace the first string "old" found in the current line with "new"

: S /oid/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 line number "#,#" range

:% s /old/new/g replace all the strings "old" with "new" in the entire file

:S /oid/new/c Add the c command at the end of the replacement command, and the user will be prompted to confirm each replacement action

Guess you like

Origin blog.csdn.net/weixin_44733021/article/details/109183395