Abstract: Linux common command, very suitable for you.
When it comes to operating systems, the first thing that comes to mind are Windows and Linux. Windows operates in an intuitive and visual way, which is especially suitable for operating the corresponding software on the desktop PC. In contrast to Windows, many Linux controls require commands to operate.
Linux command line operations, especially in server-side programming, management, operation and maintenance, are very simple, short, concise. A short line of combined commands can complete complex and efficient functional operations that require various processing and integration in Windows. No, it’s all down there.
Start network command
IP addr Displays the nic information
Service network start Starts a NIC. Service network stop Stops a NIC. Service network restart Restarts the networkCopy the code
The PWD command
The PWD command is used to view the current directory path
All absolute paths under Linux start at the root directory “/”
/root: indicates the root directory of user root in Linux
/home: the default root directory for other users on Linux (for example, if a bow user is created on Linux, a bow directory will be generated under /home as the root directory for the BOW user).
/etc: indicates the directory of the system configuration file in Linux
/ TMP: temporary file directory, available to all users
The ls command
Ls [Parameter] Directory path ls Displays files in a directory
Ls -l # Displays details about the current directory ls -a # Displays details about all files (including hidden files) in the current directory ls -la # Displays details about all files (including hidden files) in the current directory ls -lh #h displays the size of the file in the appropriate units ls -lh displays the details of the file in the current directory, Ls -l / # displays detailed information about files in the root directory "/" ls /etc # displays detailed information about files in the root directory "/" ls /etc # displays detailed information about files in the root directory "/" ls /etc # displays detailed information about files in the root directory "/" ls --help # Displays the help file of the command --help parameter: (1)--help (2)--h (3)-help (4)-h ll command: It has the same function as ls -l, but is not installed by default on all LinuxCopy the code
The CD command
CD Directory path # Enter a directory. The directory path can be an absolute path (any path starting with/is an absolute path) or a relative path. Relative path: a path starting with a non-/. For example, for the root user, ~ indicates the /root directory. For the bow user, ~ indicates the /home/bow directory
CD / # indicates the root directory of the system. CD usr/ # indicates the usr directory of the current directory. CD. # indicates the upper level of current directory CD.. /.. /usr/local/bin = /usr/local/bin = /usr/local/bin = /usr/local/bin # /etc # /etc # Represents the upper level of the current directory.. CD ~ # Indicates the root directory of the current user. (CD ~ has the same effect as running CD without adding a directory.) # Example: CD ~/data # Indicates the data directory in the root directory of the current user. For example, the root user is in the /root/data directoryCopy the code
The mkdir command
Mkdir directory path # Create a directory. The directory path can be absolute or relative
/dir # create a data directory in /root/ TMP # create a TMP directory in /rootCopy the code
Mkdir A directory is created only when its parent directory exists
Mkdir -p directory # When creating a directory, if there is no parent directory, the parent directory is created, and the directory is created recursively
Mkdir -p a/b/c # create level 3 directories in the current directory
Rmdir commands
Rmdir [parameter] directory path /dir # Delete dir directory under current directory rmdir -p directory path # Delete directory and its parent directory (if the directory is an empty directory) rmdir -p a/b/c # Delete a/b/c directory under current directoryCopy the code
Touch command
Create a file
Touch File path
# create a 1.txt file in /root/2. TXT # create a 2.txt file in /rootCopy the code
The rm command
Rm [parameter] path # Delete command
Rm 1.txt # delete the 1.txt file from the current directory. If y is entered, delete it. If n is entered, delete it
Rm -f /root/2. TXT #-f forcibly deletes 2.txt from /root without prompting
[root@bow ~]# rm -r a/ rm: enter directory "a/". Y rm: Check whether directory A/B is displayed. Y rm: Check whether the directory a/ B/C is displayed. Y rm: Check whether to delete the common empty file "A /b/c/3. TXT". Y rm: Check whether to delete directory A/B/C. Y rm: Check whether to delete the common empty file "A /b/2.txt". Y rm: Check whether to delete directory A/B. Y rm: Check whether to delete the common empty file "A /1.txt". Y rm: Check whether to delete directory "A /". Y rm -rf a/ # delete all the contents in the current directory rm -rf * * # delete all the contents in the current directory rm -rf *. TXT # Delete all the contents in the current directory rm -rf * -rf *s* # Deletes all files or folders in the current directory whose names contain sCopy the code
The echo command
Echo # output command, can input variable, string value
Echo Hello World # print Hello World echo $PATH # print the value of the environment variable PATH, where $is the symbol of the variable value. Echo -n # Prints the content without newline echo -n Hello WorldCopy the code
> and >> commands
And >>: output symbols to output content to a file, > indicates overwrite (deleting the original file content) >> indicates append
Echo Hello World > 1.txt # echo Hello World > 1.txt # echo Hello World > 1.txt # echo Hello World > 1.txt # echo Hello World > 1.txt # Write Hello World echo 1234 >> 1.txt # append 1234 to 1.txt in the current directory, create a new file if it doesn't existCopy the code
Files can be created using either > or >>
File view command
Cat file path # View all contents of the file
Cat /root/1. TXT # Check the contents of 1. TXT in /rootCopy the code
More File path # Page to view file contents
TXT file to view the contents of the common Linux commands in the current directory
# Press space or Enter to continue loading file contents, press q to exit view
When loaded at the end of the file, it will automatically exit view
Less File path # Page to view file contents
TXT # Paging to view the file content, press space to continue loading the file, press q to exit the view, the view will not be automatically quit
Head [parameter] File path # View file from file
TXT # View the first 10 lines of a file Head-n File path # n is a positive integer, indicating that the first n lines of a file are displayedCopy the code
Tail [parameter] File path # View file contents from the end of the file
# Check the last 10 lines of the file
Tail -n File path # n is a positive integer, indicating the last n lines of a file. Tail -15 Common Linux commands. TXT # View the last 15 lines of a file. If the file is updated, the new content is displayed.Copy the code
Tail -f 1.txt # Check the latest contents of 1.txt. Tail -f is used to check log files. Press CTRL+C or CTRL+Z to exit the view
CTRL+C: Pause the process CTRL+Z: Stop the processCopy the code
File editing command
Vi /vim commands: These two commands are almost identical in use (personally I like vim)
Install vim command :(install requires network)
yum -y install vim
Copy the code
The yum command is a command to install software using official resource packs on centos and Red Hat systems
Yum -y install yum -y removeCopy the code
The virtual machine cannot access the Internet.
Ping www.baidu.com CTRL+C or CTRL+Z to exitCopy the code
Vim commands fall into two categories
Vim file path – Enter unedited mode
Non-edit mode command:
Yy: Copy the current line of cursor P: Paste DD: delete cursor current line $: cursor jumps to end of current line ^: cursor jumps to start of current line: S/old string/new string /: replace cursor current line content :% S/old string/new string /g: full text Replace #g to indicate global I to indicate ignore case / What to look for: Look back from the current line of the cursor /d # Look up the d letter in the file? What to look for: Looking forward from the current cursor position? CTRL+F: Scroll down 1 page CTRL+B: Scroll up 1 page set nu: display the line number of the file :set nonu: Delete line number to display u: undo **:set ff: display file format # Unix: file on Unix DOS: file on Windows **: w: save file :q: Exit vim command :wq: save and exit :w! : Forcibly save :q! : Forced exit without saving :wq! : Force save and exit I: enter edit mode with the cursor on the current line O: Enter edit mode with the cursor on the next line (new line) of the current lineCopy the code
Edit mode command:
In edit mode, the cursor position can be controlled by arrow keys, and the file can be entered to the current position of the cursor
ESC: Exits the editing modeCopy the code
The cp command
Cp copy command
Cp [parameter] Source file path Destination file path
TXT 2. TXT # copy 1. TXT to 2. TXT cp -r a data #-r indicates that the directory is copied together with other files in the directory and a is copied to the data directoryCopy the code
The SCP command
SCP Remote copy command. It can copy files from a local server to a remote server, or from one server to another
SCP -r local file path name @ IP [: port] : remote path # will copy the local file to the remote server SCP - r 2. TXT [email protected]: / root/data / # copy 2.txt to /root/data at 192.168.5.105 # copy 2.txt to /root/data at 192.168.5.105 # copy 2.txt to /root/data at 192.168.5.105 # copy 2.txt to /root/data at 192.168.5.105 [email protected]: / root / 3. Bak/root/data # will remote/root / 3. Bak file copy to the local/root/data directoryCopy the code
SCP -r username@ip [:port]: remote file path username@ip [:port]: remote file path # Copy files from one server to another
SCP - r [email protected]: / root/TMP [email protected]: / root/data copies the/root/TMP / # to remote/root/data directoryCopy the code
The mv command
Mv move command, which can move or rename files
Mv Source file path Destination file path # Copy files from one location to another
TXT TMP TMP # change TMP directory to tm mv 12.txt tm # Move 12.txt to tmCopy the code
Man command
Man command, view the command of the command, view the command help document (display the most detailed information)
The man command is similar to the --help parameter of the command.Copy the code
The free command
The free command, which is used to view system memory
Free -h # View the memory usage and display the size in appropriate unitsCopy the code
The df command
The df command is used to view system disks
Df # View disk usage df -h # View disk usage and display size in appropriate unitsCopy the code
Wc command
The WC command, short for word count, looks at the number of words in a file
Wc [parameter] file
TXT #-l Indicates the number of lines in the calculation file. Wc -w Indicates the number of words in the word calculation fileCopy the code
The ps command
Ps command, which is the command to view the system process
ps -aux
ps -ef
JPS looks at Java processes
Kill command
Kill process ID # End a process
Root 21752 1.6 0.5 158800 5532? Ss 08:34 0:00 sshd: root@pts/0Copy the code
Kill 21752 # End SSH login process
Kill -9 Process ID # Forcibly end a process
User and permission commands
Create a user group:
Groupadd User group name # Create a user group
Groupadd bows # Create a user group called bows. Delete a user group: groupdel Name of a user group. Delete a user group only when no users exist in the user groupCopy the code
Create a user:
Useradd user name [-g user group name -g user group name] # Create a user,-g specifies the primary user group of the user,-g specifies the other user groups of the user
Useradd bow -g bows # Create a bow user and specify that the primary user group is bows IDCopy the code
Delete a user:
Userdel user name # delete user bow # delete user bowCopy the code
Switching users:
Su - user name # Switch user and load user's environment variable (recommended to use this way to switch users) su bow # Switch to user BOW (user root does not need to enter a password to switch to another user, other users also need to enter a password to switch to other users) # exit # Log out of the current userCopy the code
Changing a user password:
Passwd bow # Change the password of user bowCopy the code
Jurisdiction:
File Type User Right User Group Right Other User Right -rw -r -- r--. 1 root root 5890 March 23 14:11 Common Linux commands. TXT d RWX r-x r-x 08:06 Data D: folder U: user permission G: user group permission O: other user permission R: read permission 4 W: write permission 2 x: execute permission 1 -: no permissionCopy the code
Chmod Specifies the permission command
1 root root 31 3月 24 07:46 2. TXT chmod u+x 2. TXT # Add execution permission to user -rwxr--r-- 1 root root 31 3月 24 -rwxrw-r--. 1 root root 31 3月 24 07:46 2.txt chmod o+x 2.txt # -rwxrw-r-x. 1 root root 31 3月 24 07:46 2.txt chmod g-w 2.txt # Root root 31 3月 24 07:46 2.txt -rwxr--r-xCopy the code
Use three digits to set permissions for files or directories. The first digit indicates user permissions, the second digit indicates user group permissions, and the third digit indicates other user permissions
1 root root 31 3月 24 07:46 2.txt chmod 766 2.txt chmod 766 2.txt chmod 766 2.txt Rw - -rwxrw-rw-. 1 root root 31 3月 24 07:46 2.txtCopy the code
When setting directory permissions, use the -r parameter to ensure that all files in a directory have the same permissions as directories
Drwxr-xr-x. 4 root root 81 3月 24 08:06 data chmod -r 777 data # set the data directory and all files under it to RWXRWX DRWXRWXRWX. 4 root root 81 March 24 08:06 dataCopy the code
The chown command, which changes the owner of a file
Chown-r user [: user group] directory or file -rwxrw-rw-. 1 root root 31 3月 24 07:46 2. TXT chown bow 2. TXT # change the owning user of 2. TXT to bow-rwxrw-rw -. 1 bow Root 31 3月 24 07:46 2. TXT chown bow:bows 2. TXT # change the user name of 2.txt to bow and the user group name to bow-rwxrw-rw-. 1 bow bows 31 3月 24 07:46 2. TXT DRWXR --r--. 4 root root 81 3月 24 08:06 Data chown -r bow:bows data # Change the user name of the data directory and its subdirectories to BOW, and the user group to bow Bows 81 March 24 08:06 dataCopy the code
To find the command
The find command allows you to find a file by date, name, etc
Find *.txt # find TXT filesCopy the code
The grep command is used to query the content
TXT # Run the following command to query the line containing cat in the common Linux command. TXT fileCopy the code
| channel symbols, and connected to the two commands, after to pass the query result of the previous command to a command
Ps - ef | grep SSHD # to check the SSHD process of ps - ef | grep Java # to see all the Java process # grep -v -v parameter query does not contain line search condition of grep -v cat commonly used Linux command. TXT # for commonly used Linux command. TXT line does not contain the cat in the ps - ef | grep SSHD | grep -v grep # query SSHD process, grep lines are not includedCopy the code
– Understanding query commands
Whoami command # Check the current user name of the system whereis command # Check the path of a software installation whereis python # Check the installation path of Python which command # Check the python executable file pathCopy the code
Compression command
Install zip and unzip commands:
yum -y install zip unzip
Copy the code
Zip Compression command
Zip File name Indicates the path of the file to be compressed
Zip data.zip data # compresses only folders, not the contents of folders. Zip da.zip da/* # Compresses folders and files within folders (compress folders and their next level files). Zip -r data.zip date #-r indicates that the folder and its subdirectory files are recursively compressedCopy the code
Unzip Decompress command
Unzip Zip file path
Zip unzip -l File name unzip -l da.zip unzip da.zip -d destination directory unzip da Unzip da. Zip -d tm/ unzip da. Zip -d tm/Copy the code
The tar command is used to compress and decompress the. Tar and. Tar. gz packages
Compressed. Tar package:
Tar CVF Compressed file name File or directory to be compressed tar CVF 2.tar 2.txt # To compress 2.txt into 2.tar package tar CVF data.tar data # To hyperbolate the data directory into data.tarCopy the code
Decompress. Tar package:
Tar XVF compressed file name [-c specifies the decompressed directory]
Tar XVF 2.tar # Decompress 2.tar to current directory tar XVF 2.tar -c a/ # Decompress 2.tar to current directory tar XVF data.tar # Decompress data.tar to current directory tar XVF 2.tar -c a/ # Decompress 2.tar to current directory tar XVF data.tarCopy the code
Zip.tar.gz package:
Tar ZCVF Compressed file name File to be compressed tar ZCVF tm.tar.gz tm # Compress the tm directory in the current directory to tm.tar.gzCopy the code
Decompress the.tar.gz package:
Gz # Decompress tm.tar.gz to the current directory. Run the gzip command to compress the file into a. Gz package (which can be used to compress Gz gzip data.tar # Data.tar to data.tar.gzCopy the code
The source command
# make configuration file changes immediately effective (you can also reference other shell scripts in shell scripts)
/etc/profile # configure the system environment variables to take effectCopy the code
The export command
Export Import global variables (environment variables) export Variable name = variable value Export Variable name Assignment of variables: Variable name = variable valueCopy the code
<<EOF
< < EOF… EOF: Passes the previous command multiple lines between <
[root@bow ~]# cat <<EOF
> HELLO
> WORD
> JOB
> SMITH
> EOF
HELLO
WORD
JOB
SMITH
Copy the code
<<EOF is the basis for using SQLPlus in shell scripts
[root@bow ~]# cat <<A
> 11234
> 1234
> 1234
> 1253
> 1253
> A
11234
1234
1234
1253
1253
Copy the code
Note :EOF must be written on the top line
[root@bow ~]# cat <<EOF
> ASDF
> EOF
> ASDFASDF
> EOF
ASDF
EOF
ASDFASDF
Copy the code
The cut command
Cut Indicates the interception command
-d specifies the separator between columns. The default separator is \t(line direction TAB) cut -f 1 1. TXT # Take the contents of column 1 in 1.txt (column separator defaults to \t) cut -f 2 1. TXT # Take the contents of column 2 in 1.txt Cut -f 1 -d ',' 3. TXT # select 1 column from 3. TXT file (column separator is,) cut -f 2 -d ',' 3. TXT # select 2 column from 3Copy the code
Wc -l commonly used Linux command. TXT | the cut – f # 1 – d “file is commonly used Linux command. TXT the number of rows (separator is space)
[root@bow ~]# cut -f 1 -d ',' <<EOF
> A,B,C
> D,E,F
> EOF
A
D
Copy the code
Printf command
%ns output string, n is a number, refers to the output of several characters %ni output integer. N is a number that refers to the output of several digits % M. nf and decimal places. For example: %8.2f represents the output of 8 digits, where 2 digits are decimals and 6 digits are integersCopy the code
Printf String content
[root@bow ~]# printf '%s,%s,%s\n' ABC def GHJ KLJ klo qer # Print the third word on a line, separated by commas ABC,def, GHJ KLJ,klo,qer [root@bow ~]# printf '%s %s\n' $(cat 4.txt) # printf '%s %s\n' $(cat 4.txt) $(cat 4.txt) empno ename job A B C D E F G H [root@bow ~]# printf '%s,%s\n' $(cat 5.txt) A,B C,D E,F G,H [root@bow ~]# printf '%5.2f\n' 12.1 #%5.2f outputs a decimal number of length 5, including two decimals 12.10 [root@bow ~]# printf '%5.2f\n' 121234.116134 [root@bow ~]# printf '% I \n' 1234.5678 #% I take only the integer part of the number -bash: printf: 1234.5678: invalid number 1234Copy the code
The awk command
Awk command string to process
[root@bow ~]# awk '{printf $1 "\n"}' 1.txt #printf $n = 1 $n = 1Copy the code
Awk ‘{print $2}’ 1. TXT # print $2
[root@bow ~]# awk '{printf $1 ","}' 1.txt Hello,smith,tomcat,[root@bow ~]# [root@bow ~]# awk '{printf $1}' 1.txt Hellosmithtomcat [root@bow ~]# awk '{printf $1 "\v"}' 1.txt Hello smith tomcat [root@bow ~]# awk '{printf $1 ","}' 1.txt Hello,smith,tomcat,Copy the code
The sed command
Sed parameter command to process
-n The sed command outputs all data to the screen. If you add this option, only lines processed by sed will be printed to the screen. -e allows you to use multiple sed commands to edit the input data. -I Can use sed to modify the read data directly. [root@bow ~]# sed '2p' 1.txt # query line 2 Hello world Smith 18 Smith 18 tomcat etl [root@bow ~]# sed -n '2p' 1.txt Smith 18 [root@bow ~]# sed -i 's/18/20/g' 1.txt Run the sed command to modify the contents of 1.txt. Replace 18 in 1.txt with 20 [root@bow ~]# cat 1.txt Hello world Smith 20 Tomcat ETL a\ Append, which adds one or more rows after the current row. When adding multiple lines, use "\" at the end of each line to indicate that the data is not finished, except for the last line. D delete, delete specified p print, print specified line [root@bow ~]# sed -i '2a! '1.txt # append line after line 2! [root@bow ~]# cat 1.txt Hello world smith 20 ! [root@bow ~]# cat 1.txt Hello world Smith 20 tomcat etl [root@bow]# sed -i '3d' 1.txt ~]# vim 6.txt [root@bow ~]# cat 6.txt abcd/home/bow if ad -e /home/bow abcd/home/bow if ad -e /home/bow abcd/home/bow If AD -e /home/bow = /user/bw # if AD -e /home/bow = /user/bw Error writing: s / / home/bow / / user/bw/g correct term s / / / home / / bow / / / user / / bw/g/root @ bow ~ # sed -i 's / / / home / / bow / / / user \ / bw/g' 6. TXT [root@bow ~]# cat 6.txt abcd/user/bw if ad -e /user/bw abcd/user/bw if ad -e /user/bw abcd/user/bw if ad -e /user/bwCopy the code
Note: String subscripts in Linux start at 0
The service command
Service Service command
Ervice service name [orders] command: enable | disable | start | stop | restart | status start: start the service stop: close the service restart: restart the service status: check the service status of the service Network start service network stop service network restart service network status Check the network status service Iptables start #centos6 or earlier service iptables stop #centos6 or earlier disable the firewall Run the following command to restart the mysql database: service iptables restart service mysqld stop # close the mysql databaseCopy the code
The chkconfig command
The chkconfig command is used to check and set various system services
The chkconfig service name on | off # on said open service off said close service Through the chkconfig set service is permanent centos6 and below version permanently closed or open the firewall the chkconfig iptables on # open the firewall Chkconfig iptables off # Disable firewall permanentlyCopy the code
Firewall:
Centos7 above:
Systemctl start firewalld # Start firewalld # stop firewalld # Stop firewalld # Stop firewalld # Stop firewalld # Stop firewalld # Stop firewalld # Stop firewalld # Stop firewalld Disable firewalld # permanently disable systemctl enable firewalld # Enable firewall (not start firewall) Run firewall-cmd to configure the firewallCopy the code
Centos6 and below:
Firewall configuration file :/etc/iptables. This file is used to configure the firewall in detail. If no /etc/iptables file is available, you can use iptables save to generate this file
The iptables command configures the firewall
Service iptables start # Centos6 or later, run the service iptables stop command to start the firewall Service iptables restart # Centos6 or later, disable the firewall (note that the firewall is temporarily disabled, and will still be started after the next restartCopy the code
Environment variable configuration file
Bash_profile: indicates the configuration of user environment variables (valid for the current user). /etc/profile: indicates the configuration of user environment variables.
Su - username # The. Bash_profile environment variable configuration file in the user root directory will not be loaded when the user is switchedCopy the code
Network profile
Nic configuration file directory :/etc/sysconfig/network-scripts
Nic configuration files start with ifcfg-LO and do not need to be configured because ifcfg-LO is a local NIC
Vim /etc/sysconfig/network-scripts/ifcfg-enp0s3 BOOTPROTO=" DHCP "DEFROUTE="yes" # nic NAME NAME=" enp0S3" # NIC NAME NAME=" enp0S3" UUID=" deed3fd2-bd67-459B-8a49-ef0DD6e575A2 "DEVICE=" enP0S3" IPADDR=192.168.1.106 # NETMASTER=255.255.255.0 # GATEWAY=192.168.1.1 DNS1=192.168.1.1 DNS2=192.168.5.1Copy the code
After modifying the nic file, restart the network
Sudo command
Sudo command, which is used to call some root commands or modify some files for non-root users. Sudo command needs to be configured. The sudo configuration file is /etc/sudoers
[root@bow ~]# vim /etc/sudoers ## ## Allow root to run any commands anywhere root ALL=(ALL) ALL Bow ALL=(ALL) ALLCopy the code
Sudo command:
Sudo command
[root@bow ~]# sudo vim /etc/profile [bow@bow ~]$sudo vim /etc/profileCopy the code
The ping command
The function of the ping command to check network connectivity is the same as that on Windows
Ping IP address (0.0.0.100)Copy the code
The ifconfig command
The ifconfig command belongs to the net-tools software package. Net-tools must be installed before using the command
Net-tools installation:
Yum -y install net-tools ifconfig Check the IP addressCopy the code
The netstat command
The netstat command also belongs to the net-tools software package
Netstat tulp | grep 1521 # to check the oracle listener program whether normal bootCopy the code
The RPM command
RPM is a Linux installation command used to install an installation package in. RPM format
RPM - the ivh. The RPM file path qa # # said installation package RPM - see the RPM - installed software qa | grep mysql # check is already installed mysql package RPM -e -- nodeps installation package # uninstall packages - e said unloading Nodeps represents ignored dependenciesCopy the code
This document is shared with huawei Cloud community Linux Common Commands. The author of the original document is HW-Qgs.
Click to follow, the first time to learn about Huawei cloud fresh technology ~