Home Operation and Maintenance Linux Operation and Maintenance Linux Compression Commands (Summary)

Linux Compression Commands (Summary)

Jan 23, 2020 pm 01:35 PM
linux

Linux Compression Commands (Summary)

Linux compression command

Common compression formats in Linux include .zip, .gz, .bz2, .tar , .tar.gz, .tar.bz2; commonly used compression commands include zip and tar. Here are examples of usage of each compression command. For more usage, please use the command --help.

Recommended: "Linux Tutorial"

zip

Format:

zip [options] 目标压缩包名称 待压缩源文件
unzip [-Z] [options] 待压缩源文件 [list] [-x xlist] [-d exdir]
Copy after login

Common commands:

# 压缩文件
zip myfile.zip myfile
# 压缩文件夹(包含子目录)
zip -r mydir.zip mydir
# 压缩当前目录所有文件
zip mydir.zip *
# 解压文件
unzip mydir.zip
Copy after login

zipMore parameters:

-v 显示操作详细信息
-d 从压缩包里删除文件
-m 将文件剪切到压缩包里,源文件将被删除
-r 递归压缩
-x 排除文件
-c 加一行备注
-z 加备注
-T 测试压缩包完整性
-e 加密
-q 安静模式
-1, --fast 更快的压缩速度
-9, --best 更好的压缩率
--help 查看帮助
-h2 查看更多帮助
Copy after login

unzipMore parameters:

-v 显示操作详细信息
-l 查看压缩包内容
-d 解压到指定文件夹
-x 排除压缩包内文件
-t 测试压缩包文件内容
-z 查看备注
-o 覆盖文件无需提示
-q 安静模式
--help 查看帮助
Copy after login

Example:

$ ls
t.md  t.php t.php.zip
Copy after login
# 创建压缩包
$ 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
# 测试压缩包完整性
$ zip -T myfile.zip 
test of myfile.zip OK
# 测试压缩包文件内容
$ unzip -t myfile.zip 
Archive:  myfile.zip
    testing: t.md                     OK
    testing: t.php                    OK
    testing: t.php.zip                OK
No errors detected in compressed data of myfile.zip.
# 查看压缩包里内容
$ 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
# 从压缩包里删除文件t.php.zip   
$ zip -d myfile.zip t.php.zip
deleting: t.php.zip
# 从压缩包里删除文件t.php
$ zip -d myfile.zip t.php
deleting: t.php
# 添加文件到压缩包里
$ zip -u myfile.zip t.php
  adding: t.php (deflated 57%)
# 给压缩包添加注释  
$ zip -z myfile.zip
enter new zip file comment (end with .):
test
.
# 查看压缩包注释
$ unzip -z myfile.zip 
Archive:  myfile.zip
test
# 解压到指定文件夹
$ unzip myfile.zip -d my
Archive:  myfile.zip
test .
  inflating: my/t.md                 
  inflating: my/t.php
# 排除文件不解压
$ unzip myfile.zip  -x t.php -d my
Archive:  myfile.zip
test .
  inflating: my/t.md
Copy after login

gz

Format:

gzip [options] 待压缩源文件
gunzip [options]  待解压文件
Copy after login

You don’t need to write the final compressed file name, it will automatically add the .gz suffix at the end and delete the source file at the same time .

Commonly used commands:

# 压缩1.log,同时会自动删除源文件
gzip 1.log
# 解压1.log.gz,同时会自动删除压缩包
gzip -d 1.log.gz
# 压缩1.log,保留源文件
gzip -k 1.log
# 解压1.log.gz,保留压缩包
gzip -dk 1.log.gz
# 查看压缩包信息
gzip -l 1.log.gz
# 递归的对目录里的每个文件单独压缩
gzip -r mydir
Copy after login

Note: gunzip and gzip -d are equivalent and can decompress gz files.

More parameters:

