[Linux]常用命令之【tar/zip/unzip/gzip/gunzip】
1 tar
.tar 只是进行打包,并没有压缩。 则:
用tar-cvf进行打包
用tar-xvf进行解包。
.tar.gz 是既打包又压缩 ,则:
tar –czvf [压缩包名] [文件或目录] // 打包,且使用 gzip 压缩
注: 生成的【压缩包的包名】可随意命名,tar命令做解压、压缩时 均不会受影响。
但建议,尽量规范化: 使用了 tar 打包,则:包名的文件格式后缀 第1段子后缀命名为 file.tar ; 以此类推,使用了 gizp 做压缩,则: .gz;若 二者均有使用,则: file.tar.gz
tar –xzvf [压缩包名] // 解包,且该包为 gzip 的压缩包
注: -z,--gzip, --gunzip, --ungzip : 通过 gzip 过滤归档。如果被解压的包并没有通过gz压缩,则 使用了-z参数会报错(gzip: stdin: not in gzip format)
.gz 只压缩。只能针对文件。如果需要对一个目录下的全部文件压缩,则:
gzip -r [目录] // 文件夹压缩
gzip [文件名] // 文件压缩
gzip –d [文件名] // 文件解压
tar命令参数详解
shell# man tar
NAME
tar - manual page for tar 1.26
SYNOPSIS
tar [OPTION...] [FILE]...
DESCRIPTION
GNU `tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive.
Note that this manual page contains just very brief description (or more like a list of possible functionality) originally generated by the
help2man utility. The full documentation for tar is maintained as a Texinfo manual. If the info and tar programs are properly installed at
your site, the command `info tar' should give you access to the complete manual.
EXAMPLES
tar -cf archive.tar foo bar
# Create archive.tar from files foo and bar.
tar -tvf archive.tar
# List all files in archive.tar verbosely.
tar -xf archive.tar
# Extract all files from archive.tar.
DEFAULTS
*This* tar installation defaults to:
--format=gnu -f- -b20 --quoting-style=escape --rmt-command=/sbin/rmt --rsh-command=/usr/bin/rsh
Main operation mode:
-A, --catenate, --concatenate
append tar files to an archive
-c, --create
create a new archive
-d, --diff, --compare
find differences between archive and file system
--delete
delete from the archive (not on mag tapes!)
-r, --append
append files to the end of an archive
-t, --list
list the contents of an archive
--test-label
test the archive volume label and exit
-u, --update
only append files newer than copy in archive
-x, --extract, --get
extract files from an archive
Common options:
-C, --directory=DIR
change to directory DIR
-f, --file=ARCHIVE
use archive file or device ARCHIVE
-j, --bzip2
filter the archive through bzip2
-J, --xz
filter the archive through xz
-p, --preserve-permissions
extract information about file permissions (default for superuser)
-v, --verbose
verbosely list files processed
-z, --gzip
filter the archive through gzip
Operation modifiers:
--check-device
check device numbers when creating incremental archives (default)
-g, --listed-incremental=FILE
handle new GNU-format incremental backup
-G, --incremental
handle old GNU-format incremental backup
--ignore-failed-read
do not exit with nonzero on unreadable files
--level=NUMBER
dump level for created listed-incremental archive
-n, --seek
archive is seekable
--no-check-device
do not check device numbers when creating incremental archives
--no-seek
archive is not seekable
--occurrence[=NUMBER]
process only the NUMBERth occurrence of each file in the archive; this option is valid only in conjunction with one of the subcommands
--delete, --diff, --extract or --list and when a list of files is given either on the command line or via the -T option; NUMBER
defaults to 1
--sparse-version=MAJOR[.MINOR]
set version of the sparse format to use (implies --sparse)
-S, --sparse
handle sparse files efficiently
Overwrite control:
-k, --keep-old-files
don't replace existing files when extracting, treat them as errors
--keep-newer-files
don't replace existing files that are newer than their archive copies
--no-overwrite-dir
preserve metadata of existing directories
--overwrite
overwrite existing files when extracting
--overwrite-dir
overwrite metadata of existing directories when extracting (default)
--recursive-unlink
empty hierarchies prior to extracting directory
--remove-files
remove files after adding them to the archive
--skip-old-files
don't replace existing files when extracting, silently skip over them
-U, --unlink-first
remove each file prior to extracting over it
-W, --verify
attempt to verify the archive after writing it
Select output stream:
--ignore-command-error ignore exit codes of children
--no-ignore-command-error
treat non-zero exit codes of children as error
-O, --to-stdout
extract files to standard output
--to-command=COMMAND
pipe extracted files to another program
Handling of file attributes:
--atime-preserve[=METHOD]
preserve access times on dumped files, either by restoring the times after reading (METHOD='replace'; default) or by not setting the
times in the first place (METHOD='system')
--delay-directory-restore
delay setting modification times and permissions of extracted directories until the end of extraction
--group=NAME
force NAME as group for added files
--mode=CHANGES
force (symbolic) mode CHANGES for added files
--mtime=DATE-OR-FILE
set mtime for added files from DATE-OR-FILE
-m, --touch
don't extract file modified time
--no-delay-directory-restore
cancel the effect of --delay-directory-restore option
--no-same-owner
extract files as yourself (default for ordinary users)
--no-same-permissions
apply the user's umask when extracting permissions from the archive (default for ordinary users)
--numeric-owner
always use numbers for user/group names
--owner=NAME
force NAME as owner for added files
-p, --preserve-permissions, --same-permissions
extract information about file permissions (default for superuser)
--preserve
same as both -p and -s
--same-owner
try extracting files with the same ownership as exists in the archive (default for superuser)
-s, --preserve-order, --same-order
sort names to extract to match archive
Handling of extended file attributes:
--acls Enable the POSIX ACLs support
--no-acls
Disable the POSIX ACLs support
--no-selinux
Disable the SELinux context support
--no-xattrs
Disable extended attributes support
--selinux
Enable the SELinux context support
--xattrs
Enable extended attributes support
--xattrs-exclude=MASK
specify the exclude pattern for xattr keys
--xattrs-include=MASK
specify the include pattern for xattr keys
Device selection and switching:
-f, --file=ARCHIVE
use archive file or device ARCHIVE
--force-local
archive file is local even if it has a colon
-F, --info-script=NAME, --new-volume-script=NAME
run script at end of each tape (implies -M)
-L, --tape-length=NUMBER
change tape after writing NUMBER x 1024 bytes
-M, --multi-volume
create/list/extract multi-volume archive
--rmt-command=COMMAND
use given rmt COMMAND instead of rmt
--rsh-command=COMMAND
use remote COMMAND instead of rsh
--volno-file=FILE
use/update the volume number in FILE
Device blocking:
-b, --blocking-factor=BLOCKS
BLOCKS x 512 bytes per record
-B, --read-full-records
reblock as we read (for 4.2BSD pipes)
-i, --ignore-zeros
ignore zeroed blocks in archive (means EOF)
--record-size=NUMBER
NUMBER of bytes per record, multiple of 512
Archive format selection:
-H, --format=FORMAT
create archive of the given format
FORMAT is one of the following:
gnu GNU tar 1.13.x format
oldgnu GNU format as per tar <= 1.12
pax POSIX 1003.1-2001 (pax) format
posix same as pax
ustar POSIX 1003.1-1988 (ustar) format
v7 old V7 tar format
--old-archive, --portability
same as --format=v7
--pax-option=keyword[[:]=value][,keyword[[:]=value]]...
control pax keywords
--posix
same as --format=posix
-V, --label=TEXT
create archive with volume name TEXT; at list/extract time, use TEXT as a globbing pattern for volume name
Compression options:
-a, --auto-compress
use archive suffix to determine the compression program
-I, --use-compress-program=PROG
filter through PROG (must accept -d)
-j, --bzip2
filter the archive through bzip2
-J, --xz
filter the archive through xz
--lzip filter the archive through lzip
--lzma filter the archive through lzma
--lzop
--no-auto-compress
do not use archive suffix to determine the compression program
-z, --gzip, --gunzip, --ungzip
filter the archive through gzip
-Z, --compress, --uncompress
filter the archive through compress
Note: You might need to install external program (lzip/ncompress/lzma...) to use some of these compression options
Local file selection:
--add-file=FILE
add given FILE to the archive (useful if its name starts with a dash)
--backup[=CONTROL]
backup before removal, choose version CONTROL
-C, --directory=DIR
change to directory DIR
--exclude=PATTERN
exclude files, given as a PATTERN
--exclude-backups
exclude backup and lock files
--exclude-caches
exclude contents of directories containing CACHEDIR.TAG, except for the tag file itself
--exclude-caches-all
exclude directories containing CACHEDIR.TAG
--exclude-caches-under exclude everything under directories containing
CACHEDIR.TAG
--exclude-tag=FILE
exclude contents of directories containing FILE, except for FILE itself
--exclude-tag-all=FILE exclude directories containing FILE
--exclude-tag-under=FILE
exclude everything under directories containing FILE
--exclude-vcs
exclude version control system directories
-h, --dereference
follow symlinks; archive and dump the files they point to
--hard-dereference
follow hard links; archive and dump the files they refer to
-K, --starting-file=MEMBER-NAME
begin at member MEMBER-NAME in the archive
--newer-mtime=DATE
compare date and time when data changed only
--no-null
disable the effect of the previous --null option
--no-recursion
avoid descending automatically in directories
--no-unquote
do not unquote filenames read with -T
--null -T reads null-terminated names, disable -C
-N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE
only store files newer than DATE-OR-FILE
--one-file-system
stay in local file system when creating archive
-P, --absolute-names
don't strip leading `/'s from file names
--recursion
recurse into directories (default)
--suffix=STRING
backup before removal, override usual suffix ('~' unless overridden by environment variable SIMPLE_BACKUP_SUFFIX)
-T, --files-from=FILE
get names to extract or create from FILE
--unquote
unquote filenames read with -T (default)
-X, --exclude-from=FILE
exclude patterns listed in FILE
- 解压: 解压压缩文件到指定目录
tar -xvf <dirName/compressedFile>.tar.gz -C <targetDir> # 解出 compressedFile.tar.gz 包中所有文件
-x, --extract, --get 解压,从归档中【解出】文件
-c, --create create a new archive 压缩,创建一个压缩文件
-C, --directory=DIR change to directory DIR 改变至目录 DIR
-v, --verbose 详细地列出处理的文件
-f, --file=ARCHIVE 使用归档文件或 ARCHIVE 设备
-z, --gzip, --gunzip, --ungzip 通过 gzip 过滤归档
-j, --bzip2 filter the archive through bzip2 通过 bzip2 过滤压缩文件
tar -xvf <dirName/compressedFile>.tar -C <targetDir> # 解出 compressedFile.tar 包中所有文件
tar -xjf <dirName/compressedFile>.tar.bz2 -C <targetDir> # 解压 compressedFile.tar.bz2 的文件
- 压缩
(↓方式1↓ 推荐)
tar -czvf fileName.tar.gz dirName
# 对 dirName 文件(夹) 【打包(-c)】,且【压缩(-z)】 为 文件名称为 fileName.tar.gz 的压缩包
-c, --create create a new archive 压缩,创建一个压缩文件
-v, --verbose 详细地列出处理的文件
-f, --file=ARCHIVE 使用归档文件或 ARCHIVE 设备
-z, --gzip, --gunzip, --ungzip 通过 gzip 过滤归档

