Linux - practical operation 2 - common instructions

1, Seven operating levels of linux


Operation level description:
0: shutdown
1: Single user [ retrieve lost password ]
2: There is no network service in multi-user state
3: Network service in multi-user state
4: The system is not used and reserved for users
5: Graphical interface
6: Restart
The common domains are 3 and 5. To modify the operation level of the system, you can:

2, Help instruction

2.1 man is used to obtain the help document of an instruction.

View ls's help documentation
man ls
Note: exit help documentation: q

2.2 help

Check out the cd's help documentation

help cd

3, File directory class

3.1 pwd view the absolute road strength of the current directory

#

3.2 ls [options] [directory or file]: View

## View file list
ls
## View file list long directory
ls -l
## ## View the long directory of the file list and show hidden files
ls -la 

3.3 cd [option]: Open Directory

## Go back to your home directory
cd ~
cd enter
## Go back to the previous directory of the current directory
cd .. 
## Navigate to the directory in a relative manner
[root@localhost home]# cd ../etc
## Navigate to the directory in absolute mode
[root@localhost home]# cd /etcc'd

3.4 mkdir [options] name of folder to be created: create folder

## Create a directory under home
mkdir /home/video
## Create multi-level directory
mkdir -p /mnt/aaa/bbb/ccc

3.5 rmdir [options] empty directory to delete

[root@localhost ccc]# mkdir -p /mnt/aaa/ddd
## The directory to be deleted is empty
[root@localhost ccc]# rmdir /mnt/aaa/ddd
## rmdir can only delete empty directories
[root@localhost ccc]# rmdir /mnt/aaa
rmdir: delete "/mnt/aaa" fail: directory not empty
## Delete non empty directory
[root@localhost ccc]# rm -rf /mnt/aaa

3.6 touch file name: create an empty file

## Create an empty test.txt file
touch test.txt
## Create multiple empty files at once
touch ok1.txt ok2.txt ok3.txt

3.7 ★ copy [options] source dest: you can copy files or folders

Copy the source file or folder to dest.
Common options: - r recursively copy the entire folder

## Copy ok1.txt under aaa to bbb/ok5.txt. The relative path or absolute path used here can also be used
cp aaa/ok1.txt bbb/ok5.txt
## Copy aaa folder recursively into ccc. If there is a file with the same name in ccc, you will be prompted whether to overwrite it
cp -r aaa/ ccc/
## Forced copy. If there is a file with the same name in ccc, you will not be prompted to overwrite it, but directly overwrite it.
\cp -r aaa/ ccc/

★ 3.8 rm [options] file / folder: delete file or folder

-r,-R,–recursive
Recursively removes the contents of the directory.
-f,–force
Ignore nonexistent files and never prompt the user.

## Delete file, prompt
rm ok1.txt
## Delete file, no prompt
rm -f ok2.txt
## Prompt for deleting files
rm -r /mnt/ccc
## Delete folder no prompt
rm -rf /mnt/ccc
## Delete multiple files plus
rm -rf /mnt/ccc mnt/aaa

3.9 mv source destination: move files or rename

## Move ok1.java to bbb and rename it hello.java
[root@localhost bbb]# mv ../aaa/ok1.java ../bbb/hell1.java
## Move ok2.java to bbb, and the file name is still ok2.java
[root@localhost bbb]# mv ../aaa/ok2.java ../bbb/

3.10 cat [options] file path: view the file. It is read-only and cannot be modified

It is usually used in combination with more and displayed page by page
Options: - n display line number

## -n displays the line number, and more displays it in pages 
cat -n ok3.java | more

3.11 more: displays the contents of the file page by page in a full screen manner

more ok3.java

Shortcut key for more
Space: page turning
enter: next line
q: Exit
ctrl+f: previous screen
ctrl+b: next screen
=: output the line number of the current line
: f output file name and current line number

3.12 ★ less: it is used for split screen display of file content, with high speed. It does not read the whole file, but read it in pages.

You can view large log files

less ok3.java

Shortcut key
Space: page turning
enter: next page
q: Exit
/Character: search down [character]: n view the next; N view previous.
? Character: search up [character]: n view the previous; N view next.

3.13 > output redirection, > > append instruction

Basic syntax:
LS - L > file

## Overwrite the content obtained from ls -l into ok2.java
[root@localhost mnt]# ls -l > ok2.java
## Append the result of ls -al / to ok2.java
[root@localhost mnt]# ls -al / >> ok2.java
## Append the contents of the file ok1.java to ok3.java
[root@localhost mnt]# cat ok1.java >> ok3.java
## Overwrite the calendar into ok3.java; cal gets the calendar
[root@localhost mnt]# cal > ok3.java


3.14 use of echo, head, ★ tail.

echo: output content to the screen, which can be used to output environment variables
head: view the first 10 lines of information in the file.
tail: view the last 10 lines of information in the file. Can be used to view logs

