Linux common commands

Posted by kiss the robot on Thu, 20 Jan 2022 20:23:13 +0100

Linux common commands

Linux text directory common commands

pwd displays the current directory location information

[root@Centos7-100 network-scripts]#:pwd
/etc/sysconfig/network-scripts #Displays the current location

cd switch directory

cd [option] [dir]
cd [option]    [catalogue]
-    #Switch to the directory path where the user was last located. For the game, it is to unlock a transfer point and then transfer it to the previous archive location.
~    #Switch to the current user's home directory. By default, no parameters are connected. Direct execution of cd will achieve the same effect. In terms of the game, it is to directly roll back to the city.
..   #Switch to the upper level directory of the current directory. For the game, it is to return to the previous map.

tree displays the directory structure

tree [option] [directory]
tree [option]    [catalogue] 

[root@Centos7-100 network-scripts]#:tree -L 1
.
├── ifcfg-eth0
├── ifcfg-lo
├── ifdown -> ../../../usr/sbin/ifdown
├── ifdown-bnep
├── ifdown-eth
.....
Important options
-d    #Show directory only 
-L N  #The maximum number of layers to traverse the directory. N is a positive integer greater than 0

mkdir create directory

mkdir [option] [directory] 
mkdir [option]    [catalogue]

-p  #1. Create a directory recursively. Recursion means that there are directories under the directory
	#2. When no parameters are added by default, an error will be reported if the directory to be created already exists. After using the - p parameter, no error will be prompted even if the directory to be created already exists.
-m	#Option is used to manually configure the permissions of the created directory instead of using the default permissions

[root@Centos7-100 test]#:mkdir -p /root/test/data/data2/data3
[root@Centos7-100 test]#:tree
.
└── data
    └── data2
        └── data3
        
[root@Centos7-100 test]#:mkdir -m 777 data2
[root@Centos7-100 test]#:ll
 Total consumption 0
drwxr-xr-x. 3 root root 19 4 September 19:58 data
drwxrwxrwx. 2 root root  6 4 September 20:00 data2

touch create file or change file timestamp

touch [option] [file] 
touch [option]    [file]
-a: or--time=atime or--time=access or--time=use  Change access time only;
-c: or--no-create  Do not establish any documents;
-d: <Time and date> Use the specified date and time instead of the current time;
-f: This parameter will be ignored, not processed, and is only responsible for solving BSD edition touch Compatibility of instructions;
-m: or--time=mtime or--time=modify  Only the change time;
-r: <Reference documents or directories>  Set the date and time of the specified document or directory to be the same as that of the reference document or directory;
-t: <Date time>  Use the specified date and time instead of the current time;

ls displays the contents and attribute information under the directory

ls [option] [file]
ls [option]    [File or directory]

-l  #List the file and directory information in detail 
-a  #Displays all files in the directory, including "." Hidden file at the beginning
-h  #Displays the file or directory size in human readable information 
-d  #Display the information of the directory itself, not the information of the files in the directory 
-r  #Reverse sort 
-t  #Sort according to the last modification time 
-i  #Display inode node information

cp copying files or directories

cp [option] [source] [dest]
cp [option]    [source file]  [Target file] 
Important options
-p  #Keep source files when copying files.
-d  #If the copied source file is a symbolic link, only the symbolic link itself is copied, and the target file or directory pointed to by the symbolic link is retained.
-r  #Recursively copy directories, that is, copy subdirectories and files at all levels under the directory 
-a  #It is equivalent to the function sum of the above three options P, D and r.
-i  #If the copied file already exists, prompt the user for confirmation before overwriting.
-t  #By default, the command format is "cp source file target file". Use the - t parameter to reverse the order and change the format to "cp -t target file source file".

mv move or rename files

mv [option] [source]  [dest]
mv [option]    [source file]   [Target file]
mv [option]... [-T] Source file destination file
mv [option]... source file... catalogue
mv [option]... -t Directory source file...

Important options

--backup[=CONTROL] : Create a backup for each existing destination file
-b : similar–backup Parameters are not accepted
-f, --force : Don't ask before overwriting
-i, --interactive : Ask before overwrite
-n, –nechoo-clobber : Do not overwrite existing files if you specify-i,-f,-n Only the last one takes effect.
--strip-trailing-slashes : Remove the slash at the end of each source file parameter
-S, --suffix=SUFFIX : Replace common backup file suffixes
-t, --target-directory=DIRECTORY : Move all the source files or directories specified by the parameters to the specified directory
-T,--no-target-directory : Treat the target file as a normal file
-u, --update : Move only if the source file is newer than the destination file, or if the destination file does not exist
-v, --verbose : Show the steps in detail

