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]
Common commands:
# 压缩文件 zip myfile.zip myfile # 压缩文件夹(包含子目录) zip -r mydir.zip mydir # 压缩当前目录所有文件 zip mydir.zip * # 解压文件 unzip mydir.zip
zipMore parameters:
-v 显示操作详细信息 -d 从压缩包里删除文件 -m 将文件剪切到压缩包里,源文件将被删除 -r 递归压缩 -x 排除文件 -c 加一行备注 -z 加备注 -T 测试压缩包完整性 -e 加密 -q 安静模式 -1, --fast 更快的压缩速度 -9, --best 更好的压缩率 --help 查看帮助 -h2 查看更多帮助
unzipMore parameters:
-v 显示操作详细信息 -l 查看压缩包内容 -d 解压到指定文件夹 -x 排除压缩包内文件 -t 测试压缩包文件内容 -z 查看备注 -o 覆盖文件无需提示 -q 安静模式 --help 查看帮助
Example:
$ ls t.md t.php t.php.zip
# 创建压缩包 $ 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
gz
Format:
gzip [options] 待压缩源文件 gunzip [options] 待解压文件
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
Note: gunzip and gzip -d are equivalent and can decompress gz files.
More parameters:
-c, --stdout 将压缩后的内容在标准输出显示出来,保留原文件 -1, --fast 更快的压缩速度 -9, --best 更好的压缩率
Example:
# 压缩1.log为1.log.gz,保留源文件 gzip -c 1.log > 1.log.gz
bz2
Format:
bzip2 [options] 待压缩源文件 bunzip2 [options] 待解压文件
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
More parameters:
-c, --stdout 将压缩后的内容在标准输出显示出来,保留原文件 -1, --fast 更快的压缩速度 -9, --best 更好的压缩率
tar
Format:
tar [options] 目标压缩包名称 待压缩源文件
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
Common parameter description:
-c, --create: 建立压缩档案 -x, --extract, --get:解压 -t, --list:查看内容 -r, --append:向压缩归档文件末尾追加文件 -u, --update:更新原压缩包中的文件 -d, --diff, --compare 将压缩包里的文件与文件系统进行对比 --delete 从压缩包里删除
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:解压到指定文件夹
The last parameter -f is required:
-f, --file=ARCHIVE: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。
View command help:
tar --help tar -? tar --usage
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
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.
The above is the detailed content of Linux Compression Commands (Summary). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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).

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).

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.

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)

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.

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.

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 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.
