Linux common commands

Posted by TFD3 on Sat, 19 Feb 2022 21:28:25 +0100


1. Document processing command

1.1 # directory processing

1.2 document processing

1.3 link handling

2. Permission management command

2.1 change authority

2.2 # change of ownership

3. File search command

4. Help command

5. User management commands

6. Compression and decompression command

7. Network command

8. Shutdown and restart command

1. Document processing command

1.1 # directory processing

ls -a view hidden files
ls -l Long display,Displays file or directory details, including size and modification time
ls -d display contents
ls -h More user-friendly display, such as byte conversion to MB,GB You don't have to do it yourself
ls -i Query file inode number(inode Store file details) Information number, used to locate documents in the system


There are three types of users

  • u: Owner (only one)
  • g: Group
  • o: Others


  • -: binary
  • d: Catalogue
  • l: Soft link file


  • r stands for readable
  • w means you can write
  • x stands for executable
mkdir Only secondary directories can be created when creating directories
mkdir -p You can recursively create a new secondary directory without a primary directory
rmdir Deleting an empty directory can only delete an empty directory without files (not often used)

pwd Displays the current path

cd . . Return to the previous directory

cp Copy file cp [Original file path][Path to copy to] You can change the name by writing the name. If you don't write the name, use the name of the source file
cp -r duplicate catalog
cp -p Keep the original file attribute copy directory, such as the copy of the creation time log file
cp -rp Copy the directory and keep the attributes of the original file

mv Cut and rename (rename under the same directory)

rm Delete file
rm -f Forced deletion no longer ask
rm -r Deleting a directory is usually rm -rf Continuous use
control+C Exit options
 be careful:
