马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
压缩命令应用场景tar大部门利用tar即可。创建,检察,解压,解压到指定目次gzip一般共同其他命令利用zip+unzip一般用于解压zip格式的压缩包 1. tar 打包压缩命令
1.1 命令详解
【功能阐明】
在Linux体系里,tar是将多个文件打包在一起,并且可以实现解压打包的文件的命令。是体系管理员最常用的命令之一,tar命令不但可以实现对多个文件进行打包,还可以对多个文件打包后进行压缩。
打包是指将一大堆文件或目次变成一个总的文件,压缩则是将一个大的文件通过一些压缩算法变成一个小文件。
【语法格式】
- tar [option] [file]
- tar [选项] [文件或目录]
复制代码 【选项阐明】
参数选项解释阐明z通过gzip压缩或解压c创建新的tar包v显示详细的tar命令执行过程f指定压缩文件的名字t不解压检察tar包的内容p保持文件的原有属性P(大写)以绝对路径打包,伤害参数j通过bzip2命令压缩或解压x解开tar包C指定解压的目次路径–exclude=PATTERN打包时排除不需要处理的文件或目次-X file从指定文件读取不需要处理的文件或目次列表-N date仅打包比指定日期新的文件,可用于增量打包备份-h打包软链接文件指向的真实源文件–hard-dereference打包硬链接文件 1.2 利用范例
tar命令主要的用途是:
- 创建压缩包
- 检察压缩包
- 解压压缩包
- 解压压缩包到指定目次
tar命令选项与阐明创建 zcf/zcvftar zcf 压缩包 被压缩的文件/目次检察 tf/ztftar tf /tmp/etc.tar.gz纵然不指定z选项,tar命令也会自动判定压缩包类型,自动调用gzip解压 xf/xvftar xf /tmp/etc.tar.gz- # 案例1.打包当前的1.txt文件为test.tar.gz
- [root@localhost ~]# echo aaa > 1.txt
- [root@localhost ~]# ll
- total 4
- -rw-r--r-- 1 root root 4 Nov 10 12:19 1.txt
- [root@localhost ~]# cat 1.txt
- aaa
- [root@localhost ~]# tar zcvf test.tar.gz 1.txt
- 1.txt
- [root@localhost ~]# ll
- total 8
- -rw-r--r-- 1 root root 4 Nov 10 12:19 1.txt
- -rw-r--r-- 1 root root 118 Nov 10 12:20 test.tar.gz
- # 案例2.同时打包多个文件
- #打包/etc/hosts 1.txt /etc/passwd
- [root@localhost ~]# tar zcvf test.tar.gz /etc/hosts /etc/passwd 1.txt
- tar: Removing leading `/' from member names
- /etc/hosts
- /etc/passwd
- 1.txt
- # 注意: 打包建议到相对路径
- [root@localhost ~]# tar zcvf dir.tar.gz etc test
- etc/
- etc/hosts
- etc/passwd
- test/
- [root@localhost ~]# ll
- total 12
- -rw-r--r-- 1 root root 4 Nov 10 12:19 1.txt
- -rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
- drwxr-xr-x 2 root root 33 Nov 10 12:24 etc
- drwxr-xr-x 2 root root 6 Nov 10 12:26 test
- # 案例3.解压test.tar.gz 到当前目录
- [root@localhost ~]# mv test.tar.gz /opt/
- [root@localhost ~]# cd /opt/
- [root@localhost opt]# tar xf test.tar.gz
- [root@localhost opt]# ll
- total 8
- -rw-r--r-- 1 root root 4 Nov 10 12:19 1.txt
- drwxr-xr-x 2 root root 33 Nov 10 12:28 etc
- -rw-r--r-- 1 root root 675 Nov 10 12:22 test.tar.gz
- [root@localhost opt]# ll etc/
- total 8
- -rw-r--r-- 1 root root 158 Nov 3 10:25 hosts
- -rw-r--r-- 1 root root 1017 Oct 10 2008 passwd
- # 案例4.指定解压到某个位置-C
- [root@localhost ~]# tar xf 1.tar.gz -C /opt/
- # 案例5.压缩包在/root下 想在/opt下解压
- [root@localhost ~]# ll
- total 4
- -rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
- [root@localhost ~]# cd /opt/
- [root@localhost opt]# rm -rf *
- [root@localhost opt]# ll
- total 0
- [root@localhost ~]# ll
- total 4
- -rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
- [root@localhost ~]# cd /opt/
- [root@localhost opt]# rm -rf *
- [root@localhost opt]# ll
- total 0
- [root@localhost opt]# ll
- total 0
- [root@localhost opt]# tar xf /root/dir.tar.gz
- [root@localhost opt]# ll
- total 0
- drwxr-xr-x 2 root root 33 Nov 10 12:24 etc
- drwxr-xr-x 2 root root 6 Nov 10 12:26 test
复制代码 2. gzip 压缩或解压文件
2.1 命令详情
【功能阐明】
gzip命令用于将一个大的文件通过压缩算法 (Lempel-Ziv coding (LZ77))变成一个小的文件。gzip命令不能直接压缩目次,因此目次需要先用tar打包成一个文件,然后tar再调用gzip进行压缩。
【语法格式】
- gzip [option] [file]
- gzip [选项] [文件]
复制代码 【选项阐明】
参数选项解释阐明-d解开压缩文件-v显示指令执行的过程-l列出压缩文件的内容信息-c将内容输出到标准输出,不改变原始文件-r对目次下的所有文件递归进行压缩操纵-n (n这里代指数字,范围1~9)指定压缩率,默认为6,值越大压缩率越高-t测试,检查压缩文件是否完备 2.2 利用范例
- [root@localhost test]# ls
- 1.txt 2.txt 3.html 4.html
- [root@localhost test]# gzip *.txt
- [root@localhost test]# ls
- 1.txt.gz 2.txt.gz 3.html 4.html
- # ".gz"后缀是gzip命令自动添加的。
- # gzip命令的缺点是压缩后源文件不见了,它的特性是压缩、解压都会自动删除源文件。
- # 不解压显示每个压缩文件的信息。使用-l参数
- # 压缩率为0是因为,源文件为空文件。
- [root@localhost test]# gzip -l *.txt.gz
- compressed uncompressed ratio uncompressed_name
- 26 0 0.0% 1.txt
- 26 0 0.0% 2.txt
- 122 16429 99.4% 5.txt
- 174 16429 99.1% (totals)
- # 实际解压文件
- [root@localhost test]# gzip -dv *.txt.gz
- 1.txt.gz: 0.0% -- replaced with 1.txt
- 2.txt.gz: 0.0% -- replaced with 2.txt
- 5.txt.gz: 99.4% -- replaced with 5.txt
- # -d 解压缩;-v 显示过程
- [root@localhost test]# ls
- 1.txt 2.txt 3.html 4.html 5.txt
- # 查看解压后的结果,gz文件不存在了,只剩下txt文件。
复制代码 3. zip 打包和压缩文件
3.1 命令详解
【功能阐明】
zip压缩格式是 Windows 与 Linux等多平台通用的压缩格式。和gzip命令相比,zip命令压缩文件不但不会删除源文件,而且还可以压缩目次。
【语法格式】
- zip [option] [file]
- zip [选项] [文件或目录]
复制代码 【选项阐明】
参数选项解释阐明-r递归压缩,将指定目次下的所有文件和子目次一并压缩-x压缩文件时排除某个文件-q不显示压缩信息 3.2 利用范例
- # 压缩文件1.txt和2.txt
- [root@localhost test]# ls
- 1.txt 2.txt 3.html 4.html 5.txt
- [root@localhost test]# zip 1-2.zip 1.txt 2.txt
- adding: 1.txt (stored 0%)
- adding: 2.txt (stored 0%)
- [root@localhost test]# ls
- 1-2.zip 1.txt 2.txt 3.html 4.html 5.txt
- # 源文件依然存在
- # 压缩目录
- [root@localhost ~]# zip -r test.zip test/
- adding: test/ (stored 0%)
- adding: test/2.txt (stored 0%)
- adding: test/1.txt (stored 0%)
- adding: test/3.html (stored 0%)
- adding: test/1-2.zip (stored 0%)
- adding: test/5.txt (deflated 99%)
- adding: test/4.html (stored 0%)
- [root@localhost ~]# ls
- test test.zip
- # 排除压缩,指定某些文件不进行压缩
- [root@localhost ~]# zip -r test.zip test/ -x test/5.txt
- adding: test/ (stored 0%)
- adding: test/2.txt (stored 0%)
- adding: test/1.txt (stored 0%)
- adding: test/3.html (stored 0%)
- adding: test/1-2.zip (stored 0%)
- adding: test/4.html (stored 0%)
复制代码 4. unzip 解压zip文件
4.1 命令详解
【功能阐明】
unzip命令可以解压zip命令或其他压缩软件压缩的zip格式的文件。
【语法格式】
- unzip [option] [fie]
- unzip [选项] [压缩文件]
复制代码 【选项阐明】
参数选项解释阐明-l不解压显示压缩包内容-d指定解压目次-o解压时不提示是否覆盖文件-v解压时显示详细信息 4.2 利用范例
- # 查看压缩文件信息
- [root@localhost ~]# ls
- test test.zip
- [root@localhost ~]# unzip -l test.zip
- Archive: test.zip
- Length Date Time Name
- --------- ---------- ----- ----
- 0 01-04-2024 13:35 test/
- 0 12-29-2023 12:55 test/2.txt
- 0 12-29-2023 12:55 test/1.txt
- 0 01-04-2024 11:17 test/3.html
- 298 01-04-2024 13:35 test/1-2.zip
- 0 01-04-2024 11:17 test/4.html
- --------- -------
- 298 6 files
- # 直接进行源路径解压,会提示是否覆盖源文件
- [root@localhost ~]# unzip test.zip
- Archive: test.zip
- replace test/2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
- extracting: test/2.txt
- ......
- replace test/4.html? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
- extracting: test/4.html
- [root@localhost ~]# ls
- test test.zip
- # 解压时不进行提示是否覆盖
- [root@localhost ~]# unzip -o test.zip
- Archive: test.zip
- extracting: test/2.txt
- extracting: test/1.txt
- extracting: test/3.html
- extracting: test/1-2.zip
- extracting: test/4.html
- # 指定解压目录解压文件
- [root@localhost ~]# unzip -d test/ test.zip
- Archive: test.zip
- creating: test/test/
- extracting: test/test/2.txt
- extracting: test/test/1.txt
- extracting: test/test/3.html
- extracting: test/test/1-2.zip
- extracting: test/test/4.html
- [root@localhost ~]# tree
- .
- ├── test
- │ ├── 1-2.zip
- │ ├── 1.txt
- │ ├── 2.txt
- │ ├── 3.html
- │ ├── 4.html
- │ ├── 5.txt
- │ └── test
- │ ├── 1-2.zip
- │ ├── 1.txt
- │ ├── 2.txt
- │ ├── 3.html
- │ └── 4.html
- └── test.zip
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |