1. Common commands of linux

Table of contents

1. Introduction to Linux

Two, Linux file system directory

3. The use of vi and vim in Linux

Four, Linux shutdown, restart, logout

Fourth, Linux user management

Five, the operating level of Linux

Six, Linux file directory command

Seven, Linux time and date command

Eight, Linux compression and decompression instructions

Nine, Linux search and find
instructions


1. Introduction to Linux

1. Introduction to Linux

①Linux is an operating system, free and open source, safe, efficient, stable, and able to handle high concurrency

② Deploy enterprise-level projects to linux/unix servers to run

2. Main distributions of Linux

Kernel improvements in Linux: CentOSE, Redhat, Suse, Ubuntu, Red Flag Linux

Two, Linux file system directory

1. Basic introduction to linux directory structure

Linux has a tree-like directory structure, the topmost root directory is "/", and other directories are created under this root directory/ . Everything is a file in Linux

2. Linux directory introduction

first row

①/bin: store frequently used commands

②/boot: Start the core files used by Linux, including some connection files and mirror files

③/dev: device manager, all hardware is stored in the form of files

④/etc: store configuration files

⑤/home: store the home directory of ordinary users, and each user has its own directory. Named after username account

second line

①/lib: library file directory

②/lost+found: restore file directory

③/media: media files

④/mnt: Temporary mount point directory

The third row

①/opt: store the installation package

②/proc: kernel and process file directory

③/root: the root directory of the root user

④/sbin: command program for system management

fourth line

①/srv: stores the data of some network services of the system

②/tmp: temporary file directory

③/usr: The user uses the installed program

④/var: store frequently changing data, log cache files

3. The use of vi and vim in Linux

1. Three modes of vi and vim

①Normal mode

In normal mode, shortcut keys are available. Move the cursor up, down , left, and right, delete characters and delete the entire line of processing content, copy and paste processing data.

②Edit mode

Press i to enter.

③Command line mode

Press esc to return to normal mode, then save and exit: wq, force exit: q!, exit: q, etc.

2. Use vim to develop hello.java program

①vim hello.java

enter normal mode

②Press i to enter edit mode

 ③ After pressing esc, enter: wq to save

3. Shortcut keys for vi and vim (in normal mode)

①Copy: yy Copy down 5 lines 5yy Paste: p

②Delete: dd delete down dd

③ Search: command line / keyword , n is the next

④ Undo: u

⑤The first line: gg, the last line: G

⑥Display line number: set nu and cancel line number: set nonu

4. Shortcut keys for vim

Four, Linux shutdown, restart, logout

1. Shutdown, restart command

sync

Synchronize the data in the memory to the disk, and operate before shutting down and restarting

shutdown

①shutdown -h now: Shut down immediately

②shutdown -h 1: Shut down after 1 minute

③shutdown -r now: restart immediately

shut  down

reboot  restart

Fourth, Linux user management

User Management

1. Add user

useradd wzl

2. Specify a password

passwd wzl

3. Delete user

userdel wzl (do not delete the home directory, recommended)

userdel -r wzl (delete home directory)

4. Query user information

pattern id

5. Switch user

su - wzl

6. Return to the original user

exit

User Group Management

1. What is a user group

Similar to roles, the system can manage multiple users in a unified manner

2. Add group

groupadd group name

3. Delete group

groupdel group name

4. Specify user group

useradd -g groupname username

5. Add user zhangsan, specify group student

①Create group student

groupadd student

② Create a user-specified group

useradd -g student zhangsan

③ View user information

id zhangsan

5. Modify user group

usermod -g groupname username

6. Change the user zhangsan to the teacher group

usermod -g teacher zhangsan

Profiles for users and groups

1. User information file /etc/passwd

① Notes for each line of vim /etc/passwd

Username: Password: Userid: Groupid: Commentary Description: Home Directory: Login Shell

2. Group information configuration file /etc/group

Group name: password: group id: list of users in the group

3. Password profiles, passwords and login information

/etc/shadow

Five, the operating level of Linux

1. What are the operating levels of linux

0: shutdown

1: Single user (retrieve lost password)

2: No network service for multiple users

3 : Multiple users have network services

4: reserved

5: Graphical interface

6: restart

Runlevel configuration file /etc/inittab

switch run level instruction init [ here is the number of levels]

2. Switch between different run levels through the init command

init 3 Multi-user Internet service