## Output JAVA_HOME and PATH information
[root@localhost mnt]# echo $PATH -------------- $JAVA_HOME
/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/tiandy/.local/bin:/home/tiandy/bin:/usr/local/anaconda3/bin:/root/bin -------------- /opt/java/jdk-1.8
## View the first 5 lines of the file
[root@localhost mnt]# head -n5 ok1.java
## View the last 5 lines of the file, -n5 display the last 5 lines, -f update the newly added content in real time,
[root@localhost mnt]# tail -f -n5 ok1.java

3.15 in [option]: Create Shortcut

Options: - s: soft connection, software
in -s source file and / or directory soft connection name

## Establish soft connection
[root@localhost mnt]# ln -s /mnt/aaa linktoaaa
## When deleting the soft connection, the linktoaaa must not be followed by /, otherwise it cannot be deleted
[root@localhost mnt]# rm -rf linktoaaa

3.16 history: history command record

## Get command history
history
## 171 is the number before a command found in history. Execute this command
! 171

4, Date category directory

4.1 date: set the date and time, and display the time and date

Basic syntax:
date -s "2021-11-17 22:29:29"
date (display current time)
date + %Y
date + %m
date + %d
date + "%Y-%m-%d %H:%M:%S"

## Set the system time to 2021-11-17 22:29:29
[root@localhost bin]# date -s "2021-11-17 22:29:29"
2021 Wednesday, 17 November:29:29 CST
## Display year
[root@localhost bin]# date +%Y
2021
## Show month
[root@localhost bin]# date +%m
11
## Display day
[root@localhost bin]# date +%d
17
## Year, day, hour, minute and second in a specific format
[root@localhost bin]# date "+%Y-%m-%d %H:%M:%S"
2021-11-17 22:26:25```

4.2 cal: display calendar

## Displays the day of the current month
cal 
## Displays the calendar for a full year
cal 2021

5, Search class

5.1 find [search scope] [options]: find files or folders

Options:
-Name finds the specified file name
-User finds the file under the specified user
-Size find according to the specified file size

## Find the file named note.txt in the directory (or subdirectory) of / opt/elasticsearch-7.11.2/ and find it accurately
find /opt/elasticsearch-7.11.2/ -name NOTICE.txt
## Find the file whose owner is tiandy in the / opt directory (or subdirectory)
find /opt -user tiandy
## Find files larger than 20M in the / opt directory (or subdirectory)
find /opt -size +20M
## Find files smaller than 20M in the / opt directory (or subdirectory)
find /opt -size -20M
## Find a file equal to 20M in the / opt directory (or subdirectory)
find /opt -size 20M
★ Find by wildcard
find /opt -name *.txt

5.2 locate file or folder: quickly find files

Before the first execution, first create the locate query database
updatedb

## Before the first execution, first create the locate query database
updatedb
## Fuzzy search for files or folders in elasticsearch
locate elasticsearch

★ 5.3 grep instructions and pipeline symbols|

grep [options] search content: indicates filter search
Options: - n display line number- i ignore case
|: the pipe symbol indicates that the processing result output of the previous command is passed to the subsequent command processing.

hold cat tools.sh The content of is passed|Give it to the back grep Find instruction processing -n Display line number,-i Case insensitive ip content
[root@localhost opt]# cat tools.sh | grep -ni ip

6, Compression and decompression class

6.1 gzip and gunzip: file compression / decompression instructions

gzip: compressed file. The source file is lost after compression
gunzip: unzip the file.

## Compressed ok1.java the compressed ok1.java.gz source file ok1.java is deleted
[root@localhost mnt]# gzip ok1.java
## Unzip the ok1.java.gz file. After unzipping, ok1.java.gz is deleted
[root@localhost mnt]# gunzip ok1.java.gz 

6.2 zip and unzip: compression / decompression instructions

zip [options]: -r recursively compress folders
unzip [options]: the location where the - d file is unzipped

zip -r /mnt/aaa aaa.zip
unzip -d /mnt/bbb /mnt/aaa.zip

6.3 ★ tar: compression and decompression instructions

tar instruction is a packaging instruction. After packaging, the file suffix is:. tar.gz.
Basic syntax:
Tar [options] xxx.tar.gz
Option Description:
-c: Package create to generate tar file
-v: Display details
-f: Specifies the compressed file name
-z: Pack and compress
-x: Unzip the tar file

## Package ok1.Java and ok2.java into a.tar.gz
[root@localhost aaa]# tar -zcvf a.tar.gz ok1.java ok2.java 
## Unzip a.tar.gz to the current directory x
[root@localhost aaa]# tar zxvf a.tar.gz 
## Unzip a.tar.gz to the specified folder / mnt/aaa/bbb. Note that - C (Change) is uppercase.
[root@localhost aaa]# tar -zxvf a.tar.gz -C /mnt/aaa/bbb

Tags: Linux server

Posted on Thu, 18 Nov 2021 19:48:29 -0500 by jharbin