Based on the environment

Tar Command introduction

The tar command is frequently used in Linux, for example, decompress offline software packages, package a directory for backup, and decompress a compressed package to a specific directory. The tar command is used to package one or more directories and one or more files into a file with suffix tar, and to compress archive files into a file with suffix tar.gz. You can extract a tar or tar.gz file to a specified directory. This command is used more frequently on Linux without an interface and must be mastered.

Format of the tar command

Decompression command

tar -zxvf test.tar.gz
Copy the code

Compression command

tar -zcvf test.tar.gz ./
Copy the code

Common parameters of the tar command

-c, --create Create a new ARCHIVE -x, --extract, --get Extract files from the ARCHIVE -f, --file=ARCHIVE Use the ARCHIVE -z, --gzip, --gunzip, --ungzip filters the archive by gzip -c, --directory=DIR changes to the directory DIR -v, --verbose lists the files processed in detailCopy the code

Compress all files in the current directory

tar -zcvf test.tar.gz ./
Copy the code

Command description:

Gz Compressed file name of the test.tar.gz directory

./ indicates the compressed directory. The current directory is compressed

– ZCVF See the preceding parameter description

Decompress the compressed file to the current directory

tar -zxvf test.tar.gz
Copy the code

Description:

Test.tar. gz Backup file to be decompressed

– ZXVF Parameter Refer to the preceding parameter description. The only difference between compression and decompression is that the decompression parameter is X and the compression parameter is C

The default path is the current one

Decompress the backup file to the specified path

 tar -zxvf test.tar.gz -C /home/
Copy the code

Description:

Test.tar. gz Backup file to be decompressed

-zxvf Parameter Refer to the preceding parameter description. The only difference between compression and decompression is that the decompression parameter is -x and the compression parameter is -c

-c Decompress the package to the specified directory /home

Packages files in the specified directory without directory structure

 tar -zcvf test.tar.gz -C /home/testDir/ .
Copy the code

Description:

Test.tar. gz Compressed file name

-zxvf Parameter Refer to the preceding parameter description. The only difference between compression and decompression is that the decompression parameter is -x and the compression parameter is -c

-c For compression, use a relative path that does not contain the directory result /home/testdir/

. Compress all files in relative directories