Common linux commands basic part


Introduction to Linux

Linux is an open source operating system, free of charge, open source, safe, efficient, processing high concurrency, very strong, many enterprise development projects are deployed on Linux / Unix.
Founder: Linus Torvalds Linus
Linux mascot: Penguin — Tux
Linux major distributions:
CentOS, RedHat, Ubuntu, SUSE, red flag Linux
The main operating system at present
Windows, Android, vehicle system, MacOS, IOS, Linux, UNIX
Three forms of virtual machine network connection
Bridge Street mode
Benefit: Linux can communicate with other systems
Disadvantages: may cause IP address conflict
Nat mode
Network address translation mode: Linux can access the Internet without IP address conflict
Host only mode
Your virtual machine is an independent host and cannot access the Internet
End use and networking
Right click to open
Linux directory structure
/Root directory
/Opt some large software installation directory, or some service program installation directory
/Usr / local is the directory where the software is installed, which is equivalent to win’s program files
/Etc stores all the configuration files and subdirectories needed by system management
/Dev manages external devices (hardware mapped to files)
/The most basic dynamic connection shared library of Lib system is similar to winddl file
/Lost + found is generally empty. Some files will appear after illegal shutdown
/Media automatically identifies some devices, such as USB flash disk, and then mounts the device to this directory
/MNT users mount other file systems, mount the CD-ROM, and you can view the contents of the CD-ROM files on / MNT /
/Proc virtual directory, system memory mapping, access to this directory to obtain system information
/SBIN system administrator’s system management program
/Data needed to restart after SRV server starts
/The kernel of sys 2.6 has changed a lot. The new file system sysfs is installed in the kernel of 2.6
/TMP temporary file
/Application program used by users of usr / bin system
/Usr / SBIN super user number using more advanced management procedures and system daemons
The default directory of usr / SRC kernel source code
/Var stores things that are constantly expanding. It is used to put the frequently modified directory here, including various log files
/Common instructions of bin
/Home user home directory (each user has its own directory, usually named after the user account)
/The home directory of the root system administrator super privilege user
/Boot starts the core files of Linux, including some connection files and image files
/Run temporary file system, which stores the information since the system was started. It is deleted or cleared when the system is restarted

Clone of virtual machine
If you have installed a Linux operating system, if you need more, you don’t need to re install it. You just need to clone the existing one.

Method 1: directly copy the installed virtual machine file
Second, use the clone operation of vmvare. Note that when cloning, you need to shut down the Linux operating system first
virtual machine snapshot
If you want to return to the original state when using the virtual machine system (such as Linux), that is to say, you are worried about the system exception caused by some misoperation and need to return to the original state of normal operation. Vmvare also provides such a function, which is called snapshot management.
Install vmtools
After installing vmtools, we can better manage VM virtual machine under windows
You can set the shared folder of windows and CentOS
Installation process:
Enter CentOS
Click Install vmware tools in VM menu
CentOS will display a VM installation package, XX. Tar. GZ
Copy to / opt
Use the decompression command tar to get an installation file
cd /opt/
tar -zxvf xx.tar.gz
Enter the VM decompression directory and / opt directory
cd vm………/
Install. /
All use the default settings, you can install successfully
Note: installing vmtools requires GCC
VI and VIM
VI built in text editor
VIM has the function of program editing and can be regarded as an enhanced version of vi
Three common patterns
Normal mode (common mode, general mode)
The first mock exam is to open a file with VIM, and directly enter this mode. In this mode, you can use shortcut keys, you can use the buttons up and down to move cursors, delete characters and delete rows to handle files, or copy and paste data to handle files.
Insert mode (edit mode)
Press I, I, O, O, a, a, R, R to enter. In this mode, you can edit the content
Press: enter command line mode
Command line mode
Provide your related instructions, complete the read, save, replace, leave, VIM, display line number and other actions
First, press ESC to return to normal mode. Enter: to enter command line mode
Enter WQ, save and exit
Q exit
q! Force exit without saving

