马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
tar:是要用来 打包或者解包 的命令,同时也是 压缩或者解压缩 的命令,经常用作备份文件使用,或将很多文件打成一个包并压缩。
注意:以下代码中 ^C 结尾的为注释行;蓝色字体为目录;赤色字体为包
一、打包
tar -cf 包名 要被打包的文件或目录
tar [选项] [目的包名] [源文件或目录]
选项:
-c:创建一个包
-f:;界说目的包
目的包名:
要打成的包名
源文件名或目录名:
哪些文件或目录要被打成包
代码如下:
[root@iZbp1gh065n66eoqapqav5Z ~]# 快捷键 Ctrl+C 可以使得此行命令无效并进入新一行^C
[root@iZbp1gh065n66eoqapqav5Z ~]# Ctrl+C 快捷键后会出现 ^c 指令,表现忽略此行命令^C
[root@iZbp1gh065n66eoqapqav5Z ~]# 以是本篇博客代码中命令行出现 ^c 指令的请看做注释行^C
[root@iZbp1gh065n66eoqapqav5Z ~]# 表现当前目录下全部文件及目录^C
[root@iZbp1gh065n66eoqapqav5Z ~]# ls
a.txt dir1 ds shellday1 soft
[root@iZbp1gh065n66eoqapqav5Z ~]# 将 dir1 目录, a.txt 文件打包为aa.tar^C
[root@iZbp1gh065n66eoqapqav5Z ~]# tar -cf aa.tar ./dir1 ./a.txt
[root@iZbp1gh065n66eoqapqav5Z ~]# ls
aa.tar a.txt dir1 ds shellday1 soft
[root@iZbp1gh065n66eoqapqav5Z ~]# 使用 ls 命令再次查看当前目录下的文件及目录^C
[root@iZbp1gh065n66eoqapqav5Z ~]# 可以看到出现了 aa.tar ,说明打包成功^C
二、查看包(在不解包的环境下查看)
tar -tf 包名
tar [选项] [包名]
选项:
-t:查看包里面的文件或目录
-f:;界说目的包
-v:表现打包过程(不重要,压缩包也适用)
目的包名:
要查看的包的路径
代码如下:
[root@iZbp1gh065n66eoqapqav5Z ~]# 上一段代码将 dir1 目录, a.txt 文件打包为 aa.tar 包^C
[root@iZbp1gh065n66eoqapqav5Z ~]# 使用 tar -tf aa.tar 可查看包内容^C
[root@iZbp1gh065n66eoqapqav5Z ~]# tar -tf aa.tar
./dir1/
./dir1/b.txt
./dir1/shellday1/
./dir1/shellday1/demo2.sh
./dir1/shellday1/demo3.sh
./dir1/shellday1/sh
./dir1/shellday1/qwe
./dir1/shellday1/demo1.sh
./dir1/c.txt
./dir1/bb.txt
./dir1/a.txt
./a.txt
[root@iZbp1gh065n66eoqapqav5Z ~]# 可以看到,aa.tar 包将dir1目录及dir1的子目录、子文件,以及a.txt文件都表现出来了,也就是表现了 aa.tar 包中全部的内容,说明查看包命令执行成功^C
三、添加文件到包
tar -rf 包名 要被追加入包的文件或目录
tar [选项] [包名] [源文件或目录]
选项:
-r:将文件追加到包中
-f:;界说目的包
包名:
要被追加到的包的路径
源文件名或目录名:
要添加的文件或目录
代码如下:
[root@iZbp1gh065n66eoqapqav5Z ~]# ls
aa.tar a.txt dir1 ds shellday1 soft
[root@iZbp1gh065n66eoqapqav5Z ~]# 已知本文章标题一打包的代码是将 dir1 目录, a.txt 文件打包为aa.tar包,通过上一条 ls 命令查看到当前目录中全部文件及目录^C
[root@iZbp1gh065n66eoqapqav5Z ~]# 我们用 tar -rf 命令将 ds 和 shellday1 目录添加到 aa.tar 包中^C
[root@iZbp1gh065n66eoqapqav5Z ~]# tar -rf aa.tar ./ds ./shellday1
[root@iZbp1gh065n66eoqapqav5Z ~]# 查看 aa.tar 包内容^C
[root@iZbp1gh065n66eoqapqav5Z ~]# tar -tf aa.tar
./dir1/
./dir1/b.txt
./dir1/shellday1/
./dir1/shellday1/demo2.sh
./dir1/shellday1/demo3.sh
./dir1/shellday1/sh
./dir1/shellday1/qwe
./dir1/shellday1/demo1.sh
./dir1/c.txt
./dir1/bb.txt
./dir1/a.txt
./a.txt
./ds/
./ds/cc
./ds/aa/
./ds/aa/bb/
./ds/aa/bb/cc/
./ds/z2/
./ds/
./ds/cc
./ds/aa/
./ds/aa/bb/
./ds/aa/bb/cc/
./ds/z2/
./shellday1/
./shellday1/demo2.sh
./shellday1/demo3.sh
./shellday1/sh
./shellday1/qwe
./shellday1/demo1.sh
[root@iZbp1gh065n66eoqapqav5Z ~]# 可以看到,aa.tar 包将dir1目录及dir1的子目录、子文件,以及a.txt文件,ds 目录及ds 的子目录、子文件,shellday1 目录及 shellday1 的子目录、子文件都显 示出来了,说明添加文件及目录到包命令执行成功^C
四、解包
tar -xf 包名
tar [选项] [包名]
选项:
-x:将包解包
-f: 界说目的包
-v:表现解包过程(不重要,压缩包也适用)
目的包名:
要解包的的包的路径
代码如下:
[root@iZbp1gh065n66eoqapqav5Z ~]# 根据本文章“打包”,“添加目录到包”两段代码的操作,已经将 当前目录下的dir1目录,a.txt文件,ds 目录,shellday1 目录都打包进 aa.tar 包里了^C[root@iZbp1gh065n66eoqapqav5Z ~]# 先 ls 命令查看当前目录下的文件及目^C
[root@iZbp1gh065n66eoqapqav5Z ~]# 为了方便观察解包结果,我们先将当前目录下的dir1目录,a.txt 文件, ds 目录,shellday1 目录删掉^C
[root@iZbp1gh065n66eoqapqav5Z ~]# rm -rf dir1 ds a.txt shellday1
[root@iZbp1gh065n66eoqapqav5Z ~]# ls 命令查看当前目录下的文件及目录^C
[root@iZbp1gh065n66eoqapqav5Z ~]# ls
aa.tar soft
[root@iZbp1gh065n66eoqapqav5Z ~]# 接下来我们用 tar -xf 命令将 aa.tar 包解包^C
[root@iZbp1gh065n66eoqapqav5Z ~]# tar -xf aa.tar
[root@iZbp1gh065n66eoqapqav5Z ~]# 再次用 ls 命令查看当前目录下的文件及目录^C
[root@iZbp1gh065n66eoqapqav5Z ~]# ls
aa.tar a.txt dir1 ds shellday1 soft
[root@iZbp1gh065n66eoqapqav5Z ~]# 发现dir1目录,a.txt文件,ds 目录,shellday1 目录重新出 现,说明解包成功^C
(注释)若将 a.txt , dir1 打包想看到它的打包过程,只须要 -cf 加上 v,写成 -cvf 即可
[root@iZbp1gh065n66eoqapqav5Z ~]# tar -cvf dir.tar ./a.txt ./dir1
./a.txt
./dir1/
./dir1/b.txt
./dir1/shellday1/
./dir1/shellday1/demo2.sh
./dir1/shellday1/demo3.sh
./dir1/shellday1/sh
./dir1/shellday1/qwe
./dir1/shellday1/demo1.sh
./dir1/c.txt
./dir1/bb.txt
./dir1/a.txt
[root@iZbp1gh065n66eoqapqav5Z ~]# 一串结果现实的是压缩过程,由于有 v ,-v是表现压缩过程
五、打压缩包
tar -czf 包名 要被打包的文件或目录
tar [选项] [压缩名] [源文件]
选项:
-c:打包
-z:将打包文件及目任命 gzip 格式压缩
-v:表现压缩过程(不重要)
-f:界说目的包
压缩名:
指定压缩的名字
源文件:
要打包压缩的文件及目录
代码如下:
[root@iZbp1gh065n66eoqapqav5Z ~]# ls查看当前目录下的文件及目录^C
[root@iZbp1gh065n66eoqapqav5Z ~]# ls
aa.tar a.txt dir1 ds shellday1 soft
[root@iZbp1gh065n66eoqapqav5Z ~]# 将 a.txt 文件和 dir1 目录放进压缩包 dir_yasuo 压缩包中,由于我们没有加 z 以是不表现压缩过程,若想看压缩过程,将以下命令的 -czf 写成 -czvf 即可^C
[root@iZbp1gh065n66eoqapqav5Z ~]# tar -czf dir_yasuo.tar.gz ./a.txt ./dir1
[root@iZbp1gh065n66eoqapqav5Z ~]# 再次用 ls 命令查看当前目录下的文件及目录^C
[root@iZbp1gh065n66eoqapqav5Z ~]# ls
aa.tar a.txt dir1 dir_yasuo.tar.gz ds shellday1 soft
[root@iZbp1gh065n66eoqapqav5Z ~]# 使用 tar -tf dir_yasuo.tar.gz 可查看包内容^C
[root@iZbp1gh065n66eoqapqav5Z ~]# tar -tf dir_yasuo.tar.gz
./a.txt
./dir1/
./dir1/b.txt
./dir1/shellday1/
./dir1/shellday1/demo2.sh
./dir1/shellday1/demo3.sh
./dir1/shellday1/sh
./dir1/shellday1/qwe
./dir1/shellday1/demo1.sh
./dir1/c.txt
./dir1/bb.txt
./dir1/a.txt
[root@iZbp1gh065n66eoqapqav5Z ~]# 可以看到,dir_yasuo.tar.gz 包将dir1目录及dir1的子目录、子文件,以及a.txt文件都表现出来了,说明打压缩包命令执行成功^C
六、解压缩包
tar -xzf 包名
tar [选项] [包名]
选项:
-x:将包解包
-z:将打包文件以 gzip 格式解压缩
-f: 界说目的包
-v:表现解压缩过程(不重要)
目的包名:
要解压缩包的包的路径
代码如下:
[root@iZbp1gh065n66eoqapqav5Z ~]# 上一个代码我们将 a.txt 文件和 dir1 目录放进压缩包 dir_yasuo 压缩包中了,为了方便查看解压缩包的结果,我们先删掉a.txt 文件和 dir1 目录^C
[root@iZbp1gh065n66eoqapqav5Z ~]# rm -rf a.txt dir1
[root@iZbp1gh065n66eoqapqav5Z ~]# ls 命令查看当前目录下的文件及目录^C
[root@iZbp1gh065n66eoqapqav5Z ~]# ls
aa.tar dir_yasuo.tar.gz ds shellday1 soft
[root@iZbp1gh065n66eoqapqav5Z ~]# 接下来我们用 tar -xzf 命令将 dir_yasuo.tar.gz 压缩包解包^C
[root@iZbp1gh065n66eoqapqav5Z ~]# tar -xzf dir_yasuo.tar.gz
[root@iZbp1gh065n66eoqapqav5Z ~]# 再次用 ls 命令查看当前目录下的文件及目录^C
[root@iZbp1gh065n66eoqapqav5Z ~]# ls
aa.tar a.txt dir1 dir_yasuo.tar.gz ds shellday1 soft
[root@iZbp1gh065n66eoqapqav5Z ~]# 发现a.txt 文件和 dir1 目录重新出现,说明解压缩包包成功^C
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |