linux删除被打包压缩的原始文件在哪,Linux运维:压缩与解压

只有光头才能变强。2019-3-2

Linux下有许多压缩与解压缩命令,列举一些。

gzip

压缩:gzip +filename 如gzip test.txt

压缩后格式:压缩后文件名为filename.gz,上面的文件压缩后就是test.txt.gz

解压缩:gzip -d test.txt.gz也可以使用gunzip test.txt.gz,gunzip == gzip -d

压缩级别(1-9):默认6级别,(压缩级别表示压缩比例和压缩速度),gzip -1 test.txt表示使用级别1压缩(压缩比例最少,速度最快)

查看压缩包文件:zcat test.txt.gz

执行权限:所有用户

注意:只能压缩文件,不能压缩目录,压缩后不保存原文件

[root@moli-04 test]# ls

test.txt

[root@moli-04 test]# cat test.txt

hello world

[root@moli-04 test]# gzip test.txt

[root@moli-04 test]# ls

test.txt.gz

[root@moli-04 test]# zcat test.txt.gz

hello world

[root@moli-04 test]# gzip -d test.txt.gz

[root@moli-04 test]# ls

test.txt

bzip2

大多数情况下,bzip2压缩的文件比gzip的大小更小,效率更高。

压缩命令:bzip2 + filename

压缩后格式:压缩后的文件名在后加.bz2

查看压缩文件内容:bzcat filename.bz2

解压:bzip2 -d filename.bz2

压缩级别:默认为9

不可压缩目录

[root@moli-04 test]# ls

test.txt

[root@moli-04 test]# bzip2 test.txt

[root@moli-04 test]# ls

test.txt.bz2

[root@moli-04 test]# bzcat test.txt.bz2

hello world

[root@moli-04 test]# bzip2 -d test.txt.bz2

[root@moli-04 test]# ls

test.txt

zip

Centos7默认是没有zip和unzip命令的,需要手动安装:yum install -y zip unzip

压缩:zip test.txt.zip test.txt zip压缩需要指定压缩后的文件名,而且压缩后源文件依旧存在

解压缩:unzip test.txt.zip 解压时会询问是否覆盖源文件,解压前如果有源文件,建议改名字或者挪到位置,比如将test.txt.zip解压到/data目录下,uzip test.txt.zip /data

zip 可以压缩目录:压缩目录时需要加上选项-r 。如:zip -r data.zip /tmp/data

xz

压缩:xz test.txt

压缩后格式:filename.xz

解压xz -d test.txt.xz

查看压缩文件 xzcat test.txt.xz

压缩级别:默认是6

[root@moli-04 test]$ ls

test.txt

[root@moli-04 test]$ xz test.txt

[root@moli-04 test]$ ls

test.txt.xz

[root@moli-04 test]$ xz -d test.txt.xz

[root@moli-04 test]$ ls

test.txt

tar

打包与解包文件,用法:tar 模式 \[选项\]\[路径\]...

模式

说明

-c

创建打包文件

--delete

从打包文件中删除文件

-r

追加文件至打包文档

-t

列出打包文档的内容

-x

释放打包文件

选项

说明

-C

指定解压路径

-f

指定打包后的文件名称

-j

打包后通过bzip2格式压缩

--remove-files

打包后删除源文件

-z

打包后通过gzip格式压缩

--exclude files

打包目录时过滤掉目录下的files文件

示例1:把1.txt 2.txt 3.txt打成一个包叫num.txt.gz

[root@moli-04 test]# ls

1.txt 2.txt 3.txt 4.txt 5.txt test.txt

[root@moli-04 test]# tar -cvf num.txt.gz 1.txt 2.txt 3.txt

1.txt

2.txt

3.txt

[root@moli-04 test]# ls

1.txt 2.txt 3.txt 4.txt 5.txt num.txt.gz test.txt

示例2:将刚才打的包num.txt.gz解包,并且解压到/tmp目录下

解压包,源文件不会被删除

[root@moli-04 test]$ tar -xvf num.txt.gz -C /tmp

1.txt

2.txt

3.txt

示例3:把test目录打包,排除test目录下的文件5.txt

[root@moli-04 test]$ pwd

/tmp/test

[root@moli-04 test]$ ls

1.txt 2.txt 3.txt 4.txt 5.txt num.txt.gz test.txt

[root@moli-04 test]$ cd ..

[root@moli-04 tmp]$ tar --exclude 5.txt -cvf test.tar test

test/

test/test.txt

test/1.txt

test/2.txt

test/3.txt

test/4.txt

test/num.txt.gz

示例4:查看打包文档中的文件信息

[root@moli-04 tmp]$ tar -tf test.tar

test/

test/test.txt

test/1.txt

test/2.txt

test/3.txt

test/4.txt

test/num.txt.gz

示例5:查看打包文档中详细地文档信息

[root@moli-04 tmp]$ tar -tvf test.tar

drwxr-xr-x root/root 0 2019-03-02 22:05 test/

-rw-r--r-- root/root 12 2019-03-02 16:40 test/test.txt

-rw-r--r-- root/root 10240 2019-03-02 22:04 test/1.txt

-rw-r--r-- root/root 0 2019-03-02 22:03 test/2.txt

-rw-r--r-- root/root 0 2019-03-02 22:03 test/3.txt

-rw-r--r-- root/root 0 2019-03-02 22:03 test/4.txt

-rw-r--r-- root/root 20480 2019-03-02 22:05 test/num.txt.gz

示例6:从打包文档中追加文件grep.txt

[root@moli-04 tmp]$ tar -f test.tar -r grep.txt

[root@moli-04 tmp]$ tar -tf test.tar

test/

test/test.txt

test/1.txt

test/2.txt

test/3.txt

test/4.txt

test/num.txt.gz

grep.txt

示例7:从打包文档中删除文件grep.txt

[root@moli-04 tmp]$ tar --delete grep.txt -f test.tar

[root@moli-04 tmp]$ tar -tf test.tar

test/

test/test.txt

test/1.txt

test/2.txt

test/3.txt

test/4.txt

test/num.txt.gz

示例8:解压gz格式的压缩包至当前目录

$ tar -zxvf test.gz

示例9:解压bz2格式的压缩包至当前目录

$ tar -xjvf test.bz2

示例10:打包压缩后删除源文件

$ tar -czf mess.tar.gz /var/log/messages --remove-files

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值