Linux commonly used instructions
1. Directory
Root directory : /
path : 1. Absolute path: the path described from the root directory "/"
2. Relative path: the path described from the current path
tree:
Tree display directory structure
Command to install tree: sudo apt-get install tree
Directory structure:
/home stores the user directory, /home/user name user home directory
/root super user home directory
2. Directory related instruction operations
ls:
For a directory, this command lists all subdirectories and files in the directory. For files, the file name and other information will be listed.
(list) View file information
-l Display detailed information
-a Display all files, including hidden files or directories; with "." is hidden files
-R Recursively display the contents of subdirectories
pwd:
function: display the user's current directory
cd:
Syntax: cd directory name
Function: change the working directory. Change the current working directory to the specified directory.
cd …/: return to the parent directory
cd /home: absolute path
cd ~: enter the user's home
directory cd -: return to the most recently visited directory
which:
Format: The which command displays the directory where the corresponding command is located.
cd is not in any directory, but the shell comes with it
mkdir:
Function: Create a directory
Format: mkdir parameter directory name 1 directory name 2…
mkdir –p test/test1: create multiple directories recursively
rmdir:
Function: delete directory
1. rmdir cannot delete non-empty directories, only empty directories: directories containing only "." and "..."
rmdir -p directory name 1 / directory name 2 if directory name 2 is deleted, directory name 1 Is empty, then it will be deleted recursively
3. File-related instruction operations
touch:
Function: Create file
Format: touch File name 1 File name 2… If the file exists, modify the last modification time of the file
rm:
Function: delete files or directories
Format: rm file name
rm -r directory name delete directory recursively
rm-f force delete
cp:
Function: (copy) copy files or directories
cp srcpath despath If despath is a directory, copy srcpath to the despath directory, if not, create a directory in the upper level of despath, copy srcpath
cp -r test1/test2/ test3/ Recursively copy the directory, copy test2 under test1 to the test3 directory
mv
Function: (move) moving files or directories is equivalent to cut + paste under windows
mv src des rename files or directories
mv src des (directory) move files or directories to other directories
4. File content operation instructions
cat:
Function: View the content of the file
Format: cat file name to display the content of the file to the terminal, if there is no file, read the standard input, and stop when a carriage return
cat -n gives the output content number of the file
more:
Format: more File name to view the file in split screen, blank space to display the next screen, press Enter to display the next part of the content
more -n Number all output lines
Press q to exit more
less:
Function: less is similar to more, but you can browse files at will with less, and more can only move forward but not backward, and less will not load the entire file before viewing.
Format: less file name is displayed on split screen, use direction Key control up and down
head:
Function: Used to display the beginning of the file to the standard output, the default head command prints the first 10 lines of the corresponding file
Format: head -n file name displays the file header n lines, if you do not add n, the default display 10 lines
tail:
tail -n file name displays the last n lines of the file, if n is not added, the default display line
tail -f file name to view the contents of the end of the file, do not exit, used to track the log
find:
Function: Used to search for qualified files in a specific directory
1. Query by file name: -name
format: find path-name file name
2. Query by file type: -type
format: find path-type type
grep:
File search, print all lines containing the pattern
Format: grep parameter'find content' file name
-v display all lines not including the search content
-n display matching lines and line numbers
-i ignore case
Compressed package management
zip:
Packing: zip Compressed name. zip File to be compressed—Compressed ordinary file
zip -r Compressed name.zip Directory to be compressed—Compressed folder
Unpacking: unzip Name of the package to be unzipped.zip
tar:
1. gz format
Compression: tar -zcvf compressed name. tar.gz file name to be compressed
Decompression: tar -zxvf file name to be decompressed. tar.gz
-c compressed file, -v displays information, -f specifies Compressed package name
5. File permissions and user attributes
-rw-rw-r-- 1 root root 900 Jan 21 18:36 file.txt
**1: ** -rw-rw-r-- file permission flag bit; r——readable, w——writable, x——executable, if the corresponding location has no permission, it will be indicated by "-"
Permission number representation (octal number): rw-rw-r-- ——>110 110 100——>664
File type :
d: folder
-: ordinary file
l: soft link (similar to Windows shortcut)
b: block device file (such as hard disk, CD-ROM, etc.)
p: pipe file
c: character device file (such as screen and other serial devices)
s: socket file**
The first rw- the permissions of the belonging user; the second rw- the permissions of the belonging group; the third r- the permissions of other users
2: One root is the owner of the file (user)
3: The second root belongs to the user group (group)
4: 900 size
whoami:
Show who the user is
chmod:
Change file permissions
Format: chomd (u/g/o/a)(+/-)(r/w/x) File name
u: user file owner, g: group belonging to group, o: others, a: all All
chomd (+/-)(r/w/x) File name
If user and group information is not added, the default is a, which means all
chomd octal number (0655) File name
Change file permissions by number
The directory needs to have executable permissions to enter the directory.
chown:
sudo chown user:group file name to
modify the owner and all groups of the file
sudo chown user file name
only modify the owner
chgrp:
sudo chgrp groupname file name to
modify the group to which the file belongs
6. User Management
Switch user:
su-switch to the root user, but will not switch the working directory
su-——Switch to root user and switch the working directory to /root
Create user:
Format: useradd option username
-s specifies the shell, -g specifies the group, -d specifies the user's home directory, -m home directory is not automatically created
delete users:
Format: userdel -r username-delete the user and delete the user's home directory at the same time
modify:
Format: usermod option username
is basically the same as the useradd command, -s specifies the shell, -g specifies the group, -d specifies the user's home directory, and -l specifies the user name
set password:
Format: passwd username
If no user is specified, the current user's password will be changed by default
User group:
switch to the root user to operate, or add sudo before the command
Add
format: groupadd user group
Delete
format: groupdel user group