Shortcut exercises:

Copy the current line YY (in normal mode); Copy the current line down 5 lines 5yy; Paste (enter P);
Delete the current line DD; Delete the current line down 5 lines 5dd;
Find a word in the file [enter / keyword in command mode, enter to find, enter n to find the next one]
Set file line number: [input: set Nu and: set nonu in command mode]
Navigate to the first and last lines of the file [first line gglast line g]
Enter “hello” in the file. To undo this action, enter u in general mode;
Move the cursor to line 10, enter 10, and then press Shift + G

Disk partition mount

1. For Linux, no matter how many partitions are allocated to which directory, it has only one root directory and an independent and unique file structure in the final analysis. Each partition in Linux is used to form a part of the whole file system.
2. Linux adopts a processing method called “loading”. Its whole file system contains a whole set of files and directories, and connects a partition with a directory. At this time, a partition to be loaded will make its storage space available in a directory.
(1) MBR partition:
① Up to four primary partitions are supported
② The system can only be installed on the primary partition
③ The extended partition should occupy one primary partition
④ MBR only supports 2TB at most, but has the best compatibility
(2) GTP partition:
① Support unlimited number of primary partitions (but the operating system may limit, for example, 128 partitions under Windows)
② Maximum capacity of 18eb (1eb = 1024 Pb, 1PB = 1024 TB)
③ Support GTP after 64 bit in Windows 7
1. Linux hard disk is divided into IDE hard disk and SCSI hard disk.
2. For IDE hard disk, the drive identifier is “HDX ~”, where “HD” indicates the type of device where the partition is located. Here, it refers to IDE hard disk“ “X” is the disk number (a is the basic disk, B is the basic slave disk, C is the auxiliary master disk, D is the auxiliary slave disk), “~” represents the partition. The first four partitions are represented by numbers 1 to 4. They are the master partition or the extended partition. Starting from 5, they are logical partitions. For example, hda3 represents the third primary partition or extended partition on the first IDE hard disk, and hdb2 represents the second primary partition or extended partition on the second IDE hard disk.
3. For the SCSI hard disk, it is marked as “SDX ~”, the SCSI hard disk uses “SD” to indicate the type of the device where the partition is located, and the rest is the same as the IDE hard disk.
To view the disk status, click
fdisk -l

Linux partition
1. To view the partition and mount of the system, click
Lsblk or lsblk – F

2. How to add a hard disk

  1. Virtual machine add hard disk

2) Zoning
Partition command
fdisk /dev/sdb

Start partitioning / SDB
· m display command list
· P shows that the disk partition is the same as fdisk – L
New partition
· D delete partition
· w write and exit Description: after the partition starts, enter n, add a new partition, and then select P, the partition type is the main partition. Two carriage returns default to the remaining space. Finally, enter w to write the partition and exit. If not, enter Q to exit.

  1. format
    Format partition command:
    mkfs -t ext4 /dev/sdb1
    Where ext4 is the partition type

  2. mount
    Mount: associate a partition with a directory,
    Mount device name mount directory
    For example: Mount / dev / sdb1 / newdisk
    · umount device name or mount directory
    For example: umount / dev / sdb1 or umount / newdisk

  3. Set to mount automatically (permanently)
    Modify / etc / fstab to implement mount. Mount – a will take effect immediately after adding. Mount with command line will be invalid after restart

Disk condition query
Basic grammar:
df -hl

2. Remove the mount of / dev / sdb1 under / home / newdisk
The first method: umount / dev / sdb1 / home / newdisk

Second way:
umount /home/newdisk

3. Query the disk occupancy of the specified directory
Basic grammar Du – H
-S specifies the directory size
-H with unit of measurement
-A includes documents
–Max depth = 1 subdirectory depth
-C. increase the aggregate value while listing the details

Count the number of files in the / home folder

ls -l /home | grep “^-” | wc -l

^Is the locator – starts with the file WC is the statistics

Count the number of directories in the / home folder

ls -l /home | grep “^d” | wc -l

^Is the locator d starts with the file “^ -” WC is the statistics

Statistics / home folder includes files in subfolders

ls -lR /home |grep “^-” | wc -l

R is a recursive scan

The statistics / home folder includes the directories in the subfolders

ls -lR /home |grep “^d” | wc -l

Displays the directory structure in tree form


At this time, we find that the tree instruction does not exist. We can install it through the yum instruction

yum install tree

At this point, we can use the tree command to display the directory structure

Shut down, restart, log off

Shut down and restart
Shutdown – h now
Shutdown – H 1 1 minute later
Shutdown – r now restart the computer now
Halt shutdown
Reboot now restart the computer
Sync synchronizes data from memory to disk

No matter restart the system or shut down the system, first execute the sync command to write the memory data to the disk
At present, commands such as shutdown / reboot / halt have been synced before shutdown, but it’s a long way to go
Login and logout
When logging in, try not to log in with the root account, because he is a system administrator, super user, with the maximum authority, to avoid operational errors. He can log in with an ordinary user, and then use the “Su user name” command to switch to the system administrator identity
Enter logout at the prompt to log off the user
The logout logout command is invalid at the graph runlevel and valid at runlevel 3

User management run level help instructions

Linux is a multi-user and multi task operating system. Any user who wants to use system resources must first apply to the system administrator for an account, and then enter the system as the account
Basic grammar

Add user useradd user name
Set password passwd user name
Input password
Confirm password
When a user is created, a home directory with the same name will be created automatically
Each user has his own home directory, which is in the / home folder by default
You can also specify the home directory for the newly created user through “useradd – D specify directory new user name”
Delete user userdel user name
Delete the user, but keep the home directory userdel user name
Delete user and home directory userdel – R user name
Query user information instruction ID user name
Switch user Su – user name
The password is not required to switch from high privilege users to low privilege users
View the user information of the first login to the system
User group
Similar to roles, the system can manage multiple users with common or same permissions
New group groupadd group name
Delete group groupdel group name
Add group useradd – G group name and user name directly when adding users
Modify the user’s group usermod – G group name user name
User and group related files
/Etc / passwd file
The user’s configuration file records all kinds of information of the user
Meaning of each line: user name: Password: user ID (uid): group ID (GID): annotative Description: home directory (home directory): login shell
/Etc / shadow file
Password profile
Meaning of each line: Login Name: encrypted password: last modified time: minimum time interval: maximum time interval: warning time: inactivity time: expiration time: Flag
/Etc / group file
The configuration file of the group, which records the information of the group contained in Linux
Meaning of each line: Group Name: Password: group ID: list of users in the group
Specify the run level
0: power off
1: Single user [retrieve lost password]
2: Multi user state has no network service
3: Multi user network service
4: The system is not used and reserved for users
5: Graphical interface
6: System restart
The common run levels are 3 and 5. You can also specify the default run level
Switch run level command: init run level (for example: init 3 switch to run level 3)
View the current running level: systemctl get default
Switch the default run level: systemctl set default the run level to switch
Help instructions
Get help information
Basic syntax: man [command or configuration file] (function description: get help information)
Case: view the help information of LS command man LS
Get help for built-in commands

File directory class

The options of the instruction can be used at the same time, such as LS – La or LS – al
PWD shows the current absolute path
Ls view the files in the current path
-A shows the current directory, so files and directories, including hidden ones
-L in the form of a list
CD absolute path or relative path (function description: switch to the specified directory)
Cd ~ or CD go back to your home directory
CD.. returns to the directory one level above the current directory
Example: the CD.. / folder name first goes back to the previous level, and then enters this folder
The MKDIR (path /) directory name is used to create the directory
-P is used to create a multi-level directory
Example: create the Wang directory under the home directory, and create the Jie directory under the Wang directory
mkdir /home/wang/jie -p
Rmdir directory name delete empty directory (if there is content in the directory, it cannot be deleted)
RM – RF (path /) directory name can delete the directory with content under the directory
Touch file name creates an empty file
For example, touch hello.txt creates an empty hello.txt file
Touch file 1 file 2… Create multiple empty files
CP to copy the file to the specified directory (copy the file to the specified directory (\ CP forced overlay))
-R recursive copy
Copy the / home / test / directory and all the files in the directory to / home / Z /
cp -r /home/test/ /home/z/
RM file or directory to delete (delete directory or file)
-R recursively delete the entire folder
-F force deletion without prompt
MV parameter 1 Parameter 2
When the second parameter type (target) in the MV command is file, implementrenameThe function of source file or directory. Remember that there can only be one source file or directory here, because if there are multiple, there will be naming conflicts.
When the second parameter type (target) of MV command is directory, it realizes the function of moving source file and directory. Here, there can be multiple source files or directories, and MV command will move the source file or directory to the target directory.
-B backup before covering
-F force coverage
-I ask when I cover
-U only when the original file is the latest update can the target file be overwritten

-T use this method when there are multiple original files. But the target file is in the front, and the original file is separated by spaces
Cat to view the file to view the file content
-N display line number
|Pagination display
Space page down
Enter next line
Q leave more immediately
CTRL + F scroll down one screen
CTRL + B returns to the previous screen
=Output the line number of the current line
: F output the file name and the line number of the current line
Less file name is more powerful than more. It is not to load the whole file and display it, but to load the content according to the display needs. It has higher efficiency for large file display
Turn to the next page
Page down
Page up
/Search the content of the string down; n: Find down n: find up
? String up search string content n: up search n: down search
Q leave less

Output redirection > > append
Create without target file
LS – L > the contents of the file list are written to the file (overlay writing)
LS – L > > the contents of the file list are appended to the end of the file
Cat file 1 > file 2 overlays the contents of 1 to 2
Echo “content” > > file appends the content in “” to the file (> when overridden)
Cal > > file append calendar to file (> overlay)
Echo output content to console
Echo $path output environment variable
Echo “content” output content
The head file displays the content at the beginning of the file. The first ten lines of the file are displayed by default
-N 5 shows the first five lines. 5 can be any line before any number shows
The tail file displays the tail content of the copybook, and the last ten lines are displayed by default
-N 5 shows the last five lines. 5 can show any number of the last five lines
-F track the document in real time, so update it (often used)
Ln soft link
Ln – s soft link name of the directory to be linked (the path has not changed, but the content points to the directory to be linked)
RM – RF soft connection name delete the soft connection (do not add /) after the soft connection name)
When using PWD to view, the directory of the soft connection still exists
View the instructions that have been executed
History n shows the N instructions that have been executed recently
Input after history! N execute the instruction with history number n once
Example: history 3 shows the three instructions that have been executed recently

Search find class

find [ Search scope (path)] [options]
-Name searches by the specified file name
Find / home / – name hello.txt
-Query by user
Find / etc / – user perwrj
-Size find by size
Find / home / – Size + 20m
Locate files to search
Locate is different from find: find is to find the hard disk, and locate is only found in / var / lib / slocate database.
The speed of locate is faster than find. It does not really look up, but looks up the database. Generally, the file database is in / var / lib / slocate / slocate.db, so the search of locate is not real-time, but is subject to the update of the database. Generally, it is maintained by the system itself, or the database can be upgraded manually. The command is: updatedb
First, update DB is executed to create the locate database (by default, update DB is executed once a day.)
Locate passwd
Locate / etc / Sh
Locate – I ~ / R (ignore case and search all files beginning with R in the current user directory)
Pipe character “|” (it means to pass the processing result output of the previous command to the subsequent command for processing)
Grep filter search (find the qualified string in the file)
Grep command is used to find the file whose content contains the specified template style. If the content of a file conforms to the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or if the given file name is – the grep instruction reads data from the standard input device.
Grep test * file (in the current directory, find the file containing the test string in the file with the suffix “file” and print out the line of the string.)
Grep – R update / etc / ACPI (find all files in the specified directory / etc / ACPI and its subdirectories (if any subdirectories exist) that contain the string “update”, and print out the contents of the line where the string is located)
grep -v test test(reverse lookup. The previous examples are to find and print the qualified lines, and the contents of the unqualified lines can be printed through the “- V” parameter. Find the file with test in the file name and the line without test in the file name)
-A or — Text: don’t ignore binary data.
-A or — after context =: in addition to the column conforming to the template style, the content after the row is displayed.
-B or — byte offset: indicates the number of the first character of the line before displaying the line that conforms to the style.
-B or — before context =: in addition to the line that conforms to the style, the content before the line is displayed.
-C or — count: calculates the number of columns that conform to the style.
-C or — context = or -: displays the contents before and after the line in addition to the line that conforms to the style.
-D or — directories =: you must use this parameter when you specify that you want to find a directory instead of a file, otherwise the grep instruction will return information and stop the action.
-E or — regexp =: Specifies the string as the style to find the contents of the file.
-E or — extended regexp: use the style as an extended regular expression.
-F or — file =: specify a rule file whose content contains one or more rule styles. Let grep find the file content that meets the rule conditions. The format is one rule style per line.
-F or — Fixed regexp: a list that treats styles as fixed strings.
-G or — Basic regexp: use styles as normal representations.
-H or — no file name: before displaying the line conforming to the style, the file name of the line is not indicated.
-H or — with file name: before displaying the line conforming to the style, it indicates the name of the file to which the line belongs.
-I or — ignore case: ignore the difference between case and case.
-L or — file with matches: lists the file names whose contents conform to the specified style.
-L or — files without match: lists the file names whose contents do not conform to the specified style.
-N or — line number: indicates the column number of the row before displaying the row that conforms to the style.
-O or — only matching: only the matching pattern part is displayed.
-Q or — quiet or — silent: no information is displayed.
-R or — recursive: this parameter has the same effect as the “- D recursive” parameter.
-S or — no messages: no error messages are displayed.
-V or — invert match: displays all lines that do not contain matching text.
-V or — version: displays version information.
-W or — word regexp: displays only columns that match the whole word.
-X — line regexp: displays only the columns that match the whole column.
-Y: this parameter has the same effect as the “- I” parameter.

Decompress compression class

Gzip file name (the compressed file can only be compressed into *. GZ file) (the source file will not be retained after compression)
Gunzip file.gz (unzip file command)
Zip [option] file or directory to be compressed (command to compress files and directories)
-R recursive compression, that is, compression directory
Unzip [options] (unzip file)
-D the directory to be decompressed (specify the directory to store the compressed files)
Tar instruction package instruction (the last package file is. Tar. GZ file)
Tar [option] xxx.tar.gz package content (package directory, compressed file format is. Tar. GZ)
To pack and compress multiple files, separate them with spaces

-C specifies the directory to extract (tar – zxvf file to extract – C directory to extract) (the directory to extract must exist)
-C generate. Tar package file
-V display details
-F specifies the compressed file name
-Z package and compress at the same time
-X decompress the. Tar file

Time date class

Date displays the current time
Date +% y displays the current year
Date +% m shows the current month
Date +% d shows the current day
Date “+% Y -% m -% d% H% m% s” displays the date, time, minute and second
Date – s “time” (such as “2020-11-11:22:11”) sets the current time
Cal displays the calendar of this month
Cal year shows the calendar of a year for the whole year

Group and rights management

In Linux, each user must belong to a group, not independent of the group. In Linux, each file has the concept of owner, group and other groups.
View file owners
ls -ahl
Modify file owner
Chown user name file name
Group creation
Groupadd group name
Create users and put them into a group
Useradd – G group name user name
After the user creates the file directory class instruction. Note, the file group is the user group
Modify file group
Chgrp group name file name
Other groups
Except for the owner of the file and the user of the group, other users of the system are all other groups of the file
Change user’s group
Usermod – G group name user name
Usermod – D directory name user name
View user information
ID user name
-Rw-r — R –. 1 root 6 March 18 19:24 ok.txt
Type of file (1st)

  • General document
    D contents
    L soft link
    C character device (mouse, keyboard, etc.)
    B block file, hard disk
    Permissions owned by the file owner (234Th):
    Permissions owned by the user in the file group (567th)
    Permissions owned by users in other groups of files (8910th)
    Apply to file
  • No permission
    R read, view
    W write, can be modified, not necessarily deleted (deletion must have permission to write to the directory where the file is located)
    X execution
    Apply to directory
  • No permission
    R can read, LS can view the contents of the directory
    W writable, modifiable, create + delete + rename directory in directory
    X executable, you can enter the directory
    Permissions can be expressed in numbers (r = 4, w = 2, x = 1, so RWX = 4 + 2 + 1 = 7)
    (if it is a file, it means the number of hard links; if it is a directory, it means the number of subdirectories of the directory.)
    The first root
    File owner
    The second root
    File group
    The size of the file is 6 bytes (4096 for directory)
    March 18 19:24
    Last modification time of file
    file name
    Management of authority
    Through the Chmod command, you can modify the permissions of a file or directory
    The first method: +, -, = change permission
    u: Owner G: all groups o: others a: all (sum of u, G, O)
    Chmod u = RWX, g = Rx, o = x file name
    Chmod 0 + W file name
    Chmod A-X file name
    The second way is to change the authority through the number
    r=4 w=2 x=1 rwx =4+2+1=7
    The file name of Chmod u = RWX, g = Rx, o = x is equivalent to the file name of Chmod 751
    Modify file owner
    Chmod new owner file name
    Chmod new owner: new group file name
    -R if it is a directory, make all its sub files and directories recursively effective (all direct and indirect)
    Modify file group
    Chgrp new group file name
    -R if it is a directory, make all its sub files and directories recursively effective (all direct and indirect)

task scheduling

Crond task scheduling
Crontab sets the timing task
Task scheduling: refers to the system at a certain time to execute a specific command or program
Task scheduling classification:
1. System work: some specific work needs to be carried out over and over again. Such as virus scanning
2. Individual user work: individual users may want to execute some programs, such as MySQL database backup.
Basic grammar
Crontab [options]
Common options:
-E edit crontab timing task
-L query crontab task
-R delete all crontab tasks of the current user
Quick start:
Set task scheduling file / etc / crontab
Set up personal task scheduling. Execute the crontab – e command.
-R terminate task scheduling
-List the tasks currently scheduled
Service crond restart
Then input the task to the scheduling file.
as/1ls -l /etc/>/tmp/to.txt
It means to execute the LS – L / etc / > / TMP / to. TXT command every minute of every hour
Placeholder Description:
: minutes of the hour range: 0-59
the second
: hours of the day 0-23
: day of the month 1-31
Fourth: month 1-12 of the year
: day of the week 0-7 (both 0 and 7 represent Sunday)
1. If it’s just a simple task, you can directly add a task to crontab without writing a script.
2. For more complex tasks, you need to write scripts (shell programming)
Special symbol description:
*It’s all the time
, stands for discontinuous time. For example, the command “0, 8, 12, 16” is executed at 8:00, 12:00 and 16:00 every day
-Represents continuous time. ” 0 5 * * 1-6 “stands for 5:00 from Monday to Saturday
/N stands for how often. “For example”/Every 10 minutes