Linux command usage tutorial

Table of contents

1. Basics of Linux commands

1. ls, ll commands - display data

①ls command - display data in tiles

②ll command - list display data

2. cd, pwd commands - directory switching commands

①cd command——switch working directory

②pwd command - view the current working directory

 Edit

3. mkdir command - create directory

4. touch-cat-more command

①Touch command——Create file

②cat command - view all file contents and display them

③more command - View file content and support page turning

5. cp-mv-rm command

①cp command - copy files and folders

②mv command - move files and folders

③rm command - delete files, folders, wildcards

6. which-find command

①which command - View the program file storage location of the command

 ②find command-find files by file name

7. grep-wc command

① grep command - filter files

②wc command - quantity statistics 

③Pipe character - the result on the left is used as the input on the right

8. echo-tail-redirector command

①echo command——output the specified content

②tail command - view the content at the end of the file

9. vi, vim editor

2. Linux users and permissions

1. su command - switch users

1.1. Switch to root user (password required)

1.2. Switch to another user, such as jackma (no password required)

2. sudo command - add root permissions to ordinary users

2.1. Usage

2.2. Summary

3. Users and user groups 

1. User group management

1.1. Create user group

1.2. Delete user group

2. User management

2.1. Create user

2.2. Delete users

2.3. View the groups to which the user belongs

2.4. Modify the group to which the user belongs

4. View permission control 

4.1. Cognitive permission information

4.2. Permission details 

5. chmod command - modify permission information

6. chown command 

3. Practical Linux Operations

1. Small shortcut key operations

1.1. ctrl + c force stop

1.2. ctrl + d to exit or log out

1.3, Clear screen

2. Software installation

3、systemctl

4. Soft connection

5. Date, time zone

6. IP address, host name

7. Network requests and downloads

7.1. Use the ping command to check whether the server can be connected.

7.2. Use wget command to download files

7.3. Use the curl command to initiate a network request

8. Port

8.1. Use the nmap command to check the port occupancy.

8.2. Use the netstat command to check the occupancy of the specified port.

9. Main process management

9.2. View the specified process

9.3. Close the process

10. File upload and download

10.1. Data exchange through FinalShell tool virtual machine

13. Compression and decompression

13.1. tar command (complex)

13.1.1. tar command compression

13.1.2. tar decompression

13.2. zip and unzip commands (simpler)

13.2.1. zip command to compress files

13.2.2. Unzip command to decompress files


1. Basics of Linux commands

1. ls, ll commands - display data

①ls command - display data in tiles

The ls command displays the data in a tiled manner.

②ll command - list display data

 The ll command displays data in a list

-a option means: all means listing all files (including hidden files/folders)

-l option, means: display the content in the form of a list (vertically arranged) and display more information

 

-h means to list the file sizes in an easy-to-read form, such as K, M, G

The -h option must be used with -l

2. cd, pwd commands - directory switching commands

①cd command——switch working directory

Syntax: cd [Linux path]

②pwd command - view the current working directory

 

 special path character

 

3. mkdir command - create directory

Syntax: mkdir [-p] Linux path

Create a folder in the current directory

Create a folder under the / directory

If you want to create multiple levels of directories at once, as shown below

An error will be reported because the upper-level directory test does not exist, so the test directory cannot be created. You can use the -p option to create the entire chain.

4. touch-cat-more command

①Touch command——Create file

②cat command - view all file contents and display them

This is the content of the txt.txt file 

Use the cat command to view

③more command - View file content and support page turning

During the viewing process, use the space to turn pages and use q to exit viewing. 

5. cp-mv-rm command

①cp command - copy files and folders

As shown above, copy the txt.txt file from the ~ directory to the move folder.

If you copy a directory, it cannot be copied

Use the -r command to copy the directory successfully.

②mv command - move files and folders

 Move the txt.txt file to the move folder

If the target does not exist, it will be renamed

Move folder

③rm command - delete files, folders, wildcards

 Delete txt2.txt file

Currently, there is a txt.txt file in move2, and the move2 folder cannot be deleted directly.

At this time, you need to add -r to delete it.

If used as wildcard

  Delete all files or folders starting with test

6. which-find command

Use su -root to switch to administrator mode

①which command - View the program file storage location of the command

 

 ②find command-find files by file name

Search for a file named txt.txt starting from the root directory

Fuzzy matching based on wildcards

 Perform fuzzy prefix matching

fuzzy suffix matching

Matches content containing txt.txt

Search based on file size 

 Find files larger than 1G

7. grep-wc command

① grep command - filter files

This is the content of txt.txt

Find the gcc content in the txt.txt file through grep pipe character

Find the line location of gcc and add -n

 

Find multiple data with the same content

②wc command - quantity statistics 

The statistics are shown in the figure:

③Pipe character - the result on the left is used as the input on the right

As shown in the figure below, count the "-" data of the txt.txt file 

As shown below, the number in the current directory is counted.

As shown below, find the txt.txt file in the current directory

As shown below, the input of the current directory of ls will be used as the input of the first grep, and the input of the first grep will be used as the input of the second grep.

8. echo-tail-redirector command

①echo command——output the specified content

Backtick (floating sign)

Redirector

 overwrite

append write 

Write the directory into the txt.txt file

②tail command - view the content at the end of the file

This is the content of the txt.txt file

By default, the last ten lines of content are viewed 

View the contents of the last 5 lines

-f will continue to track and dynamically display any changes.

9. vi, vim editor

vim file name

Use i to enter compile mode

Save and exit using Esc key and :wq

2. Linux users and permissions

1. su command - switch users

1.1. Switch to root user (password required)

su - root

1.2. Switch to another user, such as jackma (no password required)

are - jackma

2. sudo command - add root permissions to ordinary users

2.1. Usage

Switch to the root user and execute the visudo command, which will automatically enter the vim compiler.

su - root

enter password

visudo

Add the following command to the last line

Add permissions for ordinary users, then:wq to save

Switch to normal user

are - jackma

Create a directory in the root directory with insufficient permissions

Use sudo command

Created successfully

2.2. Summary

You can make an ordinary command have root privileges, the syntax is:

sudo other commands 

You need to execute the visudo command as the root user, and add configurations to allow ordinary users to have sudo command execution permissions. 

3. Users and user groups 

There are two levels of permission control in Linux, which are:

  • User permission control
  • Permission control for user groups

For example, for a certain file, you can control the permissions of the user or the permissions of the user group. 

1. User group management
1.1. Create user group

groupadd user group name

1.2. Delete user group

groupdel user group name

2. User management

2.1. Create user

useradd [-g -d] username

  • -g specifies the user's group. If -g is not specified, a group with the same name will be created and automatically joined. Specifying -g requires that the group already exists. If a group with the same name already exists, -g must be used.
  • -d specifies the user HOME path. If not specified, the HOME directory defaults to:/home/username

Add to boss group

  

If you do not specify a directory, it will automatically be created in the home directory.

Create in the specified /home/user directory

2.2. Delete users

userdel [-r] username

  • -r, delete the user's HOME directory. If -r is not used, the HOME directory will be retained when the user is deleted. 

 Before deletion

Delete user3

 After deletion

2.3. View the groups to which the user belongs

id username

 

2.4. Modify the group to which the user belongs

Add the specified user to the specified group

usermod -aG user group username

Add user2 to boss2 group

2.5. Check the users in the system

getent passwd 

The bottom two lines are user1 and user2 

There are 7 pieces of information, namely
user name: password (x): user ID: group ID: description information (useless): HOME directory: execution terminal (default bash)

4. View permission control 

4.1. Cognitive permission information

Use ls -l to view the content in list form and display permission details

  • Serial number 1 represents the permission control information of files and folders.
  • Serial number 2 indicates the user to whom the file and folder belongs.
  • Serial number 3 indicates the user group to which the file and folder belongs.
4.2. Permission details 

Permission details are divided into 10 slots in total

Example: drwxr-xr-x, means:

  • This is a folder, the first letter d means
  • The permissions of the user (number 2 in the upper right corner) are: r, w, x, rwx
  • The permissions of the user group it belongs to (number 3 in the upper right corner) are: r, no w, x, rx (- means no such permission)
  • The permissions of other users are: r, no w, x, rx 

So, what does rwx stand for?

  • r means read permission
  • w means write permission
  • x represents execution permission 

The meaning of rwx is slightly different for different files and folders.

  • r
    • For files , you can view the file content
    • For folders , you can view the contents of the folder, such as the ls command
  • w
    • Indicates that the file can be modified
    • For folders , you can create, delete, rename, etc. operations within the folder.
  • x
    • Indicates that the file can be executed as a program
    • For a folder , it means you can change the working directory to this folder, that is, cd to enter 

5. chmod command - modify permission information

We can use the chmod command to modify the permission information of files and folders. Note that only the user who owns the file or folder or the root user can modify it.

chmod [-R] permissions file or folder

-R, applies the same operation to the entire contents of the folder 

For example:

chmod u=rwx,g=rx,o=x hello.txt, change the file permissions to: rwxr-x--x

  • Among them: u represents the user permissions to which user belongs, g represents group group permissions, and o represents other user permissions.

chmod -R u=rwx,g=rx,o=x test, set the permissions of the folder test and all contents in the folder to: rwxr-x--x

  • In addition, there is a shortcut: chmod 751 hello.txt 

 Permissions before modification

Permissions after modification 

6. chown command 

Use the chown command to modify the users and user groups that files and folders belong to.

Ordinary users cannot change their membership to other users or groups, so this command is only applicable to root users.

chown [-R] [user] [:] [group] file or folder

  • Option, -R, same as chmod, applies the same rules to all contents in the folder
  • Options, users, modify the user
  • Options, user groups, modify user groups to which they belong
  • : used to separate users and user groups 

chown root hello.txt, change the user belonging to hello.txt to root

chown:root hello.txt, change the user group to which hello.txt belongs to root

chown root:itheima hello.txt, change the user hello.txt belongs to root, and change the user group to itheima

chown -R root test, change the user belonging to the folder test to root and apply the same rules to all contents in the folder 

As shown in the figure, modify the test file permissions to the root user

As shown in the figure, modify the test file permissions to the boss user group

3. Practical Linux Operations

1. Small shortcut key operations

1.1. ctrl + c force stop

If you want to forcefully stop certain programs running on Linux, you can use the shortcut key ctrl + c

If the command is entered incorrectly, you can also use the shortcut key ctrl + c to exit the current input and re-enter.

1.2. ctrl + d to exit or log out

You can use the shortcut key: ctrl + d to log out of the account.

Or exit the exclusive page of some specific programs

1.3, Clear screen

Use the shortcut key ctrl + l to clear the terminal contents

2. Software installation

yum: RPM package software manager, used to automatically install and configure Linux software, and can automatically resolve dependency issues.

yum [-y] [install] [remove] [search] software name

  • Options: -y, automatic confirmation, no need to manually confirm the installation or uninstallation process
  • install: install
  • remove: uninstall
  • search: search 

The yum command requires root privileges. You can switch to root with su, or use sudo to escalate privileges.

The yum command requires an Internet connection 

For example:

yum [-y] install wget, install the wget program through the yum command

yum [-y] remove wget, uninstall the wget command through the yum command

yum search wget, use the yum command to search whether there is a wget installation package

3、systemctl

The systemctl command controls the startup and shutdown of software

Many software (built-in or third-party) in Linux systems support the use of systemctl command control: start, stop, and auto-start at boot.

Software that can be managed by systemctl is also generally called: service

systemctl start | strop | status | enable | disable service name

  • start start
  • stop close
  • status view status
  • enable Enable auto-start at boot
  • disable Turn off auto-start at boot

