tar命令的一些常用参数举例


tar命令用来将多个文件归档成一个文件。用法为:tar [选项] [文件]

[root@osker ~]# tar --help
Usage: tar [OPTION...] [FILE]...
GNU `tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.
...

GUN官方文档:https://www.gnu.org/software/tar/manual/tar.pdf


测试环境

[root@backup ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@backup ~]# uname -r
3.10.0-957.el7.x86_64
[root@backup ~]# rpm -qa tar
tar-1.26-35.el7.x86_64

 




1.打包并压缩

[root@osker ~]# tar -czvf ./osker.tar.gz /osker/*
tar: Removing leading `/' from member names
/osker/foo1.txt
/osker/foo2.txt
/osker/foo3.txt
[root@osker ~]# ll
-rw-r--r-- 1 root root 135 Apr 12 20:03 osker.tar.gz

-c, --create    create a new archive
-c或--create    建立新的备份文件

-z, --gzip, --gunzip, --ungzip    filter the archive through gzip
-z或--gzip或--ungzip            通过gzip指令处理备份文件

-j或--bzip2    支持bzip2解压文件
-J或--xz    支持xz解压文件

-v, --verbose    verbosely list files processed
-v或--verbose    显示指令执行过程  

-f, --file=ARCHIVE        use archive file or device ARCHIVE
-f或--file=备份文件        指定备份文件

 
 
2.查看压缩包  

[root@osker ~]# tar -tf osker.tar.gz
osker/foo1.txt
osker/foo2.txt
osker/foo3.txt

-t, --list    list the contents of an archive
-t或--list    列出备份文件的内容



3.解压

[root@osker ~]# tar -xf osker.tar.gz
[root@osker ~]# tree ./osker
./osker
├── foo1.txt
├── foo2.txt
└── foo3.txt
0 directories, 3 files
#使用-C,来指定解压的位置
[root@osker ~]# tar -xf osker.tar.gz -C /opt/
[root@osker ~]# tree /opt
/opt
└── osker
    ├── foo1.txt
    ├── foo2.txt
    └── foo3.txt
1 directory, 3 files

-x, --extract, --get    extract files from an archive
-x或--extract或--get    从备份文件中还原文件

-C, --directory=DIR    change to directory DIR
-C <目录>            解压到指定的目录
 
 
 
4.打包压缩时候排除某些文件

详见链接:https://www.cnblogs.com/osker/p/12427447.html
需使用以下参数:
    --exclude=PATTERN        exclude files, given as a PATTERN
-X, --exclude-from=FILE        exclude patterns listed in FILE
-T, --files-from=FILE        get names to extract or create from FILE

[root@localhost ~]# cat tar-include
/root
[root@localhost ~]# cat tar-exclude
/root/rootbak.tar
/root/tmp.tar.bz2
[root@localhost ~]# tar czvf mybackup.tgz -T /root/tar-include -X /root/tar-exclude

 

 

5.不移除文件名称前的“/”号
平时打包的时候如果使用绝对路径的时候,打包会有提示:tar: Removing leading `/' from member names。加上-P参数就不会提示了,但打包的时候会将“/”目录加进去,如果解压这个压缩包的时候当前目录为“/”就比较危险。
下面这一个参数慎用!!! 下面这一个参数慎用!!! 下面这一个参数慎用!!!

[root@osker ~]# tar -czvPf foo.tar.gz /tmp
/tmp/
/tmp/.font-unix/
/tmp/.Test-unix/
/tmp/.XIM-unix/
/tmp/.ICE-unix/
/tmp/.X11-unix/
/tmp/gen.txt
[root@osker ~]# tar -czvf foo.tar.gz /tmp
tar: Removing leading `/' from member names
/tmp/
/tmp/.font-unix/
/tmp/.Test-unix/
/tmp/.XIM-unix/
/tmp/.ICE-unix/
/tmp/.X11-unix/
/tmp/gen.txt 

-P, --absolute-names       don't strip leading `/'s from file names
-P或--absolute-names:文件名使用绝对名称,不移除文件名称前的“/”号;
大家还是忘了它吧!!!


6.打包软连接的源文件
打包压缩时候遇到文件中有软连接文件的时候,建议使用-h参数,以免打包后软链接失效!

[root@osker ~]# ll osker
total 0
lrwxrwxrwx 1 root root 11 Apr 12 21:08 e.ln -> /root/e.txt
-rw-r--r-- 1 root root  0 Apr 12 20:03 foo1.txt
-rw-r--r-- 1 root root  0 Apr 12 20:03 foo2.txt
-rw-r--r-- 1 root root  0 Apr 12 20:03 foo3.txt
[root@osker ~]# cat osker/e.ln
osker/excd.txt
[root@osker ~]# tar -czvhf ./soft.tar.gz ./osker
./osker/
./osker/foo1.txt
./osker/foo2.txt
./osker/foo3.txt
./osker/e.ln
[root@osker ~]# tar -xf ./soft.tar.gz -C /opt
[root@osker ~]# tar -czvf ./osker.tar.gz ./osker
./osker/
./osker/foo1.txt
./osker/foo2.txt
./osker/foo3.txt
./osker/e.ln
[root@osker ~]# tar -xf ./osker.tar.gz -C /tmp
[root@osker ~]# rm -f e.txt
[root@osker ~]# cat /opt/osker/e.ln
osker/excd.txt
[root@osker ~]# cat /tmp/osker/e.ln
cat: /tmp/osker/e.ln: No such file or directory