⚠ rm ⚠ Delete file or directory

Warning this is one of the most dangerous commands in Linux. Please use it with caution.

Syntax format

rm [option] [file]
rm [option]    [File or directory]
Important options:
-f  #Force deletion, ignore nonexistent files, and do not prompt for confirmation 
-r  #Recursively delete directories and their contents
-i  #Confirmation is required before deletion

echo command ---- the most basic command in linux

Basic usage of ehco command

echo  [OPTION]... [STRING]...

echo important options

echo -n		#Indicates output without wrapping
echo ""		#Output command specification
echo -e 	#Output escape character
echo —E 	#Cancel backslash ESC escape (default)
 -version display version information
 -help Show help

Common escape characters are as follows:

\b after escape, it is equivalent to pressing backspace, but only if there are characters after "\ b". Refer to the example below for specific effects.
\c does not wrap output. When there are no characters after "\ c", the function is equivalent to echo -n. refer to the following example for specific effects.
\n line feed. See the example for the effect.
\f line feed, but the beginning of the new line after line feed is connected to the end of the previous line. See the example for the specific effect;
\v is the same as \ f;
\After t turns, it means to insert tab, that is, tab, which has been exemplified above;
\r move the cursor to the beginning of the line without line break, which is equivalent to using the characters after "\ r" to cover the characters of the same length before "\ r". It may not be easy to understand if you only look at this text description. See the example for specific effects;
\Indicates to insert "\" itself;

cat: merge files & view file contents

Important options

-A: --show-all Equivalent to -vET
-b: --number-nonblank For non empty output lines, the line number is displayed in front of each line
-e: Equivalent to -vE
-E:  --show-ends Show at the end of each line $
-n: --number Number all output lines, that is, the line number displayed in front of each line
-s: --squeeze-blank Do not output multiple blank lines
-t: And -vT equivalence
-T: --show-tabs Display skip characters as ^I
-u : (Ignored)
-v:  --show-nonprinting use ^ and M- References, except LFD and TAB outside
--help Show this help message and leave

Switch machine

1. Shutdown command

shutdown -h now 
shutdown -h +1 
halt 
init0
poweroff

2. Restart command

reboot
shutdown -r now
shutdown -r +1
init 6

3. Logout order

logot
exit

VIM common commands

Normal mode

hjkl 		#Up, down, left and right
gg			#Move to first row
G			#Move to last line
0			#Move to the beginning of the line
$			#Move to end of line
w			#Move by word
yy			#copy
nyy			#Copy N rows
p			#Copy under cursor
u			#Undo last action
ctrl + r	#Previous step
r			#replace
dd			#Delete cut
ndd			#Delete N-line cut
dG			#Delete the line where the cursor is located to all the previous lines
dgg			#Delete the line where the cursor is located to all the previous lines
d$			#Delete all contents from the line where the cursor is located to the line after this line
d0			#Delete all contents from the line where the cursor is located to the front of this line
di+Plus sign	  #Erase everything in the symbol

Text mode

i			#Enter the editing mode from the normal mode, and the insertion position is in front of the cursor
a			#Enter the editing mode from the normal mode, and the insertion position is behind the cursor
I       	#From normal mode to edit mode, the insertion position is at the beginning of the line
A       	#From normal mode to edit mode, the insertion position is at the end of the line
o       	#Enter the editing mode from the normal mode, and create a new line under the line where the cursor is inserted
O       	#Enter the editing mode from the normal mode, and create a new line above the line where the cursor is inserted
ESC			Switch from edit mode to normal mode
Backspace 	Delete the text in front of the cursor in edit mode

Command line mode

:w		preservation
:q		Exit without saving
:wq		Save and exit
:wq!	Force save and push
:x      Save and exit
/key word
n		Find keywords down
N   	Find keywords up
:noh  	Unhighlight keywords
:setnu	Display rows
N + Ctrl + A		Fast addition and subtraction
N + Ctrl + X
f+word			Fast positioning
 key word+ Ctrl+p 	Word Competion 