-c, --stdout 将压缩后的内容在标准输出显示出来,保留原文件
-1, --fast 更快的压缩速度
-9, --best 更好的压缩率
Copy after login
Copy after login

Example:

# 压缩1.log为1.log.gz,保留源文件
gzip -c 1.log > 1.log.gz
Copy after login

bz2

Format:

bzip2 [options] 待压缩源文件
bunzip2 [options]  待解压文件
Copy after login

Common commands:

# 压缩1.log
bzip2 1.log
bzip2 -k 1.log
# 解压1.log.bz2
bzip2 -d 1.log.bz2
bzip2 -dk 1.log.bz2
bunzip2 1.log.bz2
bunzip2 -k 1.log.bz2
Copy after login

More parameters:

-c, --stdout 将压缩后的内容在标准输出显示出来,保留原文件
-1, --fast 更快的压缩速度
-9, --best 更好的压缩率
Copy after login
Copy after login

tar

Format:

tar [options] 目标压缩包名称 待压缩源文件
Copy after login

Commonly used commands:

# 打包后,以gzip 压缩
tar zcvf test.tar.gz /test  #压缩/test为test.tar.gz
# 解压test.tar.gz
tar zxvf test.tar.gz 
# 打包后,以bzip2 压缩
tar jcvf test.tar.bz2 /test  #压缩/test为test.tar.bz2
# 解压test.tar.bz2
tar jxvf test.tar.bz2
# 仅打包,不压缩
tar cvf test.tar /test  #压缩/test为test.tar
# 解压test.tar
tar xvf test.tar
# 查看压缩包内容列表
tar tvf test.tar.gz
# 解压到指定文件夹(目标文件夹必须存在)
$ tar -zxvf all.tar.gz -C my/
# 压缩时排除某些目录
$ 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
Copy after login

Common parameter description:

-c, --create: 建立压缩档案
-x, --extract, --get:解压
-t, --list:查看内容
-r, --append:向压缩归档文件末尾追加文件
-u, --update:更新原压缩包中的文件
-d, --diff, --compare 将压缩包里的文件与文件系统进行对比
    --delete 从压缩包里删除
Copy after login

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

-z, --gzip, --gunzip, --ungzip:有gzip属性的
-j, --bzip2:有bz2属性的
-Z, --compress, --uncompress:有compress属性的
-v, --verbose:显示所有过程
-O, --to-stdout:将文件解开到标准输出
-C, --directory=DIR:解压到指定文件夹
Copy after login

The last parameter -f is required:

-f, --file=ARCHIVE: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。
Copy after login

View command help:

tar --help
tar -?
tar --usage
Copy after login

More examples:

# 比较压缩包里文件与源文件变动
$ tar --diff -vf 1.log.tar 1.log
1.log
1.log: Mod time differs
1.log: Size differs
# 删除压缩包里的1.log
$ tar --delete -vf 1.log.tar 1.log
# 向压缩归档文件里追加文件
$ tar rvf 1.log.tar 1.log 2.log
1.log
2.log
# 向压缩归档文件里更新文件
$ tar uvf 1.log.tar 1.log 2.log
Copy after login

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

$ tar zrvf all.tar.gz 3.log
tar: Cannot update compressed archives
Try 'tar --help' or 'tar --usage' for more information.
Copy after login

The above is the detailed content of Linux Compression Commands (Summary). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

What to do if the docker image fails What to do if the docker image fails Apr 15, 2025 am 11:21 AM

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

How to back up vscode settings and extensions How to back up vscode settings and extensions Apr 15, 2025 pm 05:18 PM

How to back up VS Code configurations and extensions? Manually backup the settings file: Copy the key JSON files (settings.json, keybindings.json, extensions.json) to a safe location. Take advantage of VS Code synchronization: enable synchronization with your GitHub account to automatically back up all relevant settings and extensions. Use third-party tools: Back up configurations with reliable tools and provide richer features such as version control and incremental backups.

See all articles