简体   繁体   中英

Shell script to pack multiple tar files (depending on the day)

I have some tar files (always increase everyday), for example:

1.tar
2.tar
3.tar
4.tar
…

I want to wait until it reaches 7.tar , 14.tar , 28.tar and continues, so if possible in the shell script we can arrange the tar to execute packing the files into single unit (tar) as automatically (for ex: every 7 days tar do packing)?

last=`ls [0-9]*.tar | sed s/.tar//g | sort -g | tail -1`
if(($last%7==0))
then
     tar tar -cf `date '+%b%d'`.tar [1-9]*.tar
fi

Add it to the login script and would automatically packages everything the next log in after 7.tar,14.tar etc are created.

What it does?

last=`ls [0-9]*.tar | sed s/.tar//g | sort -g | tail -1`

lists the files, remove the ending .tar, sort and take the last file name, which would be the most recently created file. If that filename is a multiple of 7, then packages the entire thing to a tar file named as monthdate, ie today it woudl by Sept24.tar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM