Detailed explanation of Linux (including Linux installation tutorial)

Detailed explanation of Linux

1. Install the Linux operating system

Introduction to VMware

VMware (virtual machine) refers to a complete computer system with complete hardware system functions simulated by software and running in a completely isolated environment. Through it, more Microsoft Windows, Linux, and Mac OS can be run simultaneously on one computer. X, DOS system.

Install virtual machine VMware

Software Website: VMware Installation

Password: 7777

1. Right-click [VMware16.0] and select [Extract to VMware16.0].

picture

2. Open the decompressed folder, right-click [VMware-workstation-full-16.0.0-16894299] and select [Run as administrator].

picture

3. Click [Next].

picture

4. Check [I accept the terms of the license agreement] and click [Next].

picture

5. Click [Change...].

picture

6. Modify the C in the "Folder Name" path address to change the installation location (I changed C to D here to indicate installation on the D drive), and click [OK].

picture

7. Click [Next].

picture

8. Uncheck [Check at startup...] and [Join VMware...] and click [Next].

picture

9. Click [Next].

picture

10. Click [Install].

picture

11.Software installation in progress...

picture

12. Click [License].

picture

13. Enter the license key [ZF3R0-FHED2-M80TY-8QYGC-NPKYF] (you can enter any group below) and click [Enter].

ZF3R0-FHED2-M80TY-8QYGC-NPKYF

YF390-0HF8P-M81RQ-2DXQE-M2UT6

ZF71R-DMX85-08DQY-8YMNC-PPHV8

picture

14. When the installation is complete, click [Finish].

picture

15. Double-click the [VMware Workstation Pro] icon on the desktop to start the software.

picture

16. Installation successful.

picture

Download centos 7 system

Download URL: centos-7-isos-x86_64 installation package download_open source mirror station-Alibaba Cloud (aliyun.com)

Install centos 7 system

After the system is successfully booted, the following interface
description will appear:

  • Install CentOS Linux 7 Install CentOS 7

  • Test this media & install CentOS Linux 7 Test the installation file and install CentOS7

  • TroubleshootingDetailed graphic and text explanation of CentOS 7 system installation and configuration

1. Select Install CentOS Linux7 here, install CentOS 7, press Enter, and enter the following interface
Detailed graphic and text explanation of CentOS 7 system installation and configuration

2. Press Enter to enter the following interface
Detailed graphic and text explanation of CentOS 7 system installation and configuration

3. Wait until Checking: 100% and enter the following interface
Detailed graphic and text explanation of CentOS 7 system installation and configuration

4. Language selection interface, it is recommended to install the English version for the official production server. ContinueContinue
Detailed graphic and text explanation of CentOS 7 system installation and configuration

5. Select DATE&TIME, set the date and time, and then select Done
Detailed graphic and text explanation of CentOS 7 system installation and configuration

6. Select INSTALLATION DESTINATION again, enter the disk partition interface (I choose to automatically configure the partition, the default is enough), and then select Done
interface instructions:

  • Automatically configure partitioning Automatically configure partitioning
  • I will configure partitioning Configure the partition myself
  • I would like to make additional space available provide additional space

Detailed graphic and text explanation of CentOS 7 system installation and configuration

7. Leave other settings as default.
Detailed graphic and text explanation of CentOS 7 system installation and configuration

8. Select Begin Installation to start the installation and enter the following interface
Detailed graphic and text explanation of CentOS 7 system installation and configuration

9. Select ROOT PASSWORD, set the root password, and then select Done
Detailed graphic and text explanation of CentOS 7 system installation and configuration

10. Select USER CREATION, create a user and set a password, then select Done
Detailed graphic and text explanation of CentOS 7 system installation and configuration

11. After the installation is completed, you will enter the following interface. (It may take 10 minutes), select Reboot
Detailed graphic and text explanation of CentOS 7 system installation and configuration

12. After restarting, enter the following interface
Detailed graphic and text explanation of CentOS 7 system installation and configuration

13. Enter the username and password set previously to enter the following interface
Detailed graphic and text explanation of CentOS 7 system installation and configuration

The CentOS system installation is now complete.

2. Linux basic commands

Linux directory structure

  • The directory structure of Linux is a tree structure

  • Windows systems can have multiple drive letters, such as C drive, D drive, and E drive

  • Linux does not have the concept of a drive letter, there is only one root directory /, and all files are under it

Please add image description

  • In Linux systems, the hierarchical relationship between paths is represented by: /

  • In Windows systems, the hierarchical relationship between paths is represented by: \

Getting started with Linux commands

No matter what the command is and what it is used for, in Linux, the command has its universal format:

command  [-options]  [parameter]
  • command: the command itself

  • -options: [Optional, not required] Some options for the command. You can control the behavior details of the command through the options.

  • parameter: [optional, not required] The parameters of the command, most of which are used for the target of the command, etc.

[ ] means optional

In the following syntax, the ls command can use options

ls [-a -l -h] [Linux路径]
  • -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

Insert image description here

Directory switching related commands cd, pwd

cd command

When the Linux terminal (command line) is opened, the user's HOME directory will be used as the current working directory by default.

We can change the current working directory through the cd command.

The cd command comes from English: Change Directory

cd [Linux路径]
  • The cd command requires no options, only parameters, indicating which directory to switch to.

  • The cd command is executed directly without writing parameters, which means returning to the user's HOME directory.

pwd command

We can use the pwd command to view the current working directory.

The pwd command comes from: Print Work Directory

grammar:

  • pwd command, no options, no parameters, just enter pwd directly

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-SjpwFQAf-1692935083103) (C:\Users\Deadwood Fengchun i\AppData\Roaming\Typora\typora-user-images \image-20230824115200545.png)]

Relative paths, absolute paths and special path characters

Through pwd, we know that the current location is the HOME directory: /home/user

Now I want to switch the working directory to the Desktop folder through the cd command.

So, how to write the parameters (Linux path) of the cd command?

- cd /home/user/Desktop

- cd Desktop

Both of the above writing methods can correctly switch directories to the specified Desktop.

Absolute path : A way of describing the path starting from the root directory. The path description starts with /

Relative path : A way of writing a path using the current directory as the starting point. The path description does not need to start with /.

Special path characters:

  • . represents the current directory, for example, cd ./Desktop represents switching to the Desktop directory under the current directory, which has the same effect as cd Desktop.

  • … represents the upper-level directory, for example: cd … can switch to the upper-level directory, cd …/… can switch to the upper-level directory.

  • ~ represents the HOME directory, for example: cd ~ to switch to the HOME directory or cd ~/Desktop to switch to the Desktop directory in HOME

Master creating folders through the mkdir command

New directories (folders) can be created through the mkdir command

mkdir from English: Make Directory

mkdir [-p] [Linux路径]
  • The parameter is required, indicating the Linux path, that is, the path of the folder to be created, either a relative path or an absolute path.

  • The -p option is optional, indicating that non-existent parent directories are automatically created, which is suitable for creating continuous multi-level directories.

If you want to create multiple levels of directories at once without adding -p, an error will be reported because the lesson and class directories do not exist.

An entire chain can be created using the -p option.

Insert image description here

**ps: **Creating a folder requires modifying permissions. Please ensure that the operations are all within the HOME directory. Do not operate outside HOME because permission issues will arise. It will not succeed outside HOME.

File operation commands touch, cat, more

Files can be created through the touch command

grammar:

touch [Linux路径]
  • The touch command has no options, and the parameter is required, indicating the path of the file to be created. Relative, absolute, and special path characters can be used.

Insert image description here

After preparing the file contents, you can view the contents through cat.

grammar:

cat [Linux路径]
  • Cat also has no options, only required parameters. The parameters represent: the file path to be viewed. Relative, absolute, and special path characters can be used.
    Insert image description here

The more command can also view the file content. The difference from cat is:

  • cat directly displays all the content

  • more supports page turning. If the file content is too much, it can be displayed page by page.

grammar:

more [Linux路径]
  • There are also no options, only required parameters. The parameters represent: the file path to be viewed. Relative, absolute, and special path characters can be used.

File operation commands cp, mv, rm

cp command to copy files folder

The cp command can be used to copy files\folders. The cp command comes from the English word: copy

grammar:

cp [-r] 参数1 参数2
  • -r option, optional, used to copy folders, indicating recursion

  • Parameter 1, Linux path, indicates the copied file or folder

  • Parameter 2, Linux path, indicates the place to be copied.

Insert image description here

mv move files or folders

The mv command can be used to move files\folders. The mv command comes from the English word: move

grammar:

mv 参数1 参数2
  • Parameter 1, Linux path, indicates the file or folder to be moved

  • Parameter 2, Linux path, indicates the place to be moved. If the target does not exist, rename it to ensure that the target exists.

**rm delete folders and files**

The rm command can be used to delete files and folders

The rm command comes from the English word: remove

grammar:

rm 参数1 参数2 参数3 ...
  • Like the cp command, the -r option is used to delete a folder

  • -f means force, forced deletion (no confirmation message will pop up)

  • Ordinary users will not be prompted when deleting content. Only root administrator users will be prompted when deleting content.

  • Therefore, ordinary users do not need the -f option.

Parameter 1, parameter 2,..., parameter N represent the paths of files or folders to be deleted, separated by spaces.

Insert image description here

The rm command supports the wildcard character *, which is used for fuzzy matching.

  • The symbol * represents a wildcard character, which matches any content (including empty), example:

  • test* means matching anything starting with test

  • *test, means match anything ending with test

  • test , meaning to match anything containing test

Find command which, find

The Linux commands we learned earlier are actually binary executable programs.

It has the same meaning as the .exe file in Windows system.

We can use the which command to see where the program files for the series of commands used are stored.

grammar:

which Linux命令

Insert image description here

In Linux systems, we can search for specified files through the find command.

grammar:

find 起始路径 -name "文件名"

The file name to be searched supports the use of wildcard character * for fuzzy query.

  • The symbol * represents a wildcard character, which matches any content (including empty), example:

  • test* means matching anything starting with test

  • *test, means match anything ending with test

  • test , meaning to match anything containing test

Based on the meaning of wildcards, you can use the find command to perform fuzzy query on files.

Insert image description here

find command - find files by file size

grammar

find 起始路径 -size +|-n[KMG]
  • +, - means greater than and less than

  • n represents a large or small number

  • kMG represents the size unit, k (lowercase letter) represents kb, M represents MB, and G represents GB.

Example:

  • Find files smaller than 10KB: find / -size -10k

  • Find files larger than 100MB: find / -size +100M

  • Find files larger than 1GB: find / -size +1G

grep, wc and pipe character

grep command

You can use the grep command to filter file lines by keywords from a file.

grammar:

grep [-n] 关键字 文件路径

•Option -n, optional, indicates to display the line numbers of matching lines in the results.

•Parameter, keyword, required, indicating filtering keyword, with spaces or other special symbols, it is recommended to use "" to surround the keyword

•Parameter, file path, required, indicating the file path to filter the content, which can be used as the content input port

wc command to do quantity statistics

You can count the number of lines, words, etc. in a file through the wc command.

grammar:

wc [-c -m -l -w ] 文件路径
  • Option, -c, counts the number of bytes

  • Option, -m, counts the number of characters

  • Option, -l, counts the number of lines

  • Option, -w, counts the number of words

  • Parameters, file paths, files to be counted can be used as content input ports

Insert image description here

echo, tail and redirection characters

echo command

You can use the echo command to output specified content in the command line

grammar:

echo 输出内容
  • No options are required, only one parameter indicating the content to be output. Complex content can be surrounded by ""

  • With special symbols such as spaces or \, it is recommended to use double quotes.

The following command: echo pwd

The original intention is to output the current working path, but pwd is output as an ordinary character.

We can enclose the command with backticks (often also called floating signs), and the content will be executed as a command instead of ordinary characters.

Redirect characters: > and >>

  • The first one overwrites and writes the result of the command on the left to the file specified on the right side of the symbol.

  • The second one appends the result of the command on the left to the file specified on the right side of the symbol.

echo “Hello Linux” > a.txt 

Just input Hello Linux into a.txt

