Linux directory and file management theory

1. Priority of command execution:

First priority: the command to specify the path. Absolute path /root/pwd.sh or relative path./pwd.sh

Second priority: the command specified by the alias alias pwd=/root/pwd.sh

Third priority: internal commands

Fourth priority: The hash command
will have a hash table under the linux system. When you first boot this hash table, the hash table will be empty. Whenever you execute a command, the hash table will record the path of the command, which is equivalent to The cache is the same. By default, the shell interpreter will look for the path of the command from the PATH path when the command is executed for the first time. When you use the command for the second time, the shell interpreter will first look at the hash table, and will go to the PATH path without the command. . The hash table can improve the calling speed of commands.

Fifth priority: search through the search order defined by PATH

If none of the above sequence is found, it will report the error "Command not found...".

Two, Linux directory structure

(1) Tree-shaped directory structure

(Two), the root directory

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

(3) Common sub-directories

1、/root /bin /boot /dev /etc
2、/home /var /usr /sbin

(4) Linux directory structure

/root Administrator's host (home) directory
/home/xxx Home directory of ordinary users
/bin Command file directory, storing all user executable commands
/sbin Administrator operation directory, storing management commands that only administrators can execute
/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)
/usr Store system user tools and programs
/media Removable media mount point, such as U disk, optical drive, etc.
/proc The file that stores the mapping system information (not a real system file)
/ mnt Directory for temporarily mounting storage devices
/opt The directory where the third-party application is installed
/tmp Temporary files stored in the system (will be automatically deleted after a period of time)

Three, view the content of the file cat command

Display the content of the entire file directly

cat  [选项 ]     文件名

-Example

cat  /etc/sysconfig/network
cat  /etc/redhat-release/proc/version

-Common options
-n, -b, -s

Common options of cat
-n: number all output lines
-b: no number for blank lines
-s: replace all consecutive blank lines with one blank line
Insert picture description here

Four, view the content of the file

(1) View the file content more command

1. Full-screen display of file content in pages

more   [选项]   文件名

2. Interactive operation method
Press Enter to scroll down,
press the space bar to scroll down one screen,
press the b key to scroll up one screen,
press the q key to exit
, and after scrolling down to the last page, it will automatically exit when
combined with pipeline operations (for example: Is -R /etc | more) cannot page up

(Two), view the content of the file less command

1. The same as the more command, but with more extended functions

less  [选项]    文件名

2. Interactive operation method
Page Up page up, Page Down page down
Press "/" key to find content, "n" next content, "N" previous content
can be realized up and down line by line through the up and down arrow keys Scrolling
Other functions are basically similar
to the more command . It will not automatically exit after
turning down to the last page . It can be turned up when used in combination with pipe operations.

(Three), view the file content head, tail commands

1. The 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 (the default is 10 lines)

tail   -n   文件名
tail   -f   文件名         #跟踪文件尾部内容的动态更新

Example:

tail -5f/var/log/messages

Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

Five, statistical file content wc command

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

wc   选项   目标文件

2. Common command options
-l: count the number of lines
-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

Six, retrieve and filter file content grep command

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

grep  选项   查找条件  目标文件 

2. Common command options
-i, -v, -n, -c, -e
3. Search condition setting
The string to be searched is enclosed in double quotation marks.
"^..." means beginning with..., "... $" means ending with...
"^$" means blank line

4. Common options of grep
-i: case insensitive when searching
-v: display all lines that do not contain matching text (reverse query, reverse matching)
-n: display matching lines and line numbers
-c: output only matches The total number of rows
reached (not the number of matches) -e: to achieve matching of multiple search conditions, logical or relationship
-E: support the use of extended regular expressions, which is equivalent to using the egrep command
Insert picture description here

grep can also be used through the pipe symbol:
Insert picture description here

Seven, compression command gzip, bzip2 command

1. Make and unzip compressed files

gzip   -9    文件名…
bzip2   -9     文件名
zip  -d  .gz格式的压缩文件
bzip2  -d    .bz2格式的压缩文件

Common command options:

    -9、-d
gzip、bzip2命令:
gzip   [-9]   文件名        #gzip制作的压缩文件默认的扩展名为".gz",原始文件不再保留     
bzip2  [-9]   文件名          #bzip2制作的压缩文件默认的扩展名为".bz2",原始文件不再保留

Use the "-9" option to increase the compression ratio
-d: used to decompress and compressed files, equivalent to using the gunzip, bunzip2 command
gunzip file name.gz
gzip-d file name.gz bunzip2
file
name.bz2 bzip2- d file name.bz2

Compression example:
Insert picture description here

Decompression example:
Insert picture description here

Eight, archive command tar command

1. Make archive files and release archive files

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

2. Common command options
-c, -x, -v, -f, -p, -t, -C, -z, -j

3. Common options of 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 during decompression.
-f: Indicates the use of archive files.
-p: Keep 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.
-z: Call the gzip program to compress or decompress.

           cd /etc/
压缩: tar -jcvf usershow.tar.bz2 passwd shadow
解压缩:tar  -jxvf  usershow.tar.bz2  -C  /opt/

Common combination:
compression:  -jcvf Compressed into bz2 format    -zcvfCompress into zp format
Unzip:  -zxvf   -C Decompress zp format, -C is to specify the save location after decompression    -jxvf -C Unzip zb2 format

compression:
Insert picture description here

unzip:
Insert picture description here

Nine, text editor vi commands

(1) The role of the 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 text editor (generally referred to as vi editor)

Insert picture description here

(3) Working mode of vi editor

1. Three working modes
Command mode, input mode, last line mode
2. Switch between different modes
Insert picture description here

(4) Switch from command mode to input mode:

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

(5) Command mode:

Operation type Operation keys Features
Page move Page Down键或ctrl+F Scroll down a whole page
Page Up键或ctrl+B Scroll up a whole page
Quick jump in line Home键或^键、数字0键 Jump to the beginning of the line
End键或$键 Jump to the end of the line
Quick jump between lines 1G或gg Go to line 1 of the file content
G Jump to the last line of the file
#G Jump to line # in the file (where "" is replaced with a specific number)
M Jump to the middle of the current page
Show line number :set nu Show line number in editor
:set nonu Cancel line number display
delete x或Delete键 Delete a single character at the cursor
dd Delete the line where the current cursor is located (with cut function)
#dd Delete the content of # line starting from the cursor
d^ Delete all characters from before the current cursor to the beginning of the line
d$ Delete all characters from the current cursor to the end of the line
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
#yy Copy the content of the # line starting from the cursor
Paste P Paste below the line where the cursor is
p Paste above the line where the cursor is
Find /word Search the string "word" backwards from the current cursor
?word Search forward from the current cursor
n Locate the next matching search string
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
U Used to cancel all edits made to the current line
Save and exit :w Save the modified content
:w 新文件名 Save as other file
:q :q! Abandon the modification of the file content and exit
zz或 :wq、:x Save the current file content and exit the vi editor
Open new file :e 其它文件名 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 Replace the first string "old" 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

To summarize:
1. View file content (cat, more, less, head, tail)
2. Statistics and retrieve file content (wc, grep)
3. Backup and restore files (gzip/gunzip, bzip2/bunzip2, tar)
4. Vi text editor Switch between working mode and different working modes
5. Basic operation of vi text editor command mode and last line mode

Guess you like

Origin blog.csdn.net/Gengchenchen/article/details/109772837