#注意:h需要写到f前面。
-h, --dereference    follow symlinks; archive and dump the files they point to
-h或--dereference    归档时,打包软连接的源文件


7.保留文件的原本属性
平时打包压缩文件的时候多数都要用来备份的,这个时候就需要保留文件的原本属性,就需要用到-p参数。

[root@osker ~]# tar -czvpf ./etc.tar.gz /etc

#注意:p需要写到f前面。
-p, --preserve-permissions, --same-permissions    extract information about file permissions,(default for superuser)
-p或--same-permissions或--same-permissions        用原来的文件权限还原文件;

 

8.打包的时候只将较指定日期更新的文件保存到备份文件里
-N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE
-N <日期格式> 或 --newer=<日期时间>:只将较指定日期更新的文件保存到备份文件里

[root@instance-16 ~]# ls -l --time-style=full-iso
total 12
-rw-r--r--. 1 root root  483 2020-04-11 09:01:29.905704747 +0800 3.txt
-rw-r--r--. 1 root root 1080 2020-04-12 15:43:03.207986554 +0800 passwd
drwxr-xr-x. 2 root root    6 2020-04-12 15:15:24.432926152 +0800 qiu
-rw-r--r--. 1 root root  151 2020-03-27 06:45:33.571820634 +0800 test.txt
-rw-r--r--. 1 root root    0 2020-04-13 07:17:12.856865010 +0800 tt.txt
[root@instance-16 ~]# tar -N '2020-04-12' -czvf /tmp/o.tar ./*
tar: Option --after-date: Treating date `2020-04-12' as 2020-04-12 00:00:00
tar: ./3.txt: file is unchanged; not dumped
./passwd
./qiu/
tar: ./test.txt: file is unchanged; not dumped
./tt.txt

可以从提示中看到有2个文件没有被打包进去。



9.添加文件到已有的打包文件中
-r, --append    append files to the end of an archive
-r                添加文件到已经压缩的文件

[root@backup test]# tar -tf fo2.tar
foo2.txt
anaconda-ks.cfg
[root@backup test]# tar -rvf fo2.tar ~/foo
foo1.txt  foo2.txt  foo3.txt  
[root@backup test]# tar -rvf fo2.tar ~/foo1.txt
tar: Removing leading `/' from member names
/root/foo1.txt
[root@backup test]# tar -tf fo2.tar
foo2.txt
anaconda-ks.cfg
root/foo1.txt

#注意:只能将新文件更新到打包文件里,不能更新到压缩文件。



10.添加改变了的文件到已经存在的打包文件中
-u, --update    only append files newer than copy in archive
-u                添加改变了的文件到已经存在的打包文件中

[root@osker ~]# tar -cvf os.tar ./osker
./osker/
./osker/1.txt
./osker/2.txt
./osker/3.txt
[root@osker ~]# echo "up" > osker/3.txt
[root@osker ~]# tar -uvf os.tar ./osker
./osker/3.txt

#注意:只能将新文件更新到打包文件里,不能更新到压缩文件。


11.从归档中删除文件
--delete    delete from the archive (not on mag tapes!)
--delete    从归档中删除文件

[root@backup ~]# tar -tf /opt/foo.tar
./anaconda-ks.cfg
./del.txt
./foo1.txt
./foo2.txt
./foo3.txt
[root@backup ~]# cd /opt
[root@backup opt]# tar --delete del.txt -vf foo.tar
tar: del.txt: Not found in archive
tar: Exiting with failure status due to previous errors
[root@backup opt]# tar --delete ./del.txt -vf foo.tar
[root@backup opt]# tar -tf foo.tar
./anaconda-ks.cfg
./foo1.txt
./foo2.txt
./foo3.txt

可以看到del.txt已经没有在foo.tar包中了。


12.新增归档文件到另一个归档文件
-A, --catenate, --concatenate   append tar files to an archive
-A或--catenate:新增归档文件到另一个归档文件

[root@backup ~]# tar -cf test/fo1.tar anaconda-ks.cfg
[root@backup ~]# tar -cf test/fo2.tar foo2.txt
[root@backup ~]# cd test
[root@backup test]# tar -A fo1.tar -vf fo2.tar
[root@backup test]# tar -tf fo2.tar
foo2.txt
anaconda-ks.cfg

可以看到fo1.tar中得anaconda-ks.cfg已经添加到fo2.tar中了。


13.解压时候,将文件得mtime修改为当前时间
-m, --touch        don't extract file modified time
-m或--touch        解压时候,将文件得mtime修改为当前时间

[root@backup test]# tar -xf fo2.tar
[root@backup test]# ll
total 24
-rw------- 1 root root  1392 Apr 19  2019 anaconda-ks.cfg
-rw-r--r-- 1 root root 20480 Apr 13 18:45 fo2.tar
-rw-r--r-- 1 root root     0 Apr 13 18:22 foo2.txt
[root@backup test]# rm foo2.txt anaconda-ks.cfg -f
[root@backup test]# tar -mxf fo2.tar
[root@backup test]# ll
total 24
-rw------- 1 root root  1392 Apr 13 18:53 anaconda-ks.cfg
-rw-r--r-- 1 root root 20480 Apr 13 18:45 fo2.tar
-rw-r--r-- 1 root root     0 Apr 13 18:53 foo2.txt

可以看到加-m参数后解压出来得文件mtime为当前时间。



posted @ 2020-04-13 20:07  osker  阅读(2391)  评论(0编辑  收藏  举报