Terminal Command Format
command [options] [params]
-
Command Indicates the command name, for example:
-
PWD Displays the path of the current directory
-
Ls Displays the files and directories in the current directory
-
-
Options Options for command control, for example:
-
Rm -r a Recursively deletes directory A
-
Ls -a Queries all directories and files in the current directory, including hidden files
-
Ls -l Displays detailed information about directories and files in the current directory in a list
-
Ls-lh is more humanized than LS-L
-
-
The target of the params operation is params, such as rm -r a, where a is params
File operations
-
Create file: touch A.txt Creates a file a.txt in the current directory
-
Deleting files: rm a.txt Deletes the a.txt files from the current directory
-
Edit file: vi a.txt Creates and edits the file if it is not present (vim is a more powerful version of vi)
-
Viewing file Contents
-
Cat a.txt View the contents of the a.txt file in the current directory
-
Cat -b a.txt Displays line numbers (ignore blank lines)
-
Cat -n a.txt Displays line numbers (blank lines are not ignored)
-
More a. TXT View the contents of a file in separate screens. Press the space bar to view the next screen
-
Grep hello a.txt Searches for the string “hello” and displays all lines containing the string
-
Grep -n hello a.table The line number is displayed
-
Grep -ni hello a.txt displays the line number and is case insensitive
-
Grep ^hello a.txt Matches all lines starting with “hello” and the matching rule complies with the regular expression. For example, if you match all lines ending with “hello”, the matching rule is “hello$”
-
-
copy
-
Cp a.txt b. TXT Copy a.txt from the current directory to the current directory and rename it b.txt
-
Cp -i a.testt b.testt If file b. testt exists in the current directory, the file is overwritten
-
-
Output content to a file
-
Echo hello world > test. TXT Empty test. TXT and output hello world to the file
-
Echo hello world >> test. TXT Add “Hello world” to the file based on the original content
-
Ls-lha >> test. TXT Output the result of ls-lha to a file
-
-
Move: mv a.txt ~/Desktop/b.txt Move the file A.txt to the user /Desktop directory and rename it b.txt
Directory operations
-
Create a directory: mkdir a
-
Create a nested directory: mkdir -p A /b/c -p Indicates that the directory is created in recursive mode
-
Deleting a directory: rm -r a Deletes directory A recursively, that is, from inside to outside
-
View the contents of the directory: ls
-
View the contents of a directory with conditions
-
Ls 1*3. TXT Displays TXT files whose names start with 1 and end with 3. * can match multiple characters
-
ls 1? 3.txt ? It can match 1 character
-
Ls [0-9].txt can match a number
-
Ls [234]. TXT can match one of the digits 2, 3, or 4. The matching rule complies with the regular expression
-
Ls – lha | grep a.t xt to view the contents of the current directory information, and only look at file “a.t xt.” “
-
.
-
-
View the tree structure of a directory: tree Displays all contents of the current directory, including nested contents. -d indicates that only directories are displayed in the tree structure
-
Copy: cp -r a b Copy directory A to the current directory and rename it b
-
Move: mv a ~/Desktop Move directory A in the current directory to the Desktop directory in the user directory
Pipe command operator
“|” commandment pipeline operator, or pipe. The output of the command on the left of the pipe character is used as the input of the command on the right. Continuous use of pipe characters means that the output of the first command is input to the second command, the output of the second command is input to the third command, and so on.
Such as: ps – ef | grep Java
- Ps is a powerful tool for viewing processes in Linux. -e displays all processes and -f displays all processes
- Grep is a very efficient query tool that can query a line of text with a certain keyword
Search for processes with keyword “Java” and list them one by one.
Using ps -ef alone will list all the processes, but we only want to see if the desired process exists in the background, so use pipe to pass all the process information to grep to help us search for the desired process information.
Remote management
-
On-off operation
-
Shutdown in a minute to turn it off (the default), equivalent to a showdown now
-
Shutdown -c Cancels the shutdown
-
Shutdown -r Shuts down and restarts the computer (operation takes one minute by default)
-
Shutdown -r now Restart immediately
-
Shutdown +5 Shutdown after 5 minutes, that is, shutdown +10 shutdown after 10 minutes
-
Shutdown at 21:10
-
-
View and configure nic information
-
Ipconfig Displays the network adapter information of the computer
-
Ping baidu.com The ping command is used to check whether the connection between the local computer and the target computer is normal (whether the local computer can communicate with the target computer, and whether the target computer can receive and send data back and forth). The current command checks whether the connection between the computer and baidu.com is normal. (For example, the computer sends 56 bytes of data to Baidu.com, and then receives 52 bytes of data from Baidu.com, so we can know the time it takes to receive 52 bytes of data. Of course, the time taken for each reception is different. If the reception fails for 5 times of 10 times, It means that the connection is not very smooth, similar to two people on the phone, one of the signal is not good will lead to incomplete speech)
-
Network card network card is a dedicated network connection communication hardware equipment, the computer wants to access the Internet, network card is a necessary condition; There are three types: integrated NIC, wireless NIC, virtual NIC, and independent NIC
-
IP Address An IP address is the address information set on a network card. If a computer is a mobile phone, the network card is a SIM card and the IP address is a mobile phone number
-
Domain name Because IP addresses are not easy to remember, domain names are generated, and unique domain names can be used as IP addresses. A good domain name is also a symbol of identity, such as “baidu.com”.
-
Port The port number is used to access programs running on the computer
-
-
Remote login
SSH username @ IP/domain name
Rights management
Permission to use directories/files/applications
permissions | abbreviations | The entire | The numbered |
---|---|---|---|
read | r | read | 4 |
write | w | write | 2 |
perform | x | excute | 1 |
-
– indicates a file. D indicates a directory. – indicates a file.
-
In each group, “-” indicates none. For example, “-wx” indicates that a file or directory can be written, executed, or read. The first group distinguishes the owner (current user) permission of the file or directory. The second group distinguishes group permissions for files or directories; The third group marks the operation permissions of other users of files or directories;
-
Number of hard links: The number after the permission description symbol indicates the number of access paths for the file or directory. This is called number of hard links. For example, if you want to access the current directory, run CD. The number of hard links depends on how many sub-directories there are. The number of hard links for files is usually 1 (absolute path), and that for directories is usually multiple.
-
Owner: The number of hard links is followed by the owner (current user)
-
Group: The owner is followed by the group; A system can have multiple users and can be grouped. For example, users A, B, C, D, and E belong to a group named X, and users C, D, and E belong to a group named Y. Then x or y is called a group. When the group permission of a directory or file is readable and executable, all users in the group have the read and execute permission on the file or directory.
-
The following values are size (KB), time (month), time (day), time (hour: minute), and file name/directory name
-
-
Permission modification, chmod + or -rwx directory or file name, + means to add permission, – means to remove; For example, run the chmod -r app command to change the permissions of the app directory in the current directory to remove the permissions of the app directory
-
For example, chmod -r 777 file or directory name indicates that the owner can read and write the file or directory, the group can read and write the file or directory, and other users can read and write the file or directory. So u equals 777, g equals 777, o equals 777
-
Digital illustration
U = owner G = group O = Other users r—w—x r—w—x r—w—x 4-2-1 4-2-1 4-2-1 0 if not specified. – No permission
4 2 1 = 7 rwx
4 2 0 = 6 rw-
.
-
-
To change the permission owner of a file or directory, chown -r (for a directory, -r is required for recursion) User name Name of the file or directory
-
To change the owning group of a file or directory, run the following command: CHGRP -r Group name File name/directory name
-
Super user root: The administrator account of the Linux server is usually root. This user has the permission to control and access all resources in the operating system and is used for system maintenance and management. To allow more people to participate in the maintenance of the server, the super user will create multiple ordinary user accounts. These ordinary users log in to the server remotely through SSH to operate their own household affairs, but cannot operate other people’s affairs, and cannot configure the operating system. If ordinary users want to perform more advanced operations and need to be authorized, they can add sudo (super user do, which means to use the identity of another user, and the default identity is root) before the command. If the user is not authorized to use SUDu, it will send a warning to the supermanagement.
Group management
-
Group management: In order to facilitate the management of users or projects, simplify the one-to-one setting of multiple users, convenient and efficient management, users are usually managed in groups.
-
Common commands for group operations
The command describe Groupadd group name Add a group. The information about the added group is saved in /etc/group Groupdel group name Delete the group The cat/etc/group | grep group name Viewing Group Information CHGRP -r Group name Directory name/file name Modify the owning group of a directory or file Look at super tube root group information such as: cat/etc/group | grep root; Run the cat /etc/group command to query information about all groups.
The group information includes the group ID and additional group ID
Etc is a system-level directory
-
In actual application development, you can set permissions for groups in advance and then add different users to corresponding groups. In this way, you do not need to set different permissions for each user
-
User management
-
Create a user/delete a user/set a user password
The command role describe Useradd -m -g Group name User name New users -m Automatically creates a user home directory
-g Specifies the group to which the user belongs. Otherwise, a group with the same user name will be createdPasswd username Set the password For a common user, run the passwd command to change the password Userdel -r Indicates the user name Delete user The -r option automatically deletes the user’s home directory The cat/etc/passwd | grep user name To view the user The information about the new user is saved in the /etc/passwd file Id is a user name – View the UID and GID of a user who – View the list of users currently logged in whoami – View the name of the currently logged in account For example, create group groupadd dev, create user account useradd -m -g dev LQD, and set password passwd 123456. At this time, it can be connected in the other device through SSH as user LQD, the password is the password of the user;
-
Set the user’s primary group, additional group, and login shell usermod
-
Primary group: When useradd is used to create a user, the specified group is the primary group of the user. When the user creates a file or directory, the owning group of the file or directory is the primary group of the user
-
Additional groups: All the groups to which users belong are additional groups except the primary group. A user can belong to multiple additional groups, but can only belong to one primary group.
-
Change the primary group of a user to usermod -g
-
Change the user’s additional group: usermod -g Group user name for example, usermod -g sudo LQD, then LQD has the permission to use sudo
-
Modify the shell for user login: Usermod -s /bin/bash User name. The shell interface displayed after the next login is the bash interface. (Shell is a terminal command window, and it is a kind of software, including Powershell, Xshell, bash, dash, and so on.
-
-
View command location: which or where, for example, view command location of node which node
-
Bin and sbin. In Linux, most executable files are stored in these two directories. The /bin binary directory is mainly used for common applications. Sbin A directory for storing binary execution files for system management. /usr/bin Directory for storing the soft links of some software installed by users. The /usr/sbin superpipe is used to store administrative applications; Bin stores common applications, and sbin stores management applications
To make the newly installed node command available anywhere on the system, add a soft link to the node execution file to /usr/bin, assuming the downloaded node is decompressed in the current directory and named node: Ln -s./node/bin/node /usr/bin/node is equivalent to adding global variables
-
Switch users and switch to the su – user name of the home directory. For example, if users LQD and ABC exist under home, run the su – ABC command to switch to ABC and access the ABC directory
System information
-
Date Query date
-
CAL Looks at the calendar for the month
-
Cal-y looks at all the calendars in a year
-
CAL 6 2021 View the calendar for June 2021
-
-
Df View disk usage. By default, view disk usage of the current directory
df -h app
View the disk usage details of the app directory in the current directory
-
Du -h app Displays the size of the app in the current directory, including the total size and the size of its subdirectories
-
Ps aux | grep node to view all running node information application process
-
By default, only applications launched by the current user through the terminal are displayed
-
-a Displays all processes on the terminal, including those of other users
-
-u Displays detailed process status
-
-x Displays the process that does not control the terminal
-
-
Top dynamic display running process and sort, exit by q
-
Kill Process ID Kills a process
-
Netstat -ano Displays the port usage
Find and soft links
-
Find find
Find path-name 'filename'
, wildcard characters can be used in file names. /notes -name ‘*.txt’ find./notes -name ‘*.txt’
-
Ln soft link is a shortcut to access an application
Ln -s Source file Absolute path Indicates the destination absolute path
, in user management useful to the command, to achieve global access to node; If -s is not specified, a hard link is created, which means that the source file is copied to the target directory
Package compression and decompression
-
Tar -cvf a.tar *. TXT Package all files ending in. TXT in the current directory and name them as a.tar.
-
-c Generates archive files and creates package files
-
-x Unlocks the file
-
-v Displays the detailed list of archiving and unarchiving and the progress
-
-f specifies the name of the file to archive/unfile. This option should be placed last, followed by the *.tar file
-
-
Tar -xvf a.tar Unpack the a.tar file in the current directory
-
Gzip In Linux, you can use this command to compress the tar file. The extension name is.tar.gz
-
Gz *. TXT: indicates that all. TXT files in the current directory are packed and compressed.
-
Gz: decompresses the gz.tar.gz file in the current directory
-
Gz -c gz Use the C option to save the decompressed file to the gz directory in the current directory
-
eggs
-
NPM Xmas Christmas tree
-
Sl little train
-
Cmatrix byte data stream, Matrix, can be specified by option C, such as cmatrix -c red
-
Visual Chart Hollywood
-
Oneko chases the mouse kitten
-
Fortune famous quotes, ancient poetry
-
Asciiview Image name to ASCII painting style
Vim editing tool
Vim (VI Improved) is a text editor developed from VI and based on VI
-
Command mode, through vim to open the file to enter the command mode, in this state, press the key of the keyboard, the key will be recognized as a command
-
Vim file name open file (create none)
-
Vim file name +3 will position the cursor on the third line
-
Esc, switch to command mode
-
English colon to enter the baseline command mode
-
U Undo the previous operation
-
CTRL + R restores the last undo
-
., English decimal point, repeat last operation
-
Switch to input mode
-
I, switch to input mode and hover the cursor there
-
A, switch to input mode and hover the cursor over the next character
-
A, switch to input mode and hover the cursor after the last character of the current line
-
O, switch to input mode and insert a blank line below the cursor line, remaining at the beginning of the blank line
-
O, switch to input mode and insert a blank line above the cursor line, remaining at the beginning of the blank line
-
-
Replace the pattern
-
R, enter the replace mode, press r and then press another character to replace the character where the cursor is, replace only once to exit the replace mode
-
R, enter the replace mode, it will always replace the character where the cursor is, each replace the cursor one bit later; Press ESC to exit replacement mode
-
-
The cursor
-
Digit + arrow key, such as 6+ down, the cursor can move down six lines at a time (the same as up, down, left and right keys)
-
CTRL + F moves the screen down one page, equivalent to PageDown
-
CTRL + B moves the screen up one page, equivalent to PageUp
-
CTRL + D moves the screen down half a page
-
CTRL + U moves the screen up half a page
-
0 or function key HOME moves the front of the current line
-
$or the function key end moves to the end of the current line
-
H, move to the first character of the first line of the current screen
-
M, moves to the first character in the middle row of the current screen
-
L, move to the first character of the last line of the current screen
-
G, move to the last line of the file
-
Number +G, how many lines to move to the file, such as 20G to move to the 20th line of the file
-
Gg, move to line 1 of this file, equivalent to 1G
-
-
To find the
-
For example, if you want to search for the string file, type /file and press Enter
-
? Word, looking over the cursor for a string named word
-
N, English key, repeat the previous search action, such as input /word to find the string word, press N will repeat the search
-
N, English key, reverse repeat the previous search action, for example, enter /word to search for the string word, press N to search up the string
-
-
The replacement needs to be done in baseline command mode
-
N1,n2s/word1/word2/g, n1,n2 are numbers, find the string word1 between n1 and n2 and replace it with word2; For example, to search for “hello” between lines 1 and 10 and replace it with “hello”, the command is: 1,10s/ hello/ hello/g
-
1,$s/word1/word2/g or %s/word1/word2/g, find word1 from the first line to the last line, and replace with word2
-
1,$s/word1/word2/gc or %s/word1/word2/gc, find word1 from the first line to the last line and replace it with word2, and confirm to the user before replacing
-
-
delete
-
X, deletes the character at the current cursor, equivalent to del
-
X, deletes a character before the cursor, equivalent to backspace
-
Nx, deletes consecutively n characters backwards, including the character at the cursor
-
Dd, delete the line where the cursor is
-
NDD: deletes n consecutive lines down, including the line where the cursor is located
-
D1G, delete all rows from the first row where the cursor is located
-
DG, delete all rows from the line where the cursor is up to the last line
-
D $deletes all data from the cursor position up to the last character of the line, including the position and the last character
-
D0, delete all data from the cursor location up to the first character on the line, including the location and the first character
-
-
copy
-
Yy, copy the line where the cursor is. Press P to copy down the line where the cursor is
-
Nyy, copies n lines down from the cursor, including the line where the cursor is located
-
Y1G, copy all data from the cursor row to the first row
-
YG, copy all data from the cursor line up to the last line
-
Y0, copy all data from the character at the cursor to the beginning of the line
-
Y $, copy all data from the character at the cursor to the end of the changed line
-
-
paste
-
P, paste the copied data on the next line of the cursor
-
P, paste the copied data on the line above the cursor
-
J, combine the cursor line with the next line of data on the same line (the next line of data is placed at the end of the cursor line)
-
-
.
-
-
Edit input mode, normal writing text
-
Bottom line command mode to save and exit
-
Q, quit
-
q! To forcibly exit. If you do not want to save the modified content, you can forcibly exit
-
W, save
-
w! , force save
-
W [filename], save the content to a file, similar to save as, for example, w.a.txt
-
R [filename], read the contents of another file into the line next to the current cursor, for example, read a.txt in the current directory: r a.txt
-
N1,n2 w [filename], save the contents of lines N1 to n2 to a file, including lines n1 and n2, for example, save the data of lines 1 to 10 to a file a.txt: 1,10 w a.txt
-
Wq, save the configuration and exit
-
wq! To forcibly save the configuration and exit
-
! Q, force the exit
-
X, save and exit
-
Set nu displays the line number
-
Set nonu, hide the line number
-
Press ESC to exit baseline command mode at any time
-
-
Create a public vim hidden file in your home directory. Vimrc, and perform global vim common configuration, such as typing “set nu”, and then editing the file will show the trip number by default