echo “Hello world” > a.txt

Execute again to overwrite new content

echo “Hello Code” >> a.txt

Execute again, use >> to append new content

tail command

Using the tail command, you can view the tail content of the file and track the latest changes to the file. The syntax is as follows:

tail [-f -num] Linux路径
  • Parameter, Linux path, indicating the tracked file path
  • Option, -f, means continuous tracking
  • The option, -num, means to check how many lines at the end, if not filled in, the default is 10 lines

Example:

查看/var/log/vmware-network.log文件的尾部10行:tail /var/log/vmware-network.log
查看/var/log/vmware-network.log文件的尾部3行:tail -3 /var/log/vmware-network.log

vim editor

vi\vim editor introduction

vi\vim is the abbreviation of visual interface and is the most classic text editor in Linux

Like the text editor in the graphical interface, vi is an excellent choice for editing text files under the command line.

Vim is an enhanced version of vi. It is compatible with all instructions of vi. It can not only edit text, but also has the function of shell program editing. It can identify the correctness of the grammar by using fonts of different colors, which greatly facilitates program design and editing.

Three working modes of vi\vim editor

1. Command mode

​ In command mode, the editor understands the keys you press as commands and executes different functions driven by commands.

Under this model, free text editing is not possible.

2. Insert mode

​ This is the so-called edit mode and insert mode.

​ In this mode, you can freely edit the file content.

3. Last line mode

  以:开始,通常用于文件的保存、退出。

If you need to edit the file through vi/vim editor, please use the following command:

vi 文件路径
vim 文件路径

vim is compatible with all vi functions, and all vim commands will be used in the future.

  • If the file represented by the file path does not exist, this command will be used to edit the new file

  • If the file represented by the file path exists, then this command is used to edit the existing file.

Command mode shortcut keys

Insert image description here

Insert image description here

bottom line command pattern

There is nothing special about the editing mode. After entering the editing mode, any shortcut keys have no effect, just enter text normally.

The only thing you need to remember is that you can return to command mode through esc.

In the command mode, enter: to enter the bottom line command mode, which supports the following commands:

Insert image description here

3. Linux users and permissions

Understanding the root user

Whether it is Windows, MacOS, or Linux, they all use multi-user management mode for permission management.

  • In the Linux system, the account with the most privileges is named: root (super administrator)

  • In the early days, the account we had been using was an ordinary user: user

su and exit commands

Previously, we have come across the su command to switch to the root account.

The su command is a system command used for account switching. Its source is the English word: Switch User

grammar:

su [-] 用户名
  • The - symbol is optional and indicates whether to load environment variables after switching users (explained later). It is recommended to bring them.
  • Parameters: username, indicating the user to be switched. The username can also be omitted. If omitted, it means switching to root.
  • After switching users, you can use the exit command to return to the previous user, or you can use the shortcut key: ctrl + d
  • When using an ordinary user, you need to enter a password to switch to other users, such as switching to the root user.
  • Use root user to switch to other users, no password is required, you can switch directly

sudo command

When we know the root password, we can switch to root through the su command to obtain maximum permissions.

However, we do not recommend using the root user for a long time to avoid system damage.

We can use the sudo command to authorize ordinary commands and temporarily execute them as root.

grammar:

sudo 其他命令
  • Before other commands, bring sudo to temporarily grant root authorization to this command.

  • But not all users have the right to use sudo. We need to configure sudo authentication for ordinary users.

Configure sudo authentication for ordinary users

  • Switch to the root user and execute the visudo command. It will automatically open through the vi editor: /etc/sudoers

  • At the end of the file add:

user ALL=(ALL)  NOPASSWD: ALL
  • The last NOPASSWD:ALL means to use the sudo command without entering a password.

  • Finally save via wq

User and user group management

User, user group

In Linux systems you can:

  • Configure multiple users

  • Configure multiple user groups

  • Users can join multiple 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.

User group management

We need to learn the basic commands for user and user group management in Linux to lay the foundation for learning permission control later.

The following commands need to be executed by the root user

  • Create user group

groupadd user group name

  • Delete user group

groupdel user group name

