场景模拟:在/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
脚本内容:
#!/bin/bash
#tar for all files by date
#write by xiaojing.zhao
#2012.6.2
DATE='date +%Y-%m-%d'
#DELDATE='date -v -3d +%Y-%m-%d'
CUR_DIR=/tmp/test/a/b
FAR_DIR=/tmp/test/a
cd ${CUR_DIR}
tar zcvf ${FAR_DIR}/`$DATE.tar.gz` *
cd ${FAR_DIR}
rm -rf `find . -name '*.tar.gz' -mtime 3`
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