LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-03-2010, 06:41 AM   #1
replica88
Member
 
Registered: Nov 2009
Posts: 48

Rep: Reputation: 18
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, 07:19 AM   #2
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

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, 08:26 AM   #3
replica88
Member
 
Registered: Nov 2009
Posts: 48

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

Code:
#!/bin/bash

#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, 07:21 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
Code:
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 07:22 PM.
 
  


Reply



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 anon091 Linux - Newbie 3 09-18-2009 01:15 PM
Automated script to DELETE files older than 2 days in a Particular folder siddhartha_ece2004 Linux - Newbie 14 07-11-2008 05:46 AM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
Delete files older then 30 days stefaandk *BSD 1 01-07-2008 08:31 PM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 08:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:34 PM.

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