WenRou's Blog
tar自动打包指定文件夹中的文件到指定目录
2015-5-27 温柔哥


 场景模拟:在/tmp/test/a下有scripts和b两个目录,scripts下存放tar.sh脚本,b目录下则是要打包的文件目录。



当前路径为:/tmp/test/a/b



其目录下所有文件为:

[root@newtest b]# ll

-rw-r--r-- 1 root root 0 06-01 17:11 1file.txt

-rw-r--r-- 1 root root 0 06-01 17:11 2file.txt

-rw-r--r-- 1 root root 0 06-01 17:11 3file.txt

-rw-r--r-- 1 root root 0 06-01 17:11 4file.txt

-rw-r--r-- 1 root root 0 06-01 17:11 5file.txt

-rw-r--r-- 1 root root 0 06-01 17:11 6file.txt

-rw-r--r-- 1 root root 0 06-01 17:04 7file.txt

-rw-r--r-- 1 root root 0 06-01 17:04 8file.txt

-rw-r--r-- 1 root root 0 06-01 17:04 9file.txt





脚本存放路径为:/tmp/test/a/scripts/tar.sh



脚本内容:






  1. #!/bin/bash  



  2. #tar for all files by date  



  3. #write by xiaojing.zhao  



  4. #2012.6.2  



  5.  



  6. DATE='date +%Y-%m-%d' 



  7. #DELDATE='date -v -3d +%Y-%m-%d' 



  8.  



  9. CUR_DIR=/tmp/test/a/b  



  10. FAR_DIR=/tmp/test/a  



  11.  



  12. cd ${CUR_DIR}  



  13. tar zcvf ${FAR_DIR}/`$DATE.tar.gz` *  



  14. cd ${FAR_DIR}  



  15. rm -rf `find . -name '*.tar.gz' -mtime 3`  



  16. echo "complete!"  





执行效果为:



chmod +x tar.sh



[root@newtest scripts]# ./tar.sh 

1file.txt

2file.txt

3file.txt

4file.txt

5file.txt

6file.txt

7file.txt

8file.txt

9file.txt

complete!

 



查看结果



[root@newtest a]# pwd

/tmp/test/a

 



[root@newtest a]# ll

-rw-r--r-- 1 root root    171 06-04 11:08  2014-06-04.tar.gz

drwxr-xr-x 2 root root 4096 06-04 08:56  b

drwxr-xr-x 2 root root 4096 06-04 08:57  scripts

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容