The system has many built-in services, such as:

NetworkManager, the main network service

network, deputy network service

firewalld, firewall service

sshd, ssh service (FinalShell uses this service to log in to Linux remotely) 

4. Soft connection

Create soft links in the system to link files and folders to other locations. Similar to "Shortcut" in Windows system

ln -s parameter 1 parameter 2

  • -s option, creates a soft connection
  • Parameter 1: The linked file or folder
  • Parameter 2: The destination to link to

 Example:

ln -s /etc/yum.conf ~/yum.conf

ln -s /etc/yum ~/yum

5. Date, time zone

5.1. date command

You can check the system time on the command line through the date command.

date [-d] [+format string]

  • -d displays the date according to the given string, generally used for date calculations
  • Format string: Use specific string tags to control the displayed date format 
    • %Y year
    • %y The last two digits of the year (00..99)
    • %m month(01..12)
    • %d day(01..31)
    • %H hours (00..23)
    • %M minutes (00..59)
    • %S seconds (00..60)
    • %s The number of seconds since 1970-01-01 00:00:00 UTC

 5.2, ntp program

We can automatically calibrate the system time through the ntp program

Install

ntp:yum -y install ntp

Start and set up auto-start:

systemctl start ntpd

systemctl enable ntpd 

When ntpd is started, it will regularly help us calibrate the system time online.

You can also calibrate manually (requires root privileges):

ntpdate -u ntp.aliyun.com

Automatically calibrate through the service URL provided by Alibaba Cloud and the ntpdate (this command will be included after installing ntp) command

6. IP address, host name

View ip address instructions

ifconfig

If you cannot use the ifconfig command, you can install

yum -y install net-tools

7. Network requests and downloads

7.1. Use the ping command to check whether the server can be connected.

You can use the ping command to check whether the specified network server is reachable.

ping [-c num] ip or host name

  • Option: -c, the number of checks. If you do not use the -c option, you will continue to check an unlimited number of times.
  • Parameters: ip or host name, the ip address or host name address of the server being checked 
7.2. Use wget command to download files

wget is a non-interactive file downloader that can download network files from the command line

wget [-b] url

  • Option: -b, optional, background download, the log will be written to the wget-log file in the current working directory
  • Parameters: url, download link 

Example: wget http://archive.apache.org/dist/hadoop/common/hadoop-3.3.0/hadoop-3.3.0.tar.gz

Download in the background: wget -b http://archive.apache.org/dist/hadoop/common/hadoop-3.3.0/hadoop-3.3.0.tar.gz

You can monitor the background download progress through the tail command: tail -f wget-log 

7.3. Use the curl command to initiate a network request

curl can send http network requests and can be used to: download files, obtain information, etc.

curl [-0] url

  • Options: -O, for downloading files, when the url is a download link, you can use this option to save the file
  • Parameters: url, the network address to initiate the request 

Initiate a network request to cip.cc: curl cip.cc

8. Port

View port occupancy

8.1. Use the nmap command to check the port occupancy.

Install nmap: yum -y install nmap

Syntax: nmap the IP address being viewed 

As you can see, 7 ports on this machine (127.0.0.1) are now occupied by programs.

8.2. Use the netstat command to check the occupancy of the specified port.

Syntax: netstat -anp | grep port number

Install netstat: yum -y install net-tools 

 

As shown in the figure, you can see that the current system port 6000 is occupied by the program (process number 7174), 0.0.0.0:6000, which means that the port is bound to the IP address 0.0.0.0, which means external access is allowed.

As you can see, the current system port 12345 is unused. 

9. Main process management

9.1. View the process

You can view process information in the Linux system through the ps command

ps [-e -f]

  • Option: -e, displays all processes
  • Options: -f, display information in fully formatted form (display all information) 

Generally speaking, the fixed usage is: ps -ef lists all information of all processes 

ps -ef 

 

  

9.2. View the specified process

 We can use the pipe character with grep to filter, such as:

Accurately find redis command information

 ps -ef | grep redis

Filter process information with the 6379 keyword (generally refers to filtering the 6379 process number) 

 ps -ef | grep 6379

 

9.3. Close the process

In Linux, a process can be shut down with the kill command.

kill [-9] process id

Option: -9 means forcefully shutting down the process. Not using this option will send a signal to the process asking it to shut down, but whether to shut down depends on the process's own processing mechanism. 

10. File upload and download

10.1. Data exchange through FinalShell tool virtual machine

In the lower form of the FinalShell software, the Linux file system view is provided, which can be conveniently:

  • Browse the file system, find the appropriate file, right-click to download, and transfer it to your local computer
  • Browse the file system, find the appropriate directory, and expand the files on the local computer to easily upload data to Linux.

Upload windows files to linux using drag and drop

Download linux files to windows using download

10.2, rz, sz command

In addition to transferring files through the lower form of FinalShell, you can also transfer files through the rz and sz commands.

rz, sz commands need to be installed

yum -y install lrzsz

Usage: Use rz command directly

rz 

The effect is as follows: 

sz command to download

Syntax: sz file to download

As shown below: 

 

13. Compression and decompression

13.1. tar command (complex)

You can use the tar command to perform compression and decompression operations.

Syntax: tar [-c -v -x -f -z -C] Parameter 1 Parameter 2 ... Parameter N

  • -c, creates a compressed file, used in compression mode
  • -v, displays the compression and decompression process, used to view the progress
  • -x, decompression mode
  • -f, the file to be created, or the file to be decompressed, the -f option must be the last one among all options
  • -z, gzip mode, without -z it is ordinary tarball format
  • -C, select the decompression destination for decompression mode 
13.1.1. tar command compression

For example:

  • tar -cvf test.tar 1.txt 2.txt 3.txt

Compress 1.txt 2.txt 3.txt into the test.tar file

  • tar -zcvf test.tar.gz 1.txt 2.txt 3.txt

Compress 1.txt 2.txt 3.txt into the test.tar.gz file, using gzip mode

Note: If the -z option is used, it is usually the first option bit. The -f option must be the last option bit. 

13.1.2. tar decompression
  • tar -xvf test.tar

Unzip test.tar and extract the file to the current directory

  • tar -xvf test.tar -C /home/itheima

Unzip test.tar and extract the file to the specified directory (/home/itheima)

  • tar -zxvf test.tar.gz -C /home/itheima

Decompress test.tar.gz in Gzip mode and extract the file to the specified directory (/home/itheima)

Notice:

  • -f option, must be the last bit in the option combination
  • -z option, recommended at the beginning
  • The -C option is used alone, separate from other parameters required for decompression 
13.2. zip and unzip commands (simpler)
13.2.1. zip command to compress files

You can use the zip command, and the compressed file is a zip compressed package.

zip [-r] Parameter 1 Parameter 2 ... Parameter N

  • -r, when compressing the included folder, you need to use the -r option, which has the same effect as the -r of rm, cp and other commands. 

 Example:

  • zip test.zip a.txt b.txt c.txt

Compress a.txt b.txt c.txt into the test.zip file

  • zip -r test.zip test itheima a.txt

Compress the test and itheima folders and the a.txt file into the test.zip file

There is no txt.zip file before compression

Compress txt.txt file to txt.zip file

13.2.2. Unzip command to decompress files

Use the unzip command to easily decompress the zip archive.

unzip [-d] parameter

  • -d, specifies the location to decompress, the same as tar's -C option
  • Parameters, the decompressed zip package file 

unzip test.zip , extract test.zip to the current directory

unzip test.zip -d /home/itheima , unzip test.zip to the specified folder (/home/itheima)

There is no txt.txt file before decompression

After decompressing txt.zip, there is a txt.txt file 

 

Guess you like

Origin blog.csdn.net/weixin_55127182/article/details/131881491