tar命令压缩与解压缩

tar命令压缩与解压缩

tar [OPTION...] [FILE]...

tar常用参数

  • z filter the archive through gzip

  • c create a new archive

  • v verbosely list files processed

  • f use archive file or device ARCHIVE

  • x extract files from an archive

  • C change to directory DIR

  • P extract information about file permissions (default for superuser)

  • j filter the archive through bzip2

    ####

tar常用方式

zcvf 创建压缩tar.gz
[root@gsh ~]# tar zcvf test.tar.gz ./test
./test/
./test/1.txt
./test/2.txt
./test/3.txt
[root@gsh ~]#
xf 解压缩文件
[root@gsh ~]# tar xf test.tar.gz 
[root@gsh ~]# tar xvf test.tar.bz2
[root@gsh ~]# ll
total 86880
-rw-r--r--. 1 root root        6 Sep 22 10:27 1
dr--r--r--. 2 root root        6 Sep 22 16:08 333dir
-r--r--r--. 1 root root        0 Sep 22 16:07 333.txt
-rw-------. 1 root root 58687965 Sep 26 19:36 initramfs-0-rescue-62a5d1b1d99b43dda6e521dec7b1537d.img
-rw-------. 1 root root 18930680 Sep 26 19:36 initramfs-3.10.0-1127.el7.x86_64.img
-rw-------. 1 root root 11291658 Sep 26 19:36 initramfs-3.10.0-1127.el7.x86_64kdump.img
-rw-r--r--. 1 root root     3210 Sep 25 19:35 ls.1.gz
-rw-r--r--. 1 root root       23 Sep 22 15:02 passwd.txt
drwxr-xr-x. 2 root root       45 Sep 27 15:32 test
tf 不解压查看tar.gz包含文件
[root@gsh ~]# tar tf test.tar.gz 
./test/
./test/1.txt
./test/2.txt
./test/3.txt
[root@gsh ~]#
-C 将文件解压到对应的路径
[root@gsh ~]# tar xvf test.tar.gz -C /test/
./test/
./test/1.txt
./test/2.txt
./test/3.txt
[root@gsh ~]# ll /test
total 0
drwxr-xr-x. 2 root root 45 Sep 27 15:32 test
[root@gsh ~]#
jcvf 创建bzip2格式的压缩包
[root@gsh ~]# tar jcvf test.tar.bz2 ./test
./test/
./test/1.txt
./test/2.txt
./test/3.txt
[root@gsh ~]# ll
total 86884
-rw-r--r--. 1 root root        6 Sep 22 10:27 1
dr--r--r--. 2 root root        6 Sep 22 16:08 333dir
-rw-r--r--. 1 root root      164 Sep 27 15:44 test.tar.bz2
-rw-r--r--. 1 root root      156 Sep 27 15:33 test.tar.gz
[root@gsh ~]#
P 打包过程中使用绝对路径不会出现告警
[root@gsh ~/test]# tar zcvf test.tar.gz /root/test/test/
tar: Removing leading `/' from member names
/root/test/test/
/root/test/test/1.txt
/root/test/test/2.txt
/root/test/test/3.txt
[root@gsh ~/test]# tar zcvfP test.tar.gz /root/test/test/
/root/test/test/
/root/test/test/1.txt
/root/test/test/2.txt
/root/test/test/3.txt
[root@gsh ~/test]#

 

posted @ 2020-09-27 15:56  Gsh-123  阅读(445)  评论(0)    收藏  举报