Regular expressions and three swordsman commands

regular expression

^       What does it start with,"^oldboy" Indicates a match to oldboy Line beginning with word
$       What does it end with,"oldboy$",Indicates a match to oldboy Line at the end of the word
^$      A combination symbol that represents a blank line. Logical explanation is based on^Start with $Ending line
.       Matches any and only one character, but does not match an empty line
\       Escape characters that invalidate special symbols, such as"\."Decimal point only
*       Match previous character(Continuous occurrence)0 Times or more, note that when repeating 0 times	Wait, it means nothing, but it will skim all the contents
.*      Combo, matching all content
[abc]   matching[]Any character contained in a or b or c
[a-z]   matching[]Included a-z Any character
[0-9]   matching[]Contains 0-9 Any number of
[^abc]  Match does not contain^Any character after a or b or c,there^Express right[abc]The opposite of the outside^Different meanings
--------------------expand
+		Match the first character one or more times
[:/]+   Match in parentheses ":" or "/" Character 1 or more times
?       Match the previous character 0 or 1 times
|       Indicates or, that is, multiple strings are filtered at the same time
()      Group filtering, the enclosed content represents a whole, in addition()The content of can be used later\n quote, n Is a number that refers to the contents of the first few parentheses
\n      Reference front()Content in, for example(abc)\1 Indicates a match abcabc
a{n,m}  Match the previous character at least n Times, up to m second
a{n,}   Match the previous character at least n second
a{,m}   Matches the previous character at most m second
a{n}    Match the previous character exactly n second

grep

grep text search, regular expression search text

Basic grammar

usage: grep [option]... PATTERN [FILE]...

option

Regular expression selection and interpretation:
-E, --extended-regexp     PATTERN Is an extensible regular expression(Abbreviated as ERE)
-F, --fixed-strings       PATTERN Is a set of fixed length strings separated by a line break.
-G, --basic-regexp        PATTERN Is a basic regular expression(Abbreviated as BRE)
-P, --perl-regexp         PATTERN It's a Perl regular expression 
-e, --regexp=PATTERN      use PATTERN To match
-f, --file=FILE           from FILE Obtained in PATTERN
-i, --ignore-case         ignore case
-w, --word-regexp         force PATTERN Only exactly matched words
-x, --line-regexp         force PATTERN Only one exact match
-z, --null-data           A 0-byte data line, but not an empty line

'egrep'Namely'grep -E'. 'fgrep'Namely'grep -F'. 
Direct use'egrep'or'fgrep'It is no longer feasible.
if FILE by -,The standard input will be read. No FILE,Reads the current directory unless specified on the command line-r Options.
If less than two FILE Parameter is used by default-h Parameters.
If any line is matched, the exit status is 0, otherwise it is 1;
If an error occurs and is not specified -q Parameter, the exit status is 2.

Disk command

lsblk list block devices

Command function:

Print out the information of the current block device and partition, including size, type and mount point

[root@Centos7-100 ~]#:lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   40G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   39G  0 part 
  ├─centos-root 253:0    0   37G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sr0              11:0    1  4.5G  0 rom  

Detailed explanation of mount in Linux and usage of mount command

Mount concept

To access files outside the root directory in Linux, you need to "associate" them to a directory under the root directory. This association operation is "Mount", and this directory is the "mount point". The process of removing the secondary association relationship is called "uninstall".

Note: the directory of "mount point" requires the following requirements:

(1) The directory exists in advance. You can create a new directory with mkdir command;

(2) The mount point directory cannot be used by other processes;

(3) The original files under the mount point will be hidden.

Mount and unload

Mounting method: mount DECE MOUNT_POINT

mount: display all devices mounted on the current system by viewing the / etc/mtab file

Command format: mount [-fnrsvw] [-t vfstype] [-o options] device dir

Command format: mount [-fnrsvw] [-t vfstype] [-o options] device dir

Device: indicates the device to be mounted;

(1) Device file: for example, / dev/sda5

(2) Volume LABEL: - L 'LABEL', e.g. - L 'MYDATA'

(3) UUID, -U 'UUID': for example, - U '0c50523c-43f1-45e7-85c0-a126711d406e'

(4) Pseudo file system name: proc, sysfs, devtmpfs, configfs

dir: mount point

Pre-existing; It is recommended to use an empty directory;

