tar打包压缩与解压使用

1.tar 压缩打包/root/Desktop整个目录,并把打包的文件放到/root/backup目录下
[root@localhost ~]# tar -czvf /root/backup/desktop.tar.gz Desktop/
Desktop/
Desktop/abc/
Desktop/abc/tff.txt
Desktop/abc/1.txt
Desktop/abc/abc.log
Desktop/def/
Desktop/def/222.txt
Desktop/def/333.txt
Desktop/def/111.txt

2.tar 压缩打包/root/Desktop整个目录,并排除def目录
root@localhost ~]# tar -czvf /root/backup/desktop2.tar.gz Desktop/ --exclude=Desktop/def             #Desktop/def 不能写成Desktop/def/
Desktop/
Desktop/abc/
Desktop/abc/tff.txt
Desktop/abc/1.txt
Desktop/abc/abc.log

3.tar压缩打包/root/Desktop整个目录,并排除打包所有txt文件
[root@localhost ~]# tar -czvf /root/backup/desktop3.tar.gz Desktop/ --exclude=*.txt
Desktop/
Desktop/abc/
Desktop/abc/abc.log
Desktop/def/

4.tar 解压到指定目录,如把/root/backup/desktop2.tar.gz 解压到/root/kang 目录下
[root@localhost backup]# tar -zxvf desktop2.tar.gz -C /root/kang/
Desktop/
Desktop/abc/
Desktop/abc/tff.txt
Desktop/abc/1.txt
Desktop/abc/abc.log
[root@localhost backup]# cd /root/kang/
[root@localhost kang]# ll
total 24
drwxr-xr-x 3 root root 4096 May  7 20:58 Desktop
-rw-r--r-- 1 root root    0 May  6 21:21 d.txt
drwxr-xr-x 2 root root 4096 May  6 21:14 kang
-rw-r--r-- 1 root root    0 May  5 18:55 kang.txt
drwxr-xr-x 2 root root 4096 May  5 23:40 nginx
-rw-r--r-- 1 root root  111 May  7 13:41 nginx.conf
-rw-r--r-- 1 root root    5 May  5 23:40 test.sh
-rw-r--r-- 1 root root   38 May  5 23:27 test.txt

5.查看压缩包里的内容
[root@localhost backup]# tar tf desktop2.tar.gz 
Desktop/
Desktop/abc/
Desktop/abc/tff.txt
Desktop/abc/1.txt
Desktop/abc/abc.log

6.打包的文件自动加上日期
[root@localhost ~]# tar -zcvf kang_`date +%F`.tar.gz kang/
[root@localhost ~]# ls | grep kang
kang
kang_2018-05-12.tar.gz