For subsequent demonstrations, we create a case user group: groupadd case

User Management

The following commands need to be executed by the root user

  • Create user
useradd [-g -d] 用户名
  • Option: -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.
  • Options: -d specifies the user HOME path. If not specified, the HOME directory defaults to: /home/username
  • delete users
userdel [-r] 用户名
  • Options: -r, delete the user's HOME directory, do not use -r, delete the user, the HOME directory is retained
  • View the group a the user belongs to
id [用户名]
  • Parameters: username, user being viewed, if not provided, view itself

  • Modify the group to which the user belongs

usermod -aG user group username, add the specified user to the specified user group

getent

Use the getent command to check which users are in the current system

grammar:

 getent passwd

Using the getent command, you can also check which user groups are in the current system.

grammar:

getent group

View permission controls

Insert image description here

The red circle indicates the permission control information of files and folders.

Insert image description here

  • 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, you can view the file content for the file

-For folders, you can view the contents of the folder, such as the ls command

  • w, for files, indicates that this file can be modified

​ - For folders, you can create, delete, rename, etc. operations within the folder

  • x, for files, indicates that the file can be executed as a program

​ - For a folder, it means that you can change the working directory to this folder, that is, cd to enter

Modify permission control - chmod

chmod command

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.

grammar:

chmod [-R] 权限 文件或文件夹
  • Options: -R, applies the same operation to the entire contents of the folder

The numerical serial number of the permission

Permissions can be represented by 3-digit numbers. The first digit represents user permissions, the second digit represents user group permissions, and the third digit represents other user permissions.

The details of the numbers are as follows: r is denoted as 4, w is denoted as 2, and x is denoted as 1. There can be:

  • 0: No permissions, that is -
  • 1: Only x permissions, i.e. --x
  • 2: Only w permission is -w-
  • 3: Have w and x permissions, namely -wx
  • 4: Only r permission is r–
  • 5: Have r and x permissions, that is, rx
  • 6: Have r and w permissions, that is, rw-
  • 7: Have full permissions, namely rwx

751 means: rwx(7) rx(5) --x(1)

Modify permission control - chown

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.

grammar:

chown [-R] [用户][:][用户组] 文件或文件夹
  • 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

4. Practical Linux Operations

Various tips (shortcut keys)

  1. ctrl + c force stop

  2. ctrl + d exit logout

  3. history View historical commands

  4. !Command prefix, automatically matches the previous command

  5. ctrl + r, search history commands

  6. ctrl + a | e, moves the cursor to the beginning or end of the command

  7. ctrl + ← | →, jump words left and right

  8. ctrl + l or clear command to clear the screen

Software Installation

Linux system application store

There are many ways to install software on the operating system, generally divided into:

  • Download the installation package and install it yourself
  • For example, win system uses exe files, msi files, etc.
  • For example, mac system uses dmg files, pkg files, etc.
  • Install from system app store
  • If the win system has a Microsoft Store
  • For example, the mac system has an AppStore store

The Linux system also supports these two methods. First, let's learn to use: "App Store" in the Linux command line, and the yum command to install software

yum command

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

grammar:

yum [-y] [install | remove | search] 软件名称
  • Options: -y, automatic confirmation, no need to manually confirm the installation or uninstallation process

  • install: install

  • remove: uninstall

  • search: search

ps:

  • 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

apt command (on Ubuntu system)

The various Linux commands learned previously are all universal. But for software installation, CentOS systems and Ubuntu use different package managers.

CentOS uses yum manager, Ubuntu uses apt manager

Through the WSL environment we learned earlier, we can get the Ubuntu operating environment.

grammar:

apt [-y] [install | remove | search] 软件名称

The usage is the same as yum, and also requires root permissions.

  • apt install wget, install wget

  • apt remove wget, remove wget

  • apt search wget, search wget

systemctl command

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

grammar:

systemctl start | stop | status | enable | disable 服务名
start 启动
stop 关闭
status 查看状态
enable 开启开机自启
disable 关闭开机自启

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)

Soft connection

Create soft links in the system to link files and folders to other locations.

Similar to "Shortcut" in Windows system

grammar:

ln -s 参数1 参数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

