常用Linux命令:tar
[test1280@localhost 20170702]$ uname -a
Linux localhost.localdomain 2.6.18-371.el5 #1 SMP Thu Sep 5 21:21:44 EDT 2013 x86_64 x86_64 x86_64 GNU/Linux
tar是一个打包工具,但是其有选项可以直接将这个“包”进行压缩,与gunzip类似。
命令总览:
tar cf myproject.tar myproject
tar xf myproject.tar
tar cvf myproject.tar myproject
tar xvf myproject.tar
tar zcf myproject.tar myproject
tar zxf myproject.tar
tar zcvf myproject.tar.gz myproject --exclude myproject/log
(注意-- exclude myproject/log后没有/)
z指定与压缩有关;f指定文件;v是verbose,详细输出;x和c互斥,x是解包,c是打包…
打包要有c/x以及f即可,压缩就多带着z即可。
PS:其实我一般都不加v选项,刷屏的感觉…
目录结构:
[test1280@localhost 20170702]$ ll
总计 4
drwxrwxr-x 7 test1280 test1280 4096 07-02 09:33 myproject
[test1280@localhost 20170702]$ tree
.
`-- myproject
|-- bin
| `-- binfile
|-- etc
| `-- etcfile
|-- lib
| `-- libfile
|-- log
| `-- logfile
`-- src
`-- srcfile
6 directories, 5 files
1.
tar打包:
[test1280@localhost 20170702]$ tar cvf myproject.tar myproject
myproject/
myproject/log/
myproject/log/logfile
myproject/etc/
myproject/etc/etcfile
myproject/lib/
myproject/lib/libfile
myproject/src/
myproject/src/srcfile
myproject/bin/
myproject/bin/binfile
[test1280@localhost 20170702]$ ll
总计 16
drwxrwxr-x 7 test1280 test1280 4096 07-02 09:33 myproject
-rw-rw-r-- 1 test1280 test1280 10240 07-02 09:38 myproject.tar
tar释放:
[test1280@localhost 20170702]$ ll
总计 12
-rw-rw-r-- 1 test1280 test1280 10240 07-02 09:38 myproject.tar
[test1280@localhost 20170702]$ tar xvf myproject.tar
myproject/
myproject/log/
myproject/log/logfile
myproject/etc/
myproject/etc/etcfile
myproject/lib/
myproject/lib/libfile
myproject/src/
myproject/src/srcfile
myproject/bin/
myproject/bin/binfile
[test1280@localhost 20170702]$ ll
总计 16
drwxrwxr-x 7 test1280 test1280 4096 07-02 09:33 myproject
-rw-rw-r-- 1 test1280 test1280 10240 07-02 09:38 myproject.tar
2.
tar打包&&压缩:
[test1280@localhost 20170702]$ tar zcvf myproject.tar.gz myproject
myproject/
myproject/log/
myproject/log/logfile
myproject/etc/
myproject/etc/etcfile
myproject/lib/
myproject/lib/libfile
myproject/src/
myproject/src/srcfile
myproject/bin/
myproject/bin/binfile
[test1280@localhost 20170702]$ ll
总计 8
drwxrwxr-x 7 test1280 test1280 4096 07-02 09:33 myproject
-rw-rw-r-- 1 test1280 test1280 284 07-02 09:42 myproject.tar.gz
tar释放&&解压缩:
[test1280@localhost 20170702]$ tar zxvf myproject.tar.gz
myproject/
myproject/log/
myproject/log/logfile
myproject/etc/
myproject/etc/etcfile
myproject/lib/
myproject/lib/libfile
myproject/src/
myproject/src/srcfile
myproject/bin/
myproject/bin/binfile
[test1280@localhost 20170702]$ ll
总计 8
drwxrwxr-x 7 test1280 test1280 4096 07-02 09:33 myproject
-rw-rw-r-- 1 test1280 test1280 284 07-02 09:42 myproject.tar.gz
[test1280@localhost 20170702]$ tree
.
|-- myproject
| |-- bin
| | `-- binfile
| |-- etc
| | `-- etcfile
| |-- lib
| | `-- libfile
| |-- log
| | `-- logfile
| `-- src
| `-- srcfile
`-- myproject.tar.gz
6 directories, 6 files
3.
我在工作中通常有这么一种情况:
现网的相关项目我需要把它取下来,但是由于现网业务很多,长时间未清理日志,所以在log目录下文件占用很大(可能有几十个G),So:
如何打包其他的目录而不包含某个目录?
[test1280@localhost 20170702]$ tar zcvf myproject.tar.gz myproject --exclude myproject/log
myproject/
myproject/etc/
myproject/etc/etcfile
myproject/lib/
myproject/lib/libfile
myproject/src/
myproject/src/srcfile
myproject/bin/
myproject/bin/binfile
[test1280@localhost 20170702]$ ll
总计 8
drwxrwxr-x 7 test1280 test1280 4096 07-02 09:33 myproject
-rw-rw-r-- 1 test1280 test1280 254 07-02 09:50 myproject.tar.gz
从v选项的verbose中可以看出确实没有将log目录打包压缩进myproject.tar.gz。
[test1280@localhost 20170702]$ tar zxvf myproject.tar.gz
myproject/
myproject/etc/
myproject/etc/etcfile
myproject/lib/
myproject/lib/libfile
myproject/src/
myproject/src/srcfile
myproject/bin/
myproject/bin/binfile
[test1280@localhost 20170702]$ ll
总计 8
drwxrwxr-x 6 test1280 test1280 4096 07-02 09:33 myproject
-rw-rw-r-- 1 test1280 test1280 254 07-02 09:50 myproject.tar.gz
[test1280@localhost 20170702]$ tree myproject
myproject
|-- bin
| `-- binfile
|-- etc
| `-- etcfile
|-- lib
| `-- libfile
`-- src
`-- srcfile
4 directories, 4 files
进一步证明:没有将log目录加进去。
如何exclude多个目录?
[test1280@localhost 20170702]$ tar zcvf myproject.tar.gz myproject --exclude myproject/log --exclude myproject/lib
myproject/
myproject/etc/
myproject/etc/etcfile
myproject/src/
myproject/src/srcfile
myproject/bin/
myproject/bin/binfile
使用多次–exclude即可。
还有重要的一点需要说明:
tar zcvf myproject.tar.gz myproject --exclude myproject/log
tar zcvf myproject.tar.gz myproject --exclude myproject/log/
两句话是不一样的,你可以试一下。
第一句可以达到我们想要的效果,第二句多了一个/,但是并没有真的exclude掉,还是在的,实际使用中注意下这点。
KEYS:
cf
xf
zcf
zxf
--exclude