Using a Linux script to move files

Source: Internet
Author: User


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

Service Crond Status

2, start the service

/sbin/service Crond Start

3, restart the service

/sbin/service Crond Restart

4, close the service

/sbin/service Crond Stop

5. View scheduling Tasks

Crontab-l

6. Edit the Dispatch task

Crontab-e

7. Delete all Scheduled Tasks

Crontab-r

Note: If you just delete a scheduled task, use "CRONTAB-E"

8. View the Dispatch log

Tail-f/var/log/cron


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.