1, Basic format of command
command prompt
[root@localhost~]#
Of which:
root | Current login user |
---|---|
localhost | host name |
~ | Current directory |
# | Prompt for superuser |
$ | Prompt for ordinary users |
Command format
command [option] [parameter]
==Note: = = individual commands do not follow this format. When there are multiple options, they can be written together to simplify options and complete options
-a equals – all
file type
-rw-r--r--. 1 root root 1648 5 October 18:11 initial-setup-ks.cfg -File type(- file d catalogue l (soft link file) rw- r-- r-- u owner g Group o someone else r read w write x implement linux Each of the three permissions is a group 1 Represents the first reference count root Second for owner root For the group to which it belongs, 1648 is the size, followed by the last modification time and file name
There is no suffix or software type in Linux. There is only one system type
The file type is identified by the first bit of permission bit, and every three bits are a group
Query contents in directory: ls
ls [option] [File or directory] Options: -a Show all files, including hidden files -l Show details -d View directory properties -h Humanized display file size -i display inode
Each file has its own id, which can be viewed through - i
You can view file permissions through - d
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-tpmkb0em-1652447802230) (C: \ users \ 57322 \ appdata \ roaming \ typora user images \ image-20220510193809565. PNG)]
2, File processing command
Directory processing command and file processing command
Create directory: mkdir
mkdir -p [Directory name] -p Recursive creation Create a list of directories Original English meaning of the order: make directories
Switch Directory: cd
cd [catalogue] Original English meaning of the order: change directory Simplify operation cd ~ Home directory of the current user in the machine cd cd - Enter last directory cd .. Enter the upper level directory cd . Enter current directory
- **Relative path: * * Find by referencing the current directory
For example:[root@imooc~]# cd ../usr/locaa/src/
- **Absolute path: * * specify from the root directory, and search recursively one level at a time. In any directory, you can enter the specified location
For example:[root@imooc~]# cd /etc/
You can press tab twice to complete automatically, identify directory completion, or command completion
Delete empty directory: rmdir
rmdir[Directory name] Original English meaning of the order: remove rmpty directories Only empty files can be deleted
Delete file or directory: rm
rm -rf [File or directory] Original English meaning of the order: remove Options: -r Delete directory -f force Never execute rm -rf / It will crash the system and delete it directly Linux New operating system
Copy command: cp
cp [option] [Source file or directory] [Target directory] Original English meaning of the order: copy Options: -r duplicate catalog -p Copy file attributes -d If the source file is a linked file, copy the linked attributes -a amount to -pdr ll amount to ls -l
Cut or rename command: mv
mv [Source file or directory] [Target directory] Original English meaning of the order: move
If there is no target directory, the default is to modify the source file name. If there is, it is the rename command
Common directory functions
/root directory /bin Command save directory (commands that can be read by ordinary users) /boot Startup directory, startup related files /dev Device file saving directory /etc Profile save directory /home Home directory of ordinary users /lib System library save directory /mnt System mount directory /media Mount directory /root Super user's home directory /tmp Temporary directory /sbin Command save directory (a directory that can only be used by super users) /proc Direct write to memory /sys /usr System software resource directory /var System related documents
bin
Bin and sbin under the root directory and bin and sbin under usr are used to save system commands. Under the bin directory, any user can execute, while the commands under sbin can only be executed by super users
dev special file
etc configuration file
Home home directory of ordinary users
root super user home directory
lib function library, function library
misc media mnt mount directory
The proc and sys directories cannot be operated directly. These two directories store the overload point of memory. They are written directly to memory and cannot be operated
Files can be placed in the home directory root or home, and in the tmp directory. Others are not recommended
Link command: ln
ln -s [Original document] [Target file] Original English meaning of the order: link Function Description: generate linked file Options: -s Create soft link
- Hard link features:
- Having the same i node and storage block can be regarded as the same file
- Can be identified by i node
- Cannot cross partition
- Cannot be used against a directory
If the user needs to check a file, first go to a file index table to find the corresponding file, and then take it out and return it to the user
Only files can create hard links
A hard link is equivalent to two different access points to a file
- Soft connection features:
- Similar to Windows shortcuts
- Soft link has its own I node and Block, but only the file name and I node number of the original file are saved in the data Block, and there is no actual file data
- lrwxrwxrwx l soft connection soft connection file permissions are lrwxrwxrwx
- Modify any file and change the other
- Delete source file, soft link cannot be used
Soft link is equivalent to a shortcut. When searching through soft link, users first find the id of the soft link in the file index, find a file that stores all the IDs of the corresponding file through the id, and then find it in the index through the file
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ph8bq08u-1652447802231) (C: \ users \ 57322 \ appdata \ roaming \ typora user images \ image-20220511082605981. PNG)]
[the external chain image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-dyvzwjqy-1652447802231) (C: \ users \ 57322 \ appdata \ roaming \ typora user images \ image-20220511090342063. PNG)]
Soft links must be written from the root directory
3, File search command
File search command locate
locate file name Searching by file name in the background database is faster /var/lib/mlocate #The background database searched by the locate command updatedb Update database
The locate command will only search in the background database, so it is fast, but the database is not updated in real time. Its update cycle is one day. We can also use the updatedb command to force the update.
Disadvantages:
You can only search by file name
/etc/updatedb.conf configuration file
- PRUNE_BIND_MOUNTS = "yes"
#Turn on search restrictions
- PRUNEFS =
#File types not searched when searching
- PRUNEPATHS =
#When searching, do not search the path of
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-beoew9ky-1652447802232) (C: \ users \ 57322 \ appdata \ roaming \ typora \ typora user images \ image-20220511093110438. PNG)]
Just like the login filter, it does not search the specified path
Command search commands whereis and which
whereis Command name #Search the path of the command and the location of the help document Options: -b: Find executable -m: Find help files for which file name #Search the path and alias of the command
Not all commands can be found. For example, cd command can only find help documents. Some commands are Linux shell commands, which are necessary for user interaction and are built-in Linux.
PATH environment variable
PATH Environment variable: defines the path of the system search command [root@localhost~]#echo $PATH /usr/lib/qt-3.3/bin: /usr/local/sbin:/usr/local/bin:/sbin:/bin/usr/sbin:/usr/bin:/root/bin
File search command find
find [Search scope] [search criteria] #search for file find / -name install.log #Avoid large-scale search, which will consume system resources #find is to search the system for qualified file names. If you need to match, use wildcard matching, which is an exact match
Fuzzy query find by name
Wildcard fuzzy query in Linux
***** | Match any content |
---|---|
? | Match any character |
[] | Match any character in brackets |
[the external chain image transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ixvnnz7m-1652447802232) (C: \ users \ 57322 \ appdata \ roaming \ typora user images \ image-20220511102042192. PNG)]
find/root -iname install.log #Case insensitive find /root -user root #Search by owner find /root -nouser #Find files without owners
Linux is strictly case sensitive
Files without owners are generally garbage files
- The files in proc and sys are generated by the kernel. It is likely that the files without owner belong to normal files
- If you use u disk to transfer files from windows to Linux, there may be no owner
Find by time
find /var/log/ -mtime +10 #Find files modified 10 days ago -10 10 Documents modified within days 10 10 Documents modified on the same day +10 10 Files modified days ago atime File access time ctime change attributes of files mtime Modify file content
Find by file size
find . -size 25k #Find files with a file size of 25KB #there. That is, the current directory k is lowercase -25k Less than 25 KB Documents 25k Equal to 25 KB Documents +25k Greater than 25 KB Documents find . -inum 262422 #Find the file whose i node is 262422
Complex find query
find /etc -size +20k -a -size -50k #Look for files larger than 20KB and smaller than 50KB in the / etc / directory -a and Logic and, both conditions are met -o or Logical or, one of the two conditions can be satisfied find/etc -size +20k -a -size -50k -exec ls -lh {} \; #Find files larger than 20KB and smaller than 50KB in the / etc / directory and display details #-exec/-ok command {} \; Perform actions on search results
Generally, it is troublesome to execute after the query, so - exec ls -lh {} is added; For such a command, this is a standard format. As long as - exec is written, the following {} must be written; Execute the second statement in the middle
String search command grep
grep [option] String file name #Match matching strings in the file Options: -i ignore case -v Exclude specified string (reverse)
Search for the corresponding string from the file
All items will be listed as long as they are included
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (IMG uffhihku-1652447802233) (C: \ users \ 57322 \ appdata \ roaming \ typora user images \ image-20220511110107486. PNG)]
The difference between find command and grep command
- find command: search for qualified file names in the system. If you need to match, use wildcard matching. Wildcard matching is perfect.
- grep command: search for qualified strings in files. If matching is needed, use regular expression to match. Regular expression contains matching
Use find to search for files or file names, and grep to search for content
4, Help command
Help command man
man command #Get help for the specified command man ls #View help for ls
man's level
1 | View help for commands |
---|---|
2 | View help for functions that can be called by the kernel |
3 | View help for functions and function libraries |
4 | Help for viewing special files (mainly files in / dev directory) |
5 | View help for profiles |
6 | View game help |
7 | View help for other miscellaneous items |
8 | View help for commands available to your system administrator |
9 | View help and related kernel files |
See what level of help the command has
man -f command amount to whatis command give an example: man -5 passwd man -4 null man -8 ifconfig
View all help related to the command
man -k command amount to apropos command for example apropos passwd
Other help commands
Shell is equivalent to a shell of Linux. It is more basic than Linux and its own language
How to distinguish between self-contained commands and later added commands? Use whereis to find them. If you can find them, they are added later. If you can't find them, they are self-contained
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-0ipblr3n-1652447802234) (C: \ users \ 57322 \ appdata \ roaming \ typora user images \ image-20220511210441932. PNG)]
shell internal command help
help shell Internal command #You can only get help from commands inside the shell For example: whereis cd #Determines whether it is an internal shell command help cd #Get internal command help
Detailed command help info
info command -enter: Enter the sub help page (with*No. (mark) -u: Enter the upper page -n: Go to the next help summary -p: Go to the previous help summary -q: t
5, Compression and decompression commands
Common compression formats: zip .gz .bz2
Common compression formats: tar.gz .tar.bz2
Compression command 1
. zip format compression and decompression
Compression:
zip Compressed file name source file #Compressed file zip -r Compressed file name source directory #Compressed directory
Decompression:
unzip Compressed file #Decompress zip file
You can also unzip the zip file in Windows
. gz format compression and decompression
Compression:
gzip source file #Compress to gz format compressed file, the source file will disappear gzip -c source file > Compressed file #Compress to gz format, source file retention #For example: gzip -c cangls > cangls. gz #gzip doesn't support this kind of - c is to print out the file and add > is to promise the file to the corresponding file gzip -r catalogue #Compress all sub files in the directory, but you cannot compress the directory
If the directory is compressed, it cannot compress the directory, but only the files in this directory
Decompression:
gzip -d Compressed file #Unzip file gunzip Compressed file #Unzip file
. bz2 format compression and decompression
Compression:
bzip2 source file #Compress to bz2 format, do not keep the source file bzip2 -k source file #Keep the original file after compression be careful: bzip2 The command cannot compress a directory
Decompression:
bzip2 -d Compressed file #Decompress and -k keep the compressed file bunzip2 Compressed file #Decompress and -k keep the compressed file
Compression Command 2
Packaging command tar
tar -cvf Package file name source file option: -c: pack -v: Display process -f: Specify the packaged file name for example tar -cvf longzls.tar longzls
Unpacking:
tar -xvf Package file name Options: -x: Unpack for example -xvf longzls.tar
.tar.gz compression format
Actually tar.gz format is first packaged as Tar format, and then compressed into gz format
tar -zcvf Compressed package name.tar.gz source file Options: -z: Compress to.tar.gz format tar -zxvf Compressed package name.tar.gz Options: -x: decompression .tar.gz format
.tar.bz2 compression format
tar -jcvf Compress enrollment.tar.bz2 source file option: -z: Compress to.tar.bz2 format tar -jxvf Compressed package name.tar.bz2 Options: -x: decompression .tar.bz2 format -ztvf View without decompression
6, Shutdown and restart commands
shutdown command: [root@localhost~]#shutdown time Options: -c Cancel the previous shutdown command -h Shut down -r restart Other shutdown commands: [root@localhost~]#halt [root@localhost~]#poweroff [root@localhost~]#init 0 Other restart commands: [root@localhost~]#reboot [root@localhost~]#init 6 Log out: logout
System operation level:
0 | Shut down |
---|---|
1 | single user |
2 | Incomplete multi-user, without NFS service |
3 | Complete multi-user |
4 | Unassigned |
5 | Graphical interface |
6 | restart |
[root@localhost~]#runlevel Query the current level of the system [root@localhost~]#cat/etc/inittab #Modify the system default run level id:3:initdefault:
7, Other common commands
Mount command
When inserting a u disk, you need to assign a drive letter and execute it automatically in Windows, while in Linux, you need to execute commands
1. Query and auto mount
[root@localhost~]#mount #Query the mounted devices in the system [root@localhost~]#mount -a #Mount automatically according to the contents of the configuration file / etc/fstab
/etc/fstab
As long as you write this file, it will be mounted automatically
It is not recommended to write files to USB flash disk and mount them automatically. If you forget to insert USB flash disk one day, the system will crash
2. Mount command format
[root@localhost~]#mount [-t file system] [- o special options] device file name mount point Options: -t File system: add file system type to specify the type of mount. You can ext3,ext4,iso9660 Such file system -o Special options: you can specify additional options to hang on
Special options:
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-bmfel7v6-1652447802234) (C: \ users \ 57322 \ appdata \ roaming \ typora user images \ image-20220512105727571. PNG)]
Cannot be easily modified:
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-0mfppl7g-1652447802235) (C: \ users \ 57322 \ appdata \ roaming \ typora \ typora user images \ image-20220512105955557. PNG)]
[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ivyfurdn-1652447802235) (C: \ users \ 57322 \ appdata \ roaming \ typora user images \ image-20220512110113130. PNG)]
3. Mount CD
[root@localhost~]#mkdir/mnt/cdrom/ #Establish mount point [root@localhost~]#mount -t iso9660 /dev/cdrom/mnt/cdrom/ #Mount CD [root@localhost~]#mount /dev/sr0/mnt/cdrom
[the external link image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ykleuoif-1652447802236) (C: \ users \ 57322 \ appdata \ roaming \ typora user images \ image-20220512111836727. PNG)]
The -t iso9660 in the middle can be omitted
User login view and user interaction commands
View login user information
w user name Command output: USER:Login user name; TTY:Login terminal; FROM:From which IP Address login; LOGIN@:Landing time; IDLE:User idle time; JCPU:It refers to the time occupied by all processes connected to the terminal. This time does not include the past background job time, but includes the time occupied by the currently running background job; PCPU:It refers to the time occupied by the current process; WHAT:Currently running commands
Query current login and past login user information
last last Command default read/var/log/wtmp file data Command output -user name -login terminal -Sign in IP -Landing time -Exit time (online time)
View the last login time of all users
lastlog lastlog Command default read/var/log/lastlog Document content Command output -user name -Login terminal -Sign in IP -Last login time
-1652447802235)]
3. Mount CD
[root@localhost~]#mkdir/mnt/cdrom/ #Establish mount point [root@localhost~]#mount -t iso9660 /dev/cdrom/mnt/cdrom/ #Mount CD [root@localhost~]#mount /dev/sr0/mnt/cdrom
[external chain picture transferring... (img-ykleuoif-1652447802236)]
The -t iso9660 in the middle can be omitted
User login view and user interaction commands
View login user information
w user name Command output: USER:Login user name; TTY:Login terminal; FROM:From which IP Address login; LOGIN@:Landing time; IDLE:User idle time; JCPU:It refers to the time occupied by all processes connected to the terminal. This time does not include the past background job time, but includes the time occupied by the currently running background job; PCPU:It refers to the time occupied by the current process; WHAT:Currently running commands
Query current login and past login user information
last last Command default read/var/log/wtmp file data Command output -user name -login terminal -Sign in IP -Landing time -Exit time (online time)
View the last login time of all users
lastlog lastlog Command default read/var/log/lastlog Document content Command output -user name -Login terminal -Sign in IP -Last login time