Introduction to Linux-Directory and File Management


1. Directory and file management

1. Directory and file management

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

2. Common subdirectories

Subdirectory Description
/root Administrator's host (home) directory
/home 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 executable by the administrator. Soft link to /usr/sbin
/boot System kernel, startup file directory
/dec 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 point, such as U disk, optical drive, etc.
/proc File to store the mapping system information
/opt The directory where the third-party application is installed
/tmp Temporary files stored in the system
/ mnt Directory for temporarily mounting storage devices

2. View the contents of the file: cat

1. Display the content of the entire file directly

Insert picture description here

2. Common options

Common options Description
-n Number all output lines
-b No number for blank lines
-s Replace all consecutive blank lines with one blank line

3. View the content of the file: more

1. Full-screen display of file content in pages

Insert picture description here

2. Interactive operation method

  • Press Enter to scroll down line by line
  • Press space to scroll down one screen
  • Press b to scroll up one screen
  • Press q to exit
  • It will automatically exit after turning down to the last page
  • Cannot page up when used in conjunction with pipeline operation

Fourth, view the content of the file: less

1. Same as more, but with more extended functions

Insert picture description here

2. Interactive operation

  • Page Up page up, Page Down page down
  • Press "/" key to find content, "n" next content, "N" previous content
  • Use the ↑ and ↓ arrow keys to scroll up and down line by line
  • Other functions are basically similar to the more command
  • The next page will not automatically exit
  • Page up can be paged in combination with pipe operation

Five, view the content of the file: head, tail

1. The head command

  • Purpose: View the beginning part of the file (default 10 lines)
    Insert picture description here

2. Tail command

  • Purpose: View a small part of the content at the end of the file (default 10 lines)
    Insert picture description here

Six, the content of the statistical file: wc

1. Count the number of words in the file (word count) and other information

Insert picture description here

2. Common command options

Common options Description
-l Statistics rows
-w Count the number of words
-c Count bytes
  • Note: The wc command without any options uses the three options -lwc at the same time by default

7. Retrieve and filter file content: grep

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

Insert picture description here

2. Common command options

Command options Description
-i Not case sensitive when searching
-v Display all lines that do not contain matching files (reverse query, reverse matching)
-c Only output the total number of matched rows (not the number of matched rows)
-n Show matching line and line number
-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 find is enclosed in double quotes
  • "^..." means beginning with..., "...$" means ending with...
  • "^$" means blank line

4. Example

grep -ie "^NAME" -e "7$" CentOS-Vault.repo

8. Compression commands: gzip, bzip2

1. Make and unzip compressed files

Insert picture description here

2. Common command options

  • Use the "-9" option to increase the compression ratio
    . The value of the command can be an integer from 1 (the fastest compression speed, the lowest compression quality) to 9 (the slowest compression speed, the highest compression ratio), and its default value is 6 (compression speed and compression quality are more balanced value)
  • -d: used to decompress the compressed files, equivalent to using gunzip, bunzip2 commands
    Insert picture description here

Nine, archive command: tar

1. Make archive files and release archive files

Insert picture description here

2. Common command options

Command options Description
-c Create a package file in .tar format
-x Unpack the package file in .tar format
-C Specify the target folder to be released when decompressing
-f Indicates the use of archive files
-p Preserve file and directory permissions when packaging
-P Keep the absolute path of files and directories when packaging
-t List the files in the package
-v Output detailed information (Verbose)
-j Call bzip2 program to compress or decompress
-with Call gzip program to compress or decompress

10. Text editor: vi

1. The role of a text editor

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

2. 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 editor

3. The working mode of the vi editor

  • Command mode After
    starting the vi editor, it enters 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.
  • Input mode
    The main operation in this mode is to record the content of the file, you can modify the text of the file or add new content. When in input mode, the last line of the vi editor will appear "–INSERT–"
  • Last line mode In
    this mode, you can set the vi editing environment, save files, exit the editor, and perform operations such as searching and replacing file contents. When in the last line mode, a colon ":" prompt appears on the last line of the vi editor

4. Switch between different modes

Insert picture description here

5. Switch command mode to input mode

command Description
a Insert content after the current cursor position
i Insert content before the current cursor position
O Insert a new line below the line where the cursor is
O Insert a new line above the line where the cursor is
A Insert content at the end of the line
I Insert content at the beginning of the line

6. Command mode

Operation type Operation keys Features
Page move Page Down or Ctrl + F
Page Up or Ctrl + B
向下翻动一整页内容
向上翻动一整页内容
行内快速跳转 home键或^键、数字0键
End键或$键
跳转到本行的行首
跳转到本行的行尾
行间快速跳转 1G或gg
G
#G
M
跳转到文件内容的第一行
跳转到文件的最后一行
跳转到文件的第#行(其中"#"用具体数字替换)
跳转到当前页的中间位置
显示行号 :set nu
:set nonu
在编辑器中显示行号
取消行号显示
删除 x或Delete键
dd
#dd
d^
d$
dw
删除光标处的单个字符
删除当前光标所在行(有剪切功能)
删除从光标处开始的#行内容
删除当前光标之前到行首的内容
删除当前光标处到行尾的内容
删除光标处的整个单词
替换字符 R或shift + r 替换当前光标处字符
复制 yy
#yy
复制当前行整行的内容到剪贴板
复制从光标处开始的#行内容
粘贴 P
p
粘贴到光标所在行之下
粘贴到光标所在行之上
查找 /word
?word
n
N
Search for the string "word"
from the current cursor backward to search and
locate the next matched searched string and
locate the previous matched searched string
Revoke u
U
Press once to cancel the most recent operation; repeat the u key to restore multiple operations
to cancel all edits made to the current line
Save and exit :W
:w New file name
: q
:q!
zz or:wq、:x
Save the modified content,
save as another file,

abandon the modification of the file content and exit,
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 Read the contents of other files in the current file
File content replacement :s /old/new
:s /old/new/g
:#,# s/old/new/g
:% s/old/new/g
:s /old/new/c
Replace the first string "o1d"
found in the current line with "new" Replace all the strings found in the current line "old" with "new"
and replace all within the range of line number "#,#" The string "old" is "new"
to replace all the strings "old" is "new" in the entire file. The
c command is added at the end of the replacement command, and the user will be prompted for confirmation for each replacement action
:8,11 m 4 Cut the contents of lines 8-11 below line 4
:8,11 co 4 Copy the contents of lines 8-11 below line 4
Tips shift + 3 key combination All relevant strings can be highlighted in the script file

Guess you like

Origin blog.csdn.net/Lucien010230/article/details/112990806