I have a folder /home/software, with some sub folders


I want to create tar backup including the date for each sub-folder so I will end up with files like:


What's the best way to do that, and to the delete any of those backups that is more than 10 days old?

  • 2
    Have you looked into rsync? It has the compression and pruning that you are looking for. – 111--- Oct 13 '15 at 14:35
  • no , i not looking for rsync. – Adam Chan Oct 13 '15 at 14:50
  • 1
    Sounds like you're trying to reinvent the wheel. There are lots of (free and open) backup programs out there that do the job (dar, attic, obnam, ...). – Jan Oct 13 '15 at 15:38

Here's a script that does what you have asked for:

ls /home/software/ | while read d
  tar czf "/home/software/$d-$(date +%y%m%d).tar.gz" "/home/software/$d"
find /home/software -type f -name '*.tar.gz' -mtime +10 -exec rm {} \;


cd /home/software/
ls | while read d
  tar czf "$d-$(date +%y%m%d).tar.gz" "$d"
find . -type f -name '*.tar.gz' -mtime +10 -exec rm {} \;
| improve this answer | |
  • show the error: tar: Removing leading `/' from member names – Adam Chan Oct 13 '15 at 16:50
  • change directory inside /home/software/ and run commands in place ! see answer editted :) – Jonah Oct 13 '15 at 17:30

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.