One: Background
There are a lot of files under one path, and more and more files accumulate over time, and the filenames follow a basic format that contains information such as month and date, for example: tsmtpp2735d20170517142005E981.txt.gz
Two: Realize
We use a script to store these files separately by date, as follows:
#文件所在的路径 Xdrbasepath= '/data/test ' Echo ' XDR file root path: ' ${xdrbasepath} for file in ${xdrbasepath}/*.txt.gz; Do # #截取文件里面的日期信息 #1, gets the file name. include suffix filename=$ (basename $file) echo ' filename: ' ${filename} #2, intercept time: TSMTPP2735D20170517142005E981.txt.gz Timeyear=${filename:11:4} Timemonth=${filename:15:2} Timeday=${filename:17:2} #echo ' Parse out the time to year: ' ${timeyear} #echo ' Parse out the time to month: ' ${timemonth} #echo ' Parse out the time to date: ' ${timeday} Yeardirpath=${xdrbasepath}/${timeyear} Monthdirpath=${yeardirpath}/${timemonth} Daydirpath=${monthdirpath}/${timeday} #echo $yeardirpath #echo $monthdirpath #echo $daydirpath #判断文件夹是否存在, do not exist to create if [!-e ${yeardirpath}-o!-d ${yeardirpath}]; Then mkdir ${yeardirpath} Fi if [!-e ${monthdirpath}-o!-d ${monthdirpath}]; Then mkdir ${monthdirpath} Fi if [!-e ${daydirpath}-o!-d ${daydirpath}]; Then mkdir ${daydirpath} Fi #将文件移动到改目录下 Mv-f ${xdrbasepath}/${filename} ${daydirpath} #echo '-------------------------' Done |
This will move the file "tsmtpp2735d20170517142005E981.txt.gz" to the "/DATA/TEST/2017/05/17" path.
The IF statement is interpreted as follows:
With:-A, when multiple conditions are specified, the default is the relationship
Or:-O
Non:!, this is a single-mesh operator
Whether the file exists:-E
Whether the file is a directory:-D
Three: Add to the system schedule inside
1. View Crontab Service Status
2, start the service
/sbin/service Crond Start |
3, restart the service
/sbin/service Crond Restart |
4, close the service
5. View scheduling Tasks
6. Edit the Dispatch task
7. Delete all Scheduled Tasks
Note: If you just delete a scheduled task, use "CRONTAB-E"
8. View the Dispatch log
Example of scheduling edits:
1, perform schedule edit "CRONTAB-E"
2, add the scheduling task information in the Open VI, for example, every minute I execute the above script
*/5 * * * */data/movetest.sh |
This article comes from the "Simple and Easy" blog, so be sure to keep this source http://dengshuangfu.blog.51cto.com/8794650/1927444
Using a Linux script to move files