The device in use by the process cannot be uninstalled;

Common command options:

- t vsftype: specify the file system type on the device to mount;

- r: readonly, read-only mount;

- w: read and write;

- n: do not update / etc/mtab;

- a: automatically mount all devices that support automatic mounting; (it is defined in the / etc/fstab file, and the "auto mount" function is available in the mount option)

- L 'LABEL': specify the mounting equipment with the volume LABEL;

- U 'UUID': specify the device to be mounted with UUID;

- B, --bind: bind directory to another directory;

Note: view all mounted devices tracked by the kernel: cat /proc/mounts

-o options: (options for mounting the file system)

async: asynchronous mode;

sync: synchronization mode;

Atime / noaatime: contains directories and files;

Diratime / nodiraatime: access timestamp of the directory

auto/noauto: whether auto mount is supported

exec/noexec: does it support running applications on the file system as processes

dev/nodev: whether to support the use of device files on this file system;

suid/nosuid: whether special permissions are supported on this file system

Remount: remount

ro: read only

rw: reading and writing

user/nouser: whether to allow ordinary users to mount this device

acl: enables acl functionality on this file system

Note: the above options can be used at the same time, separated by commas;

Default mount options: defaults: rw, suid, dev, exec, auto, nouser, and async

The above information can be seen by viewing the super block information, which will not be demonstrated here.

Uninstall command: umount

Command format:

​ # umount DEVICE

​ # umount MOUNT_POINT

The use of umount has been demonstrated above

fuser: view the processes accessing the specified file system:

Command format:

​ # fuser -v MOUNT_POINT

Terminate all processes accessing the specified file system: use with caution

​ # fuser -km MOUNT_POINT

swap partition

Here we introduce the exchange partition, and demonstrate the creation and mounting of the exchange partition through experiments

Mount switch partition:

Enable: swapon

swapon [OPTION]... [DEVICE]

- a: activate all switching partitions;

- p PRIORITY: specify priority;

Disable: swapoff [OPTION]... [DEVICE]

Boot auto mount

If we want to automatically mount a device after startup, we just need to modify the / etc/fstab file.

File mounted configuration file: / etc/fstab

Device or pseudo file system to mount mount mount point file system type mount option dump frequency self test order

UUID=6efb8a23-bae1-427c-ab10-3caca95250b1 /boot  xfs    defaults    0 0

    Device or pseudo file system to mount: device file LABEL(LABEL=""),UUID(UUID=""),Pseudo file system name(proc, sysfs)

    Mount point: specified folder

   Mount options: defaults

    Dump frequency:

      0: No backup

      1: Daily dump

      2: Dump every other day

    Self inspection sequence:

      0: No self check

      1: First, self inspection; Generally only rootfs Only use 1;

/Differences between / etc/fstab and / etc/mtab

/Functions of the etc/fstab file:

The information about the hard disk partition on the computer is recorded. When starting Linux, the fsck command to check the partition and the mount command to mount the partition need the information in the fstab to correctly check and mount the hard disk.

/Functions of etc/mtab file:

First look at its English is:

​ This changes continuously as the file /proc/mount changes. In other words, when filesystems are mounted and unmounted, the change is immediately reflected in this file.

Mount point: specified folder

Mount options: defaults

Dump frequency:

  0: No backup

  1: Daily dump

  2: Dump every other day

Self inspection sequence:

  0: No self check

  1: First, self inspection; Generally only rootfs Only use 1;
### **/Differences between / etc/fstab and / etc/mtab**

####    **/Function of etc/fstab file * *: 

Record the relevant information of the hard disk partition on the computer, and start Linux Check the partition when fsck Command, and mount partition mount Orders, all required fstab To correctly check and mount the hard disk. 

####   **/Function of etc/mtab file * *: 

   First look at its English is: 

​     This changes continuously as the file /proc/mount changes. In other words, when filesystems are mounted and unmounted, the change is immediately reflected in this file. 

​     It records the file system that has been loaded by the current system, including the virtual file established by the operating system, etc; and/etc/fstab The system is ready to load. whenever mount Mount partition umount When the partition is unloaded, it will be updated dynamically mtab,mtab Always maintain the mounted partition information in the current system, fdisk,df Such programs must be read mtab File to get the partition mounting status in the current system. Of course, we can also read by ourselves/proc/mount You can also get the current mount information