Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Linux Compression Command

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Linux Compression Command

The common compression formats of Linux are .zip, .gz, .bz2, .tar, .tar.gz, .tar.bz2; the common compression commands are zip and tar. Examples of the use of various compression commands are given here. For more uses, please use the command-- help to check.

Zip

Format:

Zip [options] destination package name source file to be compressed unzip [- Z] [options] source file to be compressed [list] [- x xlist] [- d exdir]

Common commands:

# compressed files zip myfile.zip myfile# compressed folder (including subdirectories) zip-r mydir.zip mydir# compressed all files in the current directory zip mydir.zip * # extract files unzip mydir.zip

More parameters for zip:

-v display operation details-d delete files from compressed package-m cut files into compressed package, source files will be deleted-r recursive compression-x exclude files-c plus one line comment-z plus remarks-T test compression package integrity-e encryption-Q quiet mode-1,-fast faster compression speed-9,-best better compression ratio-help view help-h3 view more help

More parameters for unzip:

-v display operation details-l view the contents of the package-d extract to the specified folder-x exclude files within the package-t test package file contents-z view notes-o overwrite files without prompt-Q quiet mode-help view help

Example:

$lst.md t.php t.php.zip# create package $zip-v myfile.zip t.* adding: t.md (in=8121) (out=1051) (deflated 87%) adding: t.php (in=740) (out=319) (deflated 57%) adding: t.php.zip (in=1666) (out=1666) (stored 0%) total bytes=10527 Compressed=3036-> 71% savings# Test package Integrity $zip-T myfile.zip test of myfile.zip OK# Test package File contents $unzip-t myfile.zip Archive: myfile.zip testing: t.md OK testing: t.php OK testing: t.php.zip OKNo errors detected in compressed data of myfile.zip.# View in the package Content $unzip-l myfile.zip Archive: myfile.zip Length Date Time Name- 8121 06-08-2016 17:03 t.md 740 06-08-2016 17:02 t.php 1666 07-30-2016 17:38 t.php.zip- -10527 3 files# delete files from the package t.php.zip $zip-d myfile.zip t.php.zipdeleting: t.php.zip# delete files from the package t.php$ zip-d myfile.zip t.phpdeleting: t.php# add files to the package $zip-u myfile.zip t.php adding: t.php (deflated 57%) # here Add comments to the package $zip-z myfile.zipenter new zip file comment (end with.): test.# view the package comments $unzip-z myfile.zip Archive: myfile.ziptest# extract to the specified folder $unzip myfile.zip-d myArchive: myfile.ziptest. Inflating: my/t.md inflating: my/t.php# excludes the file from unzipping $unzip myfile.zip-x t.php-d myArchive: myfile.ziptest. Inflating: my/t.md

Gz

Format:

Gzip [options] Source file to be compressed gunzip [options] File to be unzipped

Instead of writing the final compressed file name, the .gz suffix is automatically appended and the source file is deleted.

Common commands:

# Compression 1.log, while automatically deleting the source file gzip 1.log#, decompressing 1.log.gz, automatically deleting the gzip-d 1.log.gz# compression 1.log, keeping the source file gzip-k 1.log# decompressing 1.log.gz, and keeping the compression package gzip-dk 1.log.gz# to view the package information gzip-l 1.log.gz# recursively to compress gzip-r mydir for each file in the directory.

Note: gunzip is equivalent to gzip-d and can extract gz files.

More parameters:

-c,-- stdout will display the compressed content in standard output, keep the original file-1,-- fast faster compression speed-9,-- best better compression ratio

Example:

# compress 1.log to 1.log.gz and keep the source file gzip-c 1.log > 1.log.gz

Bz2

Format:

Bzip2 [options] Source file to be compressed bunzip2 [options] File to be unzipped

Common commands:

# compress 1.logbzip2 1.logbzip2-k 1.log# and decompress 1.log.bz2bzip2-d 1.log.bz2bzip2-dk 1.log.bz2bunzip2 1.log.bz2bunzip2-k 1.log.bz2

More parameters:

-c,-- stdout will display the compressed content in standard output, keep the original file-1,-- fast faster compression speed-9,-- best better compression ratio

Tar

Format:

Tar [options] destination package name source file to be compressed

Common commands:

# after packaging, decompress test.tar.gztar zxvf test.tar.gz# with gzip compression tar zcvf test.tar.gz / test # compression / test for test.tar.gz#, and decompress test.tar.bz2tar jxvf test.tar.bz2# with bzip2 compression tar jcvf test.tar.bz2 / test # compression / test only Do not compress tar cvf test.tar / test # Compression / test unzip test.tartar xvf test.tar# View contents list of compressed packages tar tvf test.tar.gz# unzipped to specified folder (destination folder must exist) $tar-zxvf all.tar.gz-C my/# Compression excludes some directories $tar-zcvf tomcat.tar.gz-- exclude=tomcat/logs tomcat$ tar-zcvf tomcat.tar.gz-- exclude=tomcat / logs-exclude=tomcat/libs-exclude=tomcat/xiaoshan.txt tomcat

Description of common parameters:

-c,-- create: create a compressed file-x,-- extract,-- get: extract-t,-- list: view-r,-- append: append a file to the end of the compressed archive file-u,-- update: update the files in the original archive-d,-- diff,-- compare compare the files in the archive with the file system-- delete is deleted from the archive.

These are independent commands, and one of them is used for compression and decompression, which can be used with other commands, but only one of them can be used. The following parameters are optional when compressing or decompressing files as needed:

-z,-- gzip,-- gunzip,-- ungzip:-j with gzip attribute,-- bzip2:-Z,-- compress,-- uncompress with bz2 attribute:-v with compress attribute,-- verbose: show all procedures-O,-- to-stdout: unpack files to standard output-C,-- directory=DIR: extract to specified folder

The final parameter-f is required:

-f,-- file=ARCHIVE: use the file name. Keep in mind that this parameter is the last parameter and can only be followed by the file name.

View command help:

Tar-helptar -? tar-- usage

More examples:

# compare the changes between the files in the compressed package and the source files $tar-- diff-vf 1.log.tar 1.log1.log1.log: Mod time differs1.log: Size differs# delete the 1.log$ tar in the compressed package-- delete-vf 1.log.tar 1.log# add files to the compressed archive file $tar rvf 1.log.tar 1.log 2.log1.log2.log# to update the file $tar uvf 1.log.tar 1.log2.log in the compressed archive file

Description: files cannot be appended or updated to tar.gz and tar.bz2:

Tar zrvf all.tar.gz 3.logtar: Cannot update compressed archivesTry 'tar-- help' or' tar-- usage' for more information.

These are the details of the Linux compression command (summary). Please pay more attention to other related articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report