rm -rf Be careful when using!!!
rm -rf /* The system will be deleted directly!!!

1.2 document processing

touch[Absolute path+file name].  Indicates the absolute path where the file is created

cat [file name] The display of file content is only suitable for short file content
cat -n [file name] Display the line number to label each line of the file content
    -A [file name] Show hidden characters
tac [file name] Displaying file contents in reverse order is not supported-n

more [file name] Display the file contents page by page. It is suitable for long file contents or spaces f Turn page and enter( Enter)Line by line q perhaps Q ,sign out

less [file name] The contents of the file are displayed page by page, but this command can turn back the page and view the contents of the turned file page up Page up,↑The arrow turns up one line, other operations and more Same command. stay less In the command, you can press/+You need to find the keyword query content of the file content. The highlighted content is the content containing the keyword. Press n(express next)You can view the next content with keywords

head -n Number of rows [file name] No default 10 rows specified
tail -n Number of rows [file name] No default 10 rows specified 
tail -f Dynamically display the contents at the end of the file (see the log file)

1.3 link handling

ln -s [Original document] [Target file] Create soft link
ln [Original document] [Target file] Create hard link

Soft link:
The function of soft link is similar to that of shortcut in Windows. It is just a symbolic link to the installation path of the source file, so its size is also very small, and its file type is lrwxrwxrwx. It seems that all three users can have all permissions. In fact, the real permission is determined by the source file, so the display in this is also one of the characteristics of soft links.

Hard link:
Hard link is to copy the source file to the target location, which is equivalent to cp -p + synchronous update. If the source file changes, the hard link file will also change at the same time, but if the source file is lost or deleted, the hard link will not disappear. It can be distinguished by the i node. The i node of the source file and the hard link file is the same, so they will update synchronously, but they cannot place hard links across partitions and cannot use directories.

2. Permission management command

2.1 change authority

Only the owner and root can change the file permissions

characterAuthorityMeaning of documentsMeaning of directory
rRead permissionYou can view the contents of the file cat/more/head/tail/lessYou can use the ls directory


Write permissionYou can modify the contents of the file cat/more/head/tail/less

You can create and delete files in the directory

You can touch/mkdir/rmdir/rm

xExecution AuthorityExecutable fileYou can enter the cd directory (rx in the directory is almost the same)
Rights management command chmod

1)chmod [{u,g,o,a}{+,-,=}{r,w,x}] [File or directory]
u : owner
g : Group
o : someone else
a : All users
 For example:
chomd g+x,o+r /tmp/testfile
chomd a=rwx /tmp/testfile

2)chmod [rwx And,rwx And,rwx And]] [File or directory]
    -R Recursive modification

The number of permission bits indicates: r=4 w=2 x=1
 For example:
rwxrw-r- - Its authority bit number is 764

2.2 # change of ownership

Only root can change the owner

Change file and directory owners
chown [owner] [File or directory]

Change the group to which files and directories belong
Groupadd [All group names] add group
chgrp [All groups][File or directory]
The default group is the default group of the owner who created the file

Default permissions for creating files and directories:
umask -S with rwx Display the default permissions of new files as
umask 0022 will be displayed after the command is entered directly

Where 0 represents special permission
022 Represents a logical relationship between 777 and 022
 He will carry out logical comparison. If the two are repeated, they cannot be retained. Write down what is not

777 rwx rwx rwx
022 --- -w- -w-
755 rwx r-x r-x  As the permission of the directory, the file will be automatically removed x

have access to umask 077 Modify the default create file permission, but it is not recommended.

In the Linux system, any newly created file will remove the executable permission. In fact, the umask value has not changed, just because it is a file. After the Trojan virus invades, it has no executable permission, so it has no effect.

3. File search command

Try to plan the directory structure and reduce search operations. Second, you cannot use the search command during peak hours in the service area. Too much memory resources, the smaller the search range, the better the path, and the more accurate the search conditions, the better

File search:
find [Search scope][Matching conditions] 
find [Search scope] -name [keyword] Search under a specific path, match any character and wildcard*
find [Search scope] -name [keyword]??? This searches for keywords with three characters after them
find [Search scope] -iname [keyword] Case insensitive search

find [Search scope] -size [data block] According to the file size, the data block is Linux Minimum unit of storage file
+Means to find files larger than this size,-Indicates less than, and not plus indicates equal to(Not commonly used)
1 Data blocks=512 byte=0.5K
size The subsequent data can only be data blocks and need to be converted. For example, you need to find more than 100 MB Documents
find /etc -size +204800

find [Search scope] -user [Owner name] Find by owner
find [Search scope] -group [Group name] Find by group

find [Search scope] -amin [time] Find the files and directories accessed within the time a: access
find [Search scope] -cmin [time] Find files and directories whose attributes have been modified within the time c: change
find [Search scope] -mmin [time] Find files and directories with modified contents within the time m: modift
+: Over how long -: How long

Connection options:
-a: Both conditions are met at the same time
-o: Either of the two conditions can be satisfied
 give an example:
find [Search scope] -size [data block] -a -size [data block]
find /etc -size +163840 -a -size -204800
 stay/etc Lower search greater than 80 MB Less than 100 MB Documents

Search for files with file name as keyword under specific path:
find [Search scope] -name [keyword] -a -type f
-type Find according to different types
f: file
d: catalogue
l: Soft link

-exec: Find the file and perform various operations on it {} \ ;Can't lose
-ok: It is used to ask for confirmation. It is usually used when deleting

Once the file is found and viewed ls Operation.
find [Search scope] -name [keyword] -exec ls -l {} \ ;
delete yangyang The user's files will be asked one by one if you are sure to delete them
find -user yangyang -ok rm {} \ ;

find [Search scope] -inum [i Node value] according to i Node query

give an example:
Delete this i Node files are very convenient
find /etc -inum 12345 -exec rm {} \ ;
It can also be used to query the hard link of a file, because the hard link and the file must be in the same partition, and i Same node
find /etc inum 12345 -exec ls -l {} \ ;
Quick find files:
Searching in the document database is faster
locate [file name]
locate -i Case insensitive lookup
updatedb Update document database
 Note: if the file is stored in/tmp Under the temporary file storage locate The file database does not store temporary file contents

Find command:
which Find the command storage location, or view the command alias 
which rm Will show alias rm='rm -i'
whereis You can also find the absolute path of the command and the location of the help information document of the query command

Find in file:
grep List query strings or keywords in the file
grep -i Case insensitive lookup
grep -v Exclude specified string
 For example:
grep -v ^# /etc/inittab
 Find exclude#File information at the beginning of the comment line

4. Help command

man [Command name or configuration file name] 
If used CentOS Server, no man Command, you need to enter the following command to download:
sudo yum install man-pages
1)man Just write the name of the command or configuration file directly later. You can't add an absolute path. After entering, operate and more,perhaps less Same. What is the use of a command\-*See the function of options.
2)Get help on configuration files
man Just write the name of the file directly later. You can't add an absolute path.
The first step of the configuration file help document is to see what it is used for, and the second step is to see the format of the configuration file.

give an example:
man services,You can understand more /etc/services The configuration file in
 Configuration file format:
service-name | port/protocol | [aliases ...]
Service name | port/transport protocol | [alias]
man passwd He will find the help of the command and the help of the configuration file
 adopt whereis passwd Can be found
/usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
 among man1 Help on behalf of orders, man5 Help on behalf of profile
 So we can pass man 5 passed You can find the help of the configuration file directly

Short help:
whatis ls [command] Query the function of short commands
[command] - -help Find the main options for the command
apropos [Profile name] The role of querying short profiles
query shell Help information for built-in commands:
help [command]
Shell Is a command interpreter with built-in commands, such as cd,umask,They use which If they can't find the path of the command, they can't use it man command ,He will list all the built-in commands and can use them directly help

5. User management commands

useradd Add new user
passwd [New user name] Add a password to the new user

who View login user information
 Login user name in the first column
 Second column landing terminal tty Represents a local terminal pts Indicates the remote login terminal
 The third column landing time
 The fourth column logs in to the host IP If the address is not written, it indicates local login

w View login user details
 The first line shows
17:04:55 up 22 days, 21:32, | 2 users,| load average: 0.02, 0.02, 0.05
 The first represents the current time
 The second indicates that the system in the service area has not been restarted or shut down for continuous operation time, which measures the stability of the service area
 The third indicates how many users have logged in
 The fourth represents the load balancing index, which records the load of the system in the past one minute, five minutes and fifteen minutes respectively. The average load index is divided by three. The load of the system mainly refers to CPU And memory load. A large number indicates severe load
uptime The command can also display this data

The second line shows
 among IDLE Indicates how long the user is idle after logging in
PCPU Indicates the occupation of the operation performed by the user after logging in CPU Time CPU time
JCPU Indicates the accumulated occupied CPU time
WHAT Indicates the operation performed

6. Compression and decompression command

gzip [file name] Compressed files can only compress files, cannot compress directories, and do not retain the original files
gunzip [Compressed package name] decompression .gz file;gzip -d [Compressed package name] The same function, decompression.gz file

tar [option] [Compressed file name] [catalogue]
-c pack
-v Show details
-f specify a filename
-z Pack and compress

tar [option] [Compressed file name] [catalogue]
-x Unpack
-v Show details
-f Specify the name of the extracted file
-z decompression 

tar -cf
tar -xf

tar -zcfv
tar -zxfv
tar -cfv Japan.tar Japan
 Package directory Japan And with Japan.tar name
tar -zcfv Japan.tar.gz Japan
 Package and compress directories Japan with Japan.tar.gz name
tar -xfv Japan.tar Japan
 Unpacking directory Japan.tar And with Japan name
tar -zxfv Japan.tar.gz
 Extract and unpack the directory Japan.tar.gz with Japan name

(4).zip(windows and Linux General)
zip [option] [Compressed file or directory name] [File or directory] 
    -r Compressed directory
unzip [Compressed file] decompression zip file
 The original file will be retained and the compression ratio will be prompted deflated,No, gzip Large compression ratio, not commonly used

(5)bzip2 [option] [file name] Compressed file, which is generally used for large files

7. Network command

write <user name> To send a message to an online user, press Ctrl+D After saving, it can only be sent to online users w Query the user's online status and send it out if it is not online
wall [information] Send broadcast messages to all online users
mail <user name> Send email to offline users

ping [IP address] Test network connectivity
ping -c Specify the number of times to send

ifconfig The main function of viewing network card information is to query the current local computer IP address

last List the current and past user information logged in to the system
lastlog Check the last login time of a specific user
lastlog -u [UID] inspect UID Last login time of user

traceroute Displays the path from the packet to the host
yum install -y traceroute install
traceroute [Website address] You can check which node of the network has a problem

netstat [option] Display network related information
-t TCP Protocol: Transmission Control Protocol
-u UDP Protocol: user datagram
-l monitor
-r Routing: Gateway
-n display IP Address and port number
 give an example:
netstat -tlun : Query the listening port of this machine
netstat -an : View all network connections of this machine
netstat -rn : View native route (Gateway)

setup configure network redhat Proprietary command

8. Shutdown and restart command

shutdown [option] Time (recommended)
Specific time can be filled in the time option, such as: now Now shut down 20:30 Shut down at 8:30
-c: Cancel the previous shutdown command
-h: Shut down
-r: restart

shutdown -h now Shut down
shutdown -c Cancel the last set shutdown time

You need to be careful when restarting the server. You need to stop the service first, otherwise the physical memory will be damaged
 Moreover, the remote server can only be restarted. After shutdown, the administrator needs to start it manually

Other shutdown commands:
poweroff Equivalent to direct power failure
init 0
 Recommended use shutdown Shutdown will save the running service

Other restart commands:
init 6

Operation level of the system:
init 0-6
0: Shut down
1: Single user access option menu only root User login is equivalent to Windows safe mode F8,But there is no graphical interface
2: Incomplete multi-user, excluding NFS No graphical interface, no service NFS Network file system, Linux File transfer and sharing between, except NFS Service, same as 3.
3: Completely multi-user, no graphical interface
4: Not assigned, no graphical interface
5: Graphical interface
6: restart

runlevel Query system operation level
logout Exit login command

Note: in the server, you must log out after the operation is completed, otherwise others will directly enter your server, causing great losses. The most basic safety awareness must have!!!

Topics: Linux Operation & Maintenance CentOS Back-end server