I am trying to write a shell script in which I want to take tar backup of the folders which are formed on daily basis.

For example my parent directory name is "log" and under this directory a directory will be created automatically on daily basis and all the child directory (which are formed on daily basis) will have several log files created hourly.

Main purpose: I want to take tar backup of the child directory (which are created on daily basis with directory name as 01, 02, 03, 04, ...... ,31) by retaining the last 20 days log files. Means out of 30 day's directory, tar should be done only for first 10 days.

find /usr/local/aryan/2015/logs -iname ".log" -mtime +10 -exec tar -cvzf backup_1.tar.gz" "VARIABLE_FOLDER_NAME" '{}' \;

Please help me to replace the "VARIABLE_FOLDER_NAME" which actually will have the folder names which will be created on daily basis.

Through above command, it will find all the files having extension as .log and are not modified in last 10 days. Now I want to take backup of all those folders which contains files with .log as extension

  • Great, where did you get stuck? Can you show what you have so far? – Paul Jul 29 '15 at 23:35
  • find /usr/local/aryan/2015/logs -iname ".log" -mtime +10 -exec tar -cvzf backup_1.tar.gz" "VARIABLE_FOLDER_NAME" '{}' \; – Aryan Jul 29 '15 at 23:40
  • I am using above command but dont know what to replace in place of "VARIABLE_FOLDER_NAME" There I need to specify the folder name which will be created daily and folder name will be 1, 2, 3, .....,30 – Aryan Jul 29 '15 at 23:40
  • Please edit the question to add any additional information, and check the formatting help for adding code and paragraphs. – Paul Jul 29 '15 at 23:41
  • Any solution? @Paul – Aryan Jul 30 '15 at 0:16

Has the folder name been created or do you need to create it?

If you are creating a new folder each date, then do something like this


Each day it will create a folder named after the date of the month.

If you want the name zero filled, try something like this

DATE_OF_MONTH=`date '+%d'`

These can all be done on one line like:

DATE_OF_MONTH=`date '+%d'`; VARIABLE_FOLDER_NAME=`printf "%02d" $DATE_OF_MONTH"; find /usr/local/aryan/2015/logs -iname ".log" -mtime +10 -exec tar -cvzf backup_1.tar.gz "VARIABLE_FOLDER_NAME" '{}' \;

You might also look at using xpath with your find command to make tar run faster.

If the folder is already created, they you just need to figure out how to pick which one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.