Date, time zone

date command

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

grammar:

date [-d] [+格式化字符串]
  • -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

Modify Linux time zone

Use root privileges and execute the following command to change the time zone to the East Eighth District time zone.

rm -f /etc/localtime
sudo ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

Delete the localtime file that comes with the system and link the /usr/share/zoneinfo/Asia/Shanghai file into a localtime file.

IP address, host name

IP address

Every computer connected to the Internet will have an address that is used to communicate with other computers.

There are two main versions of IP addresses, V4 and V6 (V6 is rarely used and will not be covered in the course)

The address format of the IPv4 version is: abcd, where abcd represents a number from 0 to 255. For example, 192.168.88.101 is a standard IP address.

You can check the IP address of the machine through the command: ifconfig. If you cannot use the ifconfig command, you can install it: yum -y install net-tools

Insert image description here

CPU name

Enter hostname in the Linux system to view the host name

You can use the command: hostnamectl set-hostname hostname to modify the hostname (root required)

DNS

Insert image description here

network transmission

ping command

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

grammar:

ping [-c num] ip或主机名
  • 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

Insert image description here

wget command

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

grammar:

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

curl command

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

grammar:

curl [-O] 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

port

  1. What is a port?

Port refers to the entrance and exit between the computer and the outside world, which can be divided into physical ports and virtual ports.

  • Physical ports: USB, HDMI, DP, VGA, RJ45, etc.

  • Virtual port: the gateway for interaction between the operating system and the outside world

IP can only determine the computer, and the program to be interacted with can be locked through the port.

  1. Port division
  • Recognized ports: 1~1023, used for system built-in or binding with commonly used well-known software
  • Registered port: 1024~49151, used for loose binding (user-defined)
  • Dynamic port: 49152~65535, for temporary use (mostly used for export)
  1. View port occupancy
  • nmap IP address, view the externally exposed port of the specified IP

  • netstat -anp | grep port number to check the occupation of the specified port number on this machine

Compression, decompression

Compression format

  • zip format: Linux, Windows, MacOS, commonly used

  • 7zip: commonly used in Windows systems

  • rar: Commonly used in Windows systems

  • tar: commonly used in Linux and MacOS

  • gzip: commonly used in Linux and MacOS

Commonly used software in Windows systems, such as winrar, bandizip and other software, all support various common compression formats, which will not be discussed here.

We now want to learn how to operate in the Linux system: tar, gzip, zip these three compression formats

Complete file compression and decompression operations.

tar command

There are two compression formats commonly used in Linux and Mac systems, and the suffix names are:

  • .tar, called tarball, is an archive file that simply assembles files into a .tar file. It does not reduce the file size much, it is just a simple package.

  • .gz, also commonly known as .tar.gz, gzip format compressed files, that is, using the gzip compression algorithm to compress the files into one file, which can greatly reduce the compressed volume.

For both formats, you can use the tar command to perform compression and decompression operations.

grammar:

tar [-c -v -x -f -z -C] 参数1 参数2 ... 参数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

Common combinations of tar are:

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

Notice:

•The -z option, if used, is usually in the first option position

•-f option, must be the last option bit

tar decompression combination

tar -xvf test.tar

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

tar -xvf test.tar -C /home/user

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

tar -zxvf test.tar.gz -C /home/user

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

Notice:

•-f option, must be the last bit in the option combination

•-z option, recommended at the beginning

•-C option used alone, separate from other parameters required for decompression

zip command to compress files

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

grammar:

zip [-r] 参数1 参数2 ...
  • -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.

Demo:

  • 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 case a.txt

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

unzip command to decompress files

Use the unzip command to easily decompress the zip archive.

grammar:

unzip [-d] 参数
  • -d, specifies the location to decompress, the same as tar's -C option

  • Parameters, the decompressed zip archive file

Demo:

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

  • unzip test.zip -d /home/user, extract test.zip to the specified folder (/home/user)

More information is available on GitHub . Readers are welcome to go to Star

⭐Academic exchange group Q 754410389 is being updated~~~

Guess you like

Origin blog.csdn.net/m0_63743577/article/details/132492526