tar 命令做打包
当 tar 命令用于打包操作时,该命令的基本格式为:
需要注意的是,在使用 tar 命令指定选项时可以不在选项前面输入“-”。例如,使用“cvf”选项和 “-cvf”起到的作用一样。下面给大家举几个例子,一起看看如何使用 tar 命令打包文件和目录。
【例 1】打包文件和目录。
[root@localhost ~]# tar -cvf anaconda-ks.cfg.tar anaconda-ks.cfg#把 anacondehks.cfg 打包为 anacondehks.cfg.tar 文件
选项 "-cvf" 一般是习惯用法,记住打包时需要指定打包之后的文件名,而且要用 ".tar" 作为扩展名。打包目录也是如此:
 [root@localhost ~]# ll -d test/drwxr-xr-x 2 root root 4096 6月 17 21:09 test/#test是我们之前的测试目录[root@localhost ~]# tar -cvf test.tar test/test/test/test3test/test2test/test1#把目录打包为test.tar文件tar命令也可以打包多个文件或目录,只要用空格分开即可。例如:[root@localhost ~]# tar -cvf ana.tar anaconda-ks.cfg /tmp/#把anaconda-ks.cfg文件和/tmp目录打包成ana.tar文件包
   复制代码
 
【例 2】打包并压缩目录。
首先声明一点,压缩命令不能直接压缩目录,必须先用 tar 命令将目录打包,然后才能用 gzip 命令或 bzip2 命令对打包文件进行压缩。例如:
 [root@localhost ~]#ll -d test test.tardrwxr-xr-x 2 root root 4096 6月 17 21:09 test-rw-r--r-- 1 root root 10240 6月 18 01:06 test.tar#我们之前已经把test目录打包成test.tar文件[root@localhost ~]# gzip test.tar[root@localhost ~]# ll test.tar.gz-rw-r--r-- 1 root root 176 6月 18 01:06 test.tar.gz#gzip命令会把test.tar压缩成test.tar.gz
   复制代码
 
评论