preface

There are many common Linux commands used to compress or decompress files, and this article will show you some unusual but useful uses of these decompression commands.

tar

Tar is the most commonly used decompression command in Linux. The tar command can be used to process files with extensions tar, tar.gz, TGZ,.tar.Z, tar.bz2. Related parameters:

-x Extracts files from compressed files. -t Displays the contents of compressed files. -z Supports gzip decompression files. -j supports bzip2 decompression files -k Keeps the source files and does not overwrite them. -c Switches to a specified directory-fSpecify compressed files --delete deletes files from the package --strip-components removes directories --add-file Adds files to the packageCopy the code

The compression

Archive tar files without compression

tar -cvf test.tar test1.log test2.log # Archive multiple files
tar -cvf test.tar test/*  Archive all files in the test directory
tar -cvf test.tar *.log  Archive all files that end in.log
Copy the code

Because this method does not compress, only archiving, it is extremely fast and takes up a lot of space.

Archive and compress as tar.gz or tar.bz2

This way of packaging compresses the file:

tar -zcvf test.tar.gz file1 file2 Package and gzip compressed
tar -jcvf test.tar.bz2 file1 file2 Package and compress with bzip2
Copy the code

View the files in the compressed package

If you do not want to decompress the file and just want to view the contents of the compressed package, you can use the -t parameter:

tar -tvf test.tar You can see what files are in the test package
Copy the code

Delete the source file after packaging

Sometimes you may need to delete the source files after packaging, but deleting them one by one is cumbersome. We can use the –remove-files option:

tar -zcvf test.tar.gz test.log --remove-files 
Copy the code
Package files other than the specified directory or file

For files in some directories, you may only need to package some files, so use the –exclude option to exclude files that do not need to be packaged:

tar -zcvf test.tar.gz --exclude=test/*.log test/ *Package all files in the test directory, excluding files that end in.log
Copy the code

The –exclude option, which supports wildcards and regular expressions, is also very powerful.

Update files to the zip package

For example, if you want to update test and other files in the package, you can use the –add-file option:

tar -tf test.tar --add-file=test Tar only
Copy the code

Add files to the tar package

To add a file to a tar package, use the -r argument:

tar -rf test.tar testfile Add files to test.tar, only for tar
Copy the code

Delete files from the compressed package

Without decompression, files in the package can be deleted using the –delete option:

tar --delete -f test.tar  test1 # delete test1 file from test.tar
Copy the code

Unpack the

Decompress tar.gz and tar packages to the current directory

tar -xvf test.tar.gz
tar -xvf test.tar 
Copy the code

Decompress to the specified directory

tar -xvf test.tar.gz -C dir
tar -xvf test.tar -C dir
Copy the code

Decompress the file specified in the package

For example, the files in the test.tar.gz package are as follows:

1.txt
log/
log/1.log
log/2.log
log/2.log
log/4.log
log/5.log
Copy the code

If we just need to extract 1.log from the log directory, we just need to execute the following command:

tar -xvf test.tar.gz log/1.log
tar -xvf test.tar.gz log/1.log -C test Unzip 1.log to the test directory
Copy the code

Unzip the directory structure

The files in the compressed package may have multi-level directories. After the files are decompressed in common ways, the corresponding directories also exist. If you only want to compress the package file, you can remove the directory structure (note: the file name in the same folder cannot be the same) :

tar -xvf test.tar.gz --strip-components=1 Drop a layer of directories

Copy the code

The original file is not overwritten when decompressed

The current directory may already exist in the package. If you do not want the decompressed file to overwrite the existing file, you can use the -k parameter (which will throw an error message) :

tar -xvkf test.tar.gz
Copy the code

Special remind

The f parameter of the decompression or compression tape mentioned earlier needs to be placed last because it specifies the package name, otherwise the decompression or compression will fail.

zip/unzip

The zip and unzip commands are used to process zip packages.

The compression

Related parameters:

-dDeletes the specified file from the compressed file.-fEffect and specification of this parameter"-u"This parameter is similar, but not only updates existing files. If some files do not already exist in the compressed file, use this parameter to add them to the compressed file. -j Stores only the file name and its contents, but does not store any directory name. -r Indicates that all files in a specified directory and subdirectories are processed together. -u Replace a newer file to the compressed file. -v Displays the command execution process or version information. -y saves the symbolic link directly, not the file to which the link points. This parameter is valid only on UNIX systems. - < Compression efficiency > The compression efficiency is a number between 1 and 9.Copy the code

The compressed file

zip -r test.zip test/ Package the files in the test directory
zip -rj test.zip test/ Package the files in the test directory without the test directory
Copy the code

Specifies the compression rate for packaging files

zip -r8 test.zip test/ *# The higher the value (1-9), the higher the compression rate and the longer the time
Copy the code

Package symbolic link files

The previous command can only package regular files. If you want to package symbolic link files, use the -y argument:

zip  -ry test.zip test
Copy the code

Adds or updates files to a compressed package

When you need to add files to a compressed package, but do not want to decompress the package again, you can use the -u parameter:

zip -u test.zip test2 Add test2 file to test.zip package
Copy the code

Wechat public account [Programming] : focus on but not limited to sharing computer programming basics, Linux, C language, C++, data structures and algorithms, tools, resources and other programming related [original] technical articles. The original address: www.yanbinghu.com/2018/10/19/…

Compression encryption

To encrypt the compressed package during compression, use the -p parameter:

zip -r test.zip test1 test -P 66666 Use password 66666 to encrypt
Copy the code

Deletes a specific file for the compressed package

zip -d test.zip test  # delete test file from test.zip package
Copy the code

Unpack the

Related parameters:

-l-j stores only the file name and its contents and does not store any directory names. -o Sets the change time of the compressed file to the same as the command execution process or version information of the compressed file.-dSpecify the unzip directory, the directory does not exist will be createdCopy the code

View information about files in compressed packages

unzip -l test.zip You can see the file name, date and other information in the compressed package
unzip -v test.zip Check for more information, such as CRC check information
Copy the code

Decompression package

unzip -o test.zip -d dir Zip to the dir directory
Copy the code

Decompress the file specified in the package

If you do not know the name of the file to decompress, you can view the files in the package first and use the following method:

unzip -o test.zip "1.log" -d dir Unzip the 1.log file in the package to dir
unzip -o tet.zip "*.log" -d dir  Unzip all the log files in the package
Copy the code

Unzip the directory structure

If you want to compress only the files in the package, you can use the -j parameter:

zip -oj test.zip -d ./temp  
Copy the code

Extract the jar package

Jar is a Java archive, but can also be unzip to view the files inside:

unzip -o java.jar -d dir
Copy the code

gzip

Related parameters:

-k Saves the source file-dUnzip files. -r indicates that all files and subdirectories in a specified directory are processed at the same time. -V displays the command execution processCopy the code

When the tar command takes the -z argument and is packaged as a tar.gz file, it is compressed by calling gzip. The compression ratio of gzip to text is about 60% to 70%, and the compressed package files usually use the suffix gz. Save the source file with the -k argument:

gzip -k ./* Zip all files in the current directory, one gz package for each fileGzip-rkv./* Recursive compressionCopy the code

Decompression is also easy:

gzip -dv test.gz 
Copy the code

bzip2

When the tar command uses the -j argument to package the file as tar.bz2, bzip2 is called for compression. After bzip2 is compressed or decompressed, the source file is deleted. If you want to save the source file, use the -k parameter:

bzip2 -zk test  Zip the test file
bzip2 -dk test.bz2  # decompression

Copy the code

rar/unrar

Rar and unrar commands are not native to Linux distributions and need to be installed separately. Common usage is as follows:

rar a test.tar test  Zip the test file to test.tar
rar e test.rar       # extract test. The tar
unrar x test.rar     # extract test. The tar
Copy the code

Compressibility comparison

Compression rate in general:

tar.bz2>tar.gz>zip>tar
Copy the code

The higher the compression rate, the longer the compression and decompression time will be.

conclusion

File compression can save disk space and bandwidth for network transmission. However, space and time need to be weighed according to actual applications. There are many commands for decompressing files. To avoid inconvenience on other platforms, you can select common commands to compress files. If you have any comments or suggestions, please leave a message.

The above content comes from the public number [programming Abas]