打包&&压缩
首先在我们的/root/new文件夹下新建一个文件,
首先切换到new路径下:
[root@localhost ~]# cd /root/new
新建文件:
[root@localhost new]# touch newfile.txt
touch newfile.txt实际上是touch ./newfile.txt,是一个相对路径写法
也可以使用绝对路径写法创建文件:
[root@localhost new]# touch /root/new/newfile1.txt
这样我们一共创建了两个文件,效果如下:
我们压缩这两个txt文件,到当前目录下:
[root@localhost new]# tar -cvf newfiles.tar ./
正确掌握绝对路径和相对路径写法后,你也可以这样写,tar -cvf newfiles.tar ../new
这里tar是压缩/解压缩命令,
参数c表示压缩
参数v显示压缩内容
参数f指定生成的压缩文件名
因此后面跟着的newfiles.tar就是该文件名
最后一个参数指定该文件生成的路径
我们试试加一个参数z
[root@localhost new]# tar -czf newfiles.tar.gz newfile.txt newfile1.txt
看看效果:
这个gz文件差不多是tar文件的%1,搜索了一下。
tar -c是打包命令
tar -cz是打包并压缩命令
查看
看看我们两个包里面都有什么内容
[root@localhost new]# tar -tf newfiles.tar
结果如下:
[root@localhost new]# tar -tzf newfiles.tar.gz
效果如下:
解压||包
因为/root/new文件夹下已经有newfile.txt和newfile1.txt两个文件
因此我们尝试把这两个文件复制到两个文件夹下
首先创建两个空目录
[root@localhost new]# cp newfiles.tar.gz targz
[root@localhost new]# mkdir ./tar
[root@localhost new]# mkdir ./targz
然后分别移动到各自的文件夹下
[root@localhost new]# cp newfiles.tar ./tar/
其实等价于[root@localhost new]# cp newfiles.tar tar
[root@localhost new]# cp newfiles.tar.gz targz
效果如下:
解压该文件
tar -x newfiles.tar
发现无法执行,并且命令行无法退出
因此输入ctrl+c(也就是Windows下的复制组合命令)退出
解压命令如下
[root@localhost tar]# tar -xf newfiles.tar
同理