init 5 graphical interface

init 0 shutdown

3. How to retrieve the root password

Idea: enter single-user mode init 1, root can log in without a password

① When starting up, see this interface and press Enter

②Input e 

 ③ Select the second input e

 ④Enter 1 to specify single-user mode, press Enter to save

⑤ After entering b, it enters the single-user mode

Enter the command: passwd root

4. Run linux to directly enter the command ha interface, the operation level is 3

①vim /etc/inittab

②Modify the last line id:3:initdefault:

Six, Linux file directory command

1.pwd command

show current directory path

2.ls command

① Function: Display the files and directories of the current directory

② Syntax: ls [-a or -l] path

③Common options:

ls -a shows all files including hidden

ls -l list to display detailed information, abbreviated as ll

3. cd command

①Function: switch directory

②Usage: cd [parameter] path

③Parameters:

cd ~ or cd  back to your home directory

cd .. Go back to the previous directory

④ Absolute path: start/locate from the root directory

Relative path: start positioning from the current path

⑤ Application

Case 1: Use an absolute path to switch to the root directory cd /root

Case 2: The current path is /etc/hgfs Use a relative path to the /root directory cd ../../root

Case 3: Return to the current upper directory cd ..

Case 4: Back home directory cd or cd~

4. mkdir command

① Function: create directory

② Syntax: mkdir [options] to create a directory

③Option: mkdir -p multi-level directory to be created

④Application:

Example 1: Create a directory /home/dog mkdir /home/dog

Example 2: Create a multi-level directory /home/animal/triger mkdir -p /home/animal/triger

5. rmdir command

① Function: delete empty directory

②Syntax: rmdir [option] To delete an empty directory

③Delete a non-empty directory: rm -rf the directory to be deleted

④Application:

Example 1: Delete directory /home/dog rmdir /home/dog

Example 2: Delete the animal directory (not empty) rm -rf the directory to be deleted

6. touch command

① Function: Create an empty file

②Syntax: touch filename

③Application:

Case 1: Create an empty file hello.txt touch hello.txt

Case 2: Create ok1.txt and ok2.txt at the same time touch touch ok1.txt ok2.txt

7. cp command

① Function: Copy

②Syntax: cp [option] source file target path

③Options: -r copy the entire folder

④ Application

Case 1: Copy /home/aaa.txt to /home/bbb single file

Case 2: Copy the entire directory of /home/bbb to /home/wzl

cp -r /home/bbb /home/wzl

8. rm command

① Function: delete

②Syntax: rm [option] directory or file to delete

③Options: -r delete the entire file -f force delete

④Application:

Case 1: delete /home/aaa.txt rm /home/aaa.txt

Case 2: Forcibly delete the entire folder /home/bbb rm -rf /home/bbb

9. mv command

①Function: Move files or directories, and you can also rename them

② Grammar:

Rename mv original name new name

Move file mv The path of the original file to move

③Application:

Case 1: Rename the /home/aaa.txt file to bbb.txt

mv /home/aaa.txt /home/bbb.txt

Case 2: Move the /home/bbb.txt file to the /root directory

mv /home/bbb.txt /root

10. cat command

①Function: read-only view file content

②Syntax: cat [option] file to view

③Option: -n: display line number

④Application:

Example 1: View the content of the /etc/profile file, display the line number -n, and display more in pages

cat -n /etc/profile | more

11. more command

①Function: Display file content by page

②Syntax: more files to be viewed

③Application: View the contents of the /etc/profile file more /etc/profile

④Shortcut keys

12. less instruction

①Function: Load and display file content by page, high efficiency for large files

②Syntax: less The file to be viewed

③Shortcut key:

13. Override > Directives and Appends >> Directives

① Grammar:

ls -l > a.txt   list content is written to file a.txt overwriting

ls -al > aa.txt   All content of the list is written to the file aa.txt to overwrite

cat file 1 > file 2 writes the contents of file 1 to file 2 overwriting

echo " content" >> b.txt appends "content" to the b.txt file

②Application:

Case 1: Overwrite the /home directory list to /home/info.txt

ll /home > /home/info.txt

Case 2: Append the current calendar information to the /home/mycal file

cal >> /home/mycal

14. echo command

①Function: echo output content to the console

②Application:

Case 1: Use the echo command to output the environment variable path to the console

echo $PATH

Case 2: Use the echo command to output hello, world

echo "hello,world"

15.head command

① Function: display the first 10 lines at the beginning of the file

② Grammar:

The head file displays the first 10 lines of the file

head -n 5 file displays the first 5 lines of the file

③Example:

View the first 5 lines of code in /etc/profile

head -n 5 /etc/profile

16.tail command

①Function: The tail command outputs the tail content of the file, and the last 10 lines are displayed by default

② Grammar:

tail file View the last 10 lines of the file

tail -n 5 file View the last 5 lines of the file

The tail -f file monitors the update of the file in real time

③ application

Real-time monitoring /home/a.txt

tail -f /home/a.txt

17. History command

①Function: View the historical instructions that have been executed

② Grammar: history

③Example:

Case 1: Display all historical instructions

history

Case 2: Display the last 10 historical instructions used

history 10

Case 3: Specify the command to execute the history

! execution number

18.ln instruction

①Function: soft link, symbolic link, similar shortcut

②Syntax: ln -s original directory or directory soft link name

③Application:

Case 1: Create a soft link linkToRoot in the /home directory to connect to the /root directory

ln -s /root /home/linkToRoot

Case 2: Delete linkToRoot

rm -rf  /home/linkToRoot

Seven, Linux time and date command

1. date command

① Display the current time

date

② Display the current year

date +%Y

③Display the current month

date +%m

④Display the current day

date +%d

⑤Format and display the current time

date "+%Y-%m-%d %H:%S:%S"

⑥Set the current time of the system to 2022-10-10 22:10:22

date -s "2022-10-10 22:10:22"

2.cal command

① Display the calendar of the current month

cal

②Display the calendar of 2022

cal 2022

Eight, Linux compression and decompression instructions

1.tar command

①Function: Packaging instruction, the file suffix is ​​.tar.gz

② Grammar:

tar -zcvf XXX.tar.gz packaged files or directories ( package )

tar -zxvf XXX.tar.gz packaged files or directories ( decompression )

③Option Description

④ Application

Example 1: Compress multiple files, compress /home/a.txt and /home/.txt into ab.tar.gz

tar -zcvf ab.tar.gz a.txt b.txt

Example 2: Compress the /home folder into myhome.tar.gz

tar -zcvf myhome.tar.gz /home

Example 3: Decompress myhome.tar.gz to the /opt/ directory

tar -zxvf myhome.tar.gz -C  /opt/myhome/

Example 4: Decompress ab.tar.gz to the current directory

tar -zxvf ab.tar.gz

2. zip/unzip command

① Function: zip compressed files, unzip decompressed files

② Grammar:

zip [options] XXX.zip compressed file or directory

unzip [options] XXX.zip

③Options:

-r recursive compression , compressed directory

-d The directory where the files are stored after decompression

④Application:

Case 1 Compress all files under /home into mypackage.zip

zip -r mypackage.zip /home

Case 2 unzip mypage.zip to /opt/tmp

unzip -d /opt/tmp/ mypackage.zip

3. gzip/gunzip command

① Function: gizp compressed files, gunzip decompressed files

② Grammar:

gzip file

gunzip-file.gz

③Application:

Case 1: gzip compression, compress hello.txt under /home

gzip /home/hello.txt

Case 2: decompress with gunzip, decompress hello.txt.gz under /home

gunzip /home/hello.txt.gz

Nine, Linux search and find commands

1. find command

①Function: Find files

②Syntax: find [search range] [option] file name

③Options:

④Application:

Case 1: Find the hello.txt file in the /home directory

find /home -name hello.txt

Case 2: Find all files in the /opt directory with the user name wzl

find /opt -user wzl

Case 3: Find files larger than 20m under linux

find / -size +20M

Case 4: Find all .txt files under linux

find / -name *.txt

2. locate command

①Function: locate the file path

②Syntax: locate search file

③Option: Before each use, use the updatedb command to update the database

④ Application: quickly locate the directory where the hello.txt file is located locate hello.txt

3. The grep command and the pipe symbol |

①Function: grep filter search, pipe symbol | continue to execute the processing result of the previous command

②Syntax: grep [option] Find content source file

③Options:

④Application:

Please find the line where "yes" is located in the hello.txt file, and display the line number

cat wang.txt | grep -ni yes (ignore case)

cat wang.txt | grep -n yes (case sensitive)

Guess you like

Origin blog.csdn.net/jbkjhji/article/details/132472033