(↓方式2↓)
tar -cvf fileName.tar.gz dirName
# 对 dirName 文件(夹) 【打包(-c)】,且【压缩(-z)】 为 文件名称为 fileName.tar.gz 的压缩包
-c, --create create a new archive 压缩,创建一个压缩文件
-v, --verbose 详细地列出处理的文件
-f, --file=ARCHIVE 使用归档文件或 ARCHIVE 设备

2 zip / unzip
- 解压
unzip <dirName/compressedFile>.zip -d <targetDir>
unzip -o /opt/sdc/ue/tomcat/portal.zip -d /opt/sdc/ue/tomcat/webapps
unzip -O CP936 xxx.zip 或者 zip -O CP936 -o xxx.zip
CP936 -- GBK: IBM在发明Code Page的时候将GBK编码集放在第936页,所以叫CP936
UTF8 -- UTF-8(8-bit Unicode Transformation Format)/ 一种针对Unicode的可变长度字符编码
- 压缩
zip -v filename.zip a.c b.c c.c e.c # 压缩文件
zip -r <dirName/compressedFile>.zip <targetDir> # 压缩指定目录的文件
zip -r filename.zip file1 file2 file3 <targetDir> # 压缩多个文件或目录
3 gzip / gunzip
GNU zip
gunzip是gzip的硬链接,请参考gzip
- 解压
gunzip -c abc.sql.gz > abc.sql # 解压到指定目录,保留源文件,-c
gzip -dv 压缩文件 # 解压并打印执行过程
- 压缩
gzip -c 待压缩文件 > 带压缩文件.gz # 压缩文件并保留源文件
X 参考文献
[Linux]常用命令之【tar/zip/unzip/gzip/gunzip】的更多相关文章
- linux常用命令:tar 命令
通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar 命令可以为linu ...
- linux常用命令(21)tar命令
通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar命令可以为linux ...
- Linux 常用命令九 tar
一.tar命令 tar命令用于打包,解包. gzip命令用于压缩,解压缩. bzip2命令用于压缩,解压缩. 这三个是在linux中常用的,还有一些不常用的. tar打包: wang@wang:~/w ...
- 2017-07-07(zip unzip gzip gunzip)
zip压缩格式 zip zip 压缩文件名 源文件 (压缩文件) zip -r 压缩文件名 源文件 (压缩目录) unzip unzip 压缩名 .gz压缩格式 gzip gz ...
- linux常用命令加实例大全
目 录引言 1一.安装和登录 2(一) login 2(二) shutdown 2(三) halt 3(四) reboot 3(五) ...
- 2020非常全的软件测试linux常用命令全集,linux面试题及参考答案
一.前言: 作为一名软件测试工程师,我相信大部分的人都和Linux打过交道,因为我们的服务器一般都是装的Linux操作系统,包括各种云服务器也都是用的Linux,目前主流是CentOS7,那么对于一个 ...
- linux常用命令与实例小全
转至:https://www.cnblogs.com/xieguohui/p/8296864.html linux常用命令与实例小全 阅读目录(Content) 引言 一.安装和登录 (一) ...
- Linux常用命令(三)
1.top 说明:即时显示 process 的动态 语法格式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b]基本参数:d : 改变显示的更新速度,或是在交谈 ...
- linux常用命令详解
Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命令.要想真正理解Linux系统, ...
- linux常用命令详解 (一) 安装和登录命令
Linux必学的60个命令 Linux提供了大量的命令,利用它可以有效地完成大量的工 作,如磁盘操作.文件存 Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取. ...
随机推荐
- 基于Docker搭建Redis集群(主从集群)
基于Docker搭建Redis集群(主从集群) 最近陆陆续续有不少园友加我好友咨询 redis 集群搭建的问题,我觉得一定是之前写的这篇 <基于Docker的Redis集群搭建> 文章 ...
- n-Queens(n皇后)问题的简单回溯
package com.main; import java.util.LinkedList; public class NoQueue { public LinkedList<Node> ...
- Java的流程控制
Scanner对象 next(); 一定要读取到有效字符后才可以结束输入. 对输入有效字符之前遇到的空白,next()方法会自动将其去掉(). 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束 ...
- ABP vNext微服务架构详细教程——项目部署
1. 基础配置 在之前的文章中,我们已经配置了Kubernetes集群并安装了管理工具Kubesphere,文章地址为:https://mp.weixin.qq.com/s/MgpdMv5A-fYxN ...
- 博弈论练习6 Deleting Divisors(sg找规律,思维)
题目链接在这里:G-Deleting Divisors_牛客竞赛博弈专题班组合游戏基本概念.对抗搜索.Bash游戏.Nim游戏习题 (nowcoder.com) 这道题一道比较明显的思路是使用sg函数 ...
- ubuntu 16.04 安装VNC远程桌面 安装wine+hfs
1.安装$sudo apt-get install xfce4 $sudo apt-get install vnc4server$sudo apt-get install xrdp 2.启动VNC s ...
- MQ(为什么要使用MQ)
为什么使用MQ? 个人认为主要由几下几点: 1.在高并发环境下,由于来不及同步处理,请求往往会发生堵塞,比如说,大量的insert,update之类的请求同时到达数据库,直接导致无数的行锁表锁,甚至最 ...
- Windows Oracle 启动OracleDBConsoleorcl 服务时报 Window不能在 本地计算机启动 OracleDBConsoleorcl 。有关更多信息,查阅系统事件日志。如果这是非Microsoft服务,请与服务厂商联系,并参考特定服务错误代码 2 。
一.报错信息如图 二.原因分析: 计算机主机名或IP地址有变化 三.解决方法 依次输入命令: Microsoft Windows [版本 10.0.22000.795](c) Microsoft Co ...
- .NET周报 【3月第2期 2023-03-12】
国内文章 ASP.NET Core中如何限制响应发送速率(不是调用频率) https://www.cnblogs.com/coredx/p/17195492.html ASP.NET Core中有很多 ...
- 说来惭愧,关于Session的某块知识的学习,感觉涨了知识
Session的查漏补缺 今天在写界面的时候,想要利用servlet和jsp页面实现界面的跳转,之前实现这些内容的时候,我是没有用到session来实现这个功能的. 直到今天,想要将第一个界面的数据隔 ...