小白的python之路10/29 文件归档

一打包解包文件

[root@localhost ~]# cd /test/
[root@localhost test]# touch a.txt b.txt c.txt
[root@localhost test]# ls
a.txt  b.txt  c.txt

#1.打包过程
#c表示创建,v表示过程信息,f表示文件名, test.tar表示打包后的文件名,文件后缀无意义
[root@localhost test]# tar cvf test.tar a.txt b.txt c.txt
a.txt
b.txt
c.txt
[root@localhost test]# ls
a.txt  b.txt  c.txt  test.tar

#2查看过程t表示查看,f指定哪个文件
[root@localhost test]# tar -tf test.tar
a.txt
b.txt
c.txt
[root@localhost test]# ls
a.txt  b.txt  c.txt  test.tar
[root@localhost test]# rm -rf a.txt b.txt c.txt
[root@localhost test]# ls
test.tar

#3 x表示解包 f表示指定文件
[root@localhost test]# tar xvf test.tar
a.txt
b.txt
c.txt
[root@localhost test]# ls
a.txt  b.txt  c.txt  test.tar
[root@localhost test]# mkdir /bak
[root@localhost test]# ls /bak
[root@localhost test]# ls
a.txt  b.txt  c.txt  test.tar

#4 大c表示指定路径(绝对路径)
[root@localhost test]# tar xvf /test/test.tar -C /bak
a.txt
b.txt
c.txt
[root@localhost test]# ls /bak
a.txt  b.txt  c.txt
#二 压缩文件的压缩和解压

#vim写入信息
[root@localhost test]# vim a.txt

#查看文件信息 文件大小59
[root@localhost test]# ll a.txt
-rw-r--r-- 1 root root 59 10月 29 20:51 a.txt

#压缩文件gzip   文件大小39
[root@localhost test]# gzip a.txt
[root@localhost test]# ll
总用量 4
-rw-r--r-- 1 root root 39 10月 29 20:51 a.txt.gz
[root@localhost test]# ll a.txt.gz
-rw-r--r-- 1 root root 39 10月 29 20:51 a.txt.gz

#解压文件
[root@localhost test]# gunzip a.txt
[root@localhost test]# ls
a.txt
[root@localhost test]# touch b.txt c.txt d.txt
[root@localhost test]# ls
a.txt  b.txt  c.txt  d.txt

#打包
[root@localhost test]# tar cvf test.tar a.txt b.txt c.txt d.txt
a.txt
b.txt
c.txt
d.txt
[root@localhost test]# ls
a.txt  b.txt  c.txt  d.txt  test.tar

#压缩
[root@localhost test]# gzip test.tar
[root@localhost test]# ls
a.txt  b.txt  c.txt  d.txt  test.tar.gz
#打包时压缩
[root@localhost test]# tar cvzf test1.tar.gz a.txt b.txt c.txt d.txt
a.txt
b.txt
c.txt
d.txt
[root@localhost test]# ls
a.txt  b.txt  c.txt  d.txt  test1.tar.gz  test.tar.gz
[root@localhost test]# ll -l test1.tar.gz
-rw-r--r-- 1 root root 164 10月 29 21:14 test1.tar.gz
[root@localhost test]# ll -l test.tar.gz
-rw-r--r-- 1 root root 173 10月 29 21:00 test.tar.gz

[root@localhost test]# ls
a.txt  b.txt  c.txt  d.txt  test1.tar.gz  test.tar.gz
[root@localhost test]# touch e.txt
[root@localhost test]# bzip2 e.txt
[root@localhost test]# ls
a.txt  b.txt  c.txt  d.txt  e.txt.bz2  test1.tar.gz  test.tar.gz

#压缩文件bz2
[root@localhost test]# tar cvjf test2.tar.bz2 a.txt b.txt c.txt
a.txt
b.txt
c.txt
#2种归档文件无论是bzip2还是gzip解压都一样

[root@localhost test]# mkdir /test1
[root@localhost test]# mkdir /test2
[root@localhost test]# tar xf test1.tar.gz -C /test1/
#解压后包中信息

[root@localhost test]# ls /test1/
a.txt  b.txt  c.txt  d.txt

[root@localhost test]# tar xf test2.tar.bz2 -C /test2/
[root@localhost test]# ls /test2
a.txt  b.txt  c.txt
#bz2比gz压缩更小  但是bz2速度慢,过程久

 

eg:对目录/etc/下文件夹的压缩

[root@localhost test]# tar cvzf etc.tar.gz /etc/
[root@localhost test]# ls
a.txt  c.txt  etc.tar.gz  test1.tar.gz   test.tar.gz
b.txt  d.txt  e.txt.bz2   test2.tar.bz2
    mkdir /conf_bak

打包目录,结果中有目录名存在    tar cvzf test.tar.gz /etc

                cd /etc

               tar cvzf test,tar,gz*

 

 

 

 

 

 

补充

>将左边输出到右面eg:cat /etc/passwd > /tmp/passswd.bak

  less随意浏览文件less /tmp/passswd.bak

Ctrl+c强制中断进程   CTRL + D  保存退出

useradd  用户 -del   mkdir 文件夹  rm

 

转载于:https://www.cnblogs.com/yushengqingzhijiaoshang/p/7751720.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值