场景

想要将以前的程序打包下来,但是发现有log日志,而且比较大,有几G大。

解决思路
打包的时候,将某些文件夹给排除掉,只打包自己想要的文件。

解决方法
使用tar命令来进行打包,并使用-exclude这个参数来排除一个文件夹。如果存在多个排除文件夹,就要写多个参数。

示例
tar -zcvf scheduler_20180508.tar.gz
--exclude=scheduler/lib.bk
--exclude=scheduler/logs scheduler

常见错误
打包命令觉得没有错,但是还是将排除的文件夹给打包进去了。原因之一便是在写路径的时候,后面跟了/,导致排除失败。
失败用法

tar -zcvf scheduler_20180508.tar.gz
--exclude=scheduler/lib.bk/
--exclude=scheduler/logs/ scheduler

正确用法

tar -zcvf scheduler_20180508.tar.gz
--exclude=scheduler/lib.bk
--exclude=scheduler/logs scheduler

————————————————
版权声明:本文为CSDN博主「枫夜求索阁」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013084266/article/details/80249244

 

参考示例:https://www.linuxprobe.com/linux-tar-file.html

方法一

[root@system1 testdir]# touch 1 2 3 4 b
[root@system1 testdir]# ls
1  2  3  4  b
[root@system1 testdir]# tar zcvf a.tar.gz ./* --exclude b
#排除b文件
./1
./2
./3
./4
[root@system1 testdir]# ls
1  2  3  4  a.tar.gz  b
[root@system1 testdir]# mv a.tar.gz /tmp/test
[root@system1 testdir]# ls
1  2  3  4  b
[root@system1 testdir]# cd /tmp/test
[root@system1 test]# ls
a.tar.gz
[root@system1 test]# tar -xvf a.tar.gz 
./1
./2
./3
./4
[root@system1 test]# ls
1  2  3  4  a.tar.gz
方法二
[root@system1 testdir]# ls
1  2  3  4  b
[root@system1 testdir]# find ./ -type f ! -name 'b'  | xargs tar caf a.tar.gz
#排除b文件
[root@system1 testdir]# ls
1  2  3  4  a.tar.gz  b
[root@system1 testdir]# mv a.tar.gz /tmp/test
[root@system1 testdir]# cd /tmp/test
[root@system1 test]# ls
a.tar.gz
[root@system1 test]# tar -xvf a.tar.gz 
./1
./2
./3
./4
[root@system1 test]# ls
1  2  3  4  a.tar.gz

 

posted on 2021-09-07 20:37  51core  阅读(1180)  评论(0)    收藏  举报