Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 02-03-2010, 07:41 AM   #1
Registered: Nov 2009
Posts: 48

Rep: Reputation: 17
script to compress a directory and then delete older than 7 days

I have been trying to write a script that will take a directory, for example /accounts compress it into a .tar file with the filename containing the date of compression, for example accounts030210.tar and then place that file into a directory called /archive

I also want the script to delete files in /archive that are older than 7 days.

I have give it ago but suspect I went around it completely the wrong way, I would appreciate any help you could give me. Thanks
Old 02-03-2010, 08:19 AM   #2
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,194

Rep: Reputation: 105Reputation: 105
That's a reasonably straightforward script. Rather than asking us to give it to you, why not post what you did and ask for advice and corrections? That's a better learning process. Start by doing the individual commands by hand to see that they work as expected. Then put them in a script and test the script. Use the man pages. Start out with `man tar`, `man date`, and `man find`. Those will give you everything you need.
Old 02-03-2010, 09:26 AM   #3
Registered: Nov 2009
Posts: 48

Original Poster
Rep: Reputation: 17
Yes, I need to get in the habit of reading those man pages


#compress all files from /accounts to /archive named backup_"day-month-year"
tar -cvvf /archive/backup_`date +%d-%m-%Y`.tar.gz /accounts/

#delete all files from /archive older than 7 days
find /archive -ctime +7 -delete
This seems to work, is there a better way?
Old 02-03-2010, 08:21 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
If you want a gzipped file, you need to use the correct flag (-z) and I don't know why you'd use -v (verbose) twice or indeed even once in a program.
Also, why not use this date format for natural sorting: YYYYMMDD
tar -czf /archive/backup_`date +%Y%m%d`.tar.gz /accounts/
Incidentally, .tgz is an acceptable alternative to .tar.gz as a file extension.

Last edited by chrism01; 02-03-2010 at 08:22 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Delete old files older than 7 days rjo98 Linux - Newbie 3 09-18-2009 02:15 PM
Automated script to DELETE files older than 2 days in a Particular folder siddhartha_ece2004 Linux - Newbie 14 07-11-2008 06:46 AM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 04:43 PM
Delete files older then 30 days stefaandk *BSD 1 01-07-2008 09:31 PM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 09:30 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:35 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration