shell script to selectively delete files based on date


I'm fairly new to Linux and although I can find my away around the basics and understand a lot of the concepts, I'm having trouble putting together a shell script to perform what seems like it should be a simple task!

I'm trying to write a shell script (that will be run as a cron job) to do the following:

tar/gzip all of the log files in a directory EXCEPT for the one with the current date (since it's still in use)

(I know the command to do this for ALL log files, but I can't figure out a way to exclude the current day's log file by its date (ctime) stamp.)

verify that the tar.gz file was successfully created

delete the individual log files that were added to the tar.gz file from the directory EXCEPT for the current day's log file

(Again, I know how to rm the files -- but, I want to rm all of them EXCEPT for the current day's log file.)

There are SO many unix/linux command switches and combinations of piped commands that it's frustrating to figure this out.

I'm sure it's a no-brainer for someone who's been using Linux for a while.

Thanks for your replies!

- d
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I will not write the whole script for You, but I'll show You how to find files older than, newer than etc:

#find all files under /dir older than 7 days, and delete
find /dir -type f -mtime +7 | xargs rm -f
#find all files newer than FILE, and delete
find /dir -type f -newer /path/to/FILE -exec rm \{\} \;
#find all files older than 10 minutes, and print names(print is default)
find /dir -type f -mmin +10 -print
#find all files newer than 1 day, and tar them to file.tar
find /dir -type f -mtime -1 | tar -c -T - -f file.tar

Need more help, ask more exactly.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

A simpler way to do what you want to do with your logfiles is to install the tool logrotate.
Check your system it is probably already installed (what distro are you using), almost all distro's are using logrotate
logrotate is scheduled via cron to run every day.

logrotate just does what you want, clean up old log files in a gz-file and delete old gz-files.

for new or self-written logfiles you will probably have to add a line in the logrotate configurationfile (/etc/logrotate.conf)


And of course logrotate can be setup to rotate any folder or files from your own packages/folders
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.