LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-31-2006, 10:29 PM   #1
JSLayton
Member
 
Registered: Apr 2005
Location: Warrior, AL
Distribution: Slackware 10.1, Fedora Core 4
Posts: 128

Rep: Reputation: 15
shell scripting question


Is there a way for me to write a script that will automatically delete the oldest file in a directory? Thanks in advance.
 
Old 05-31-2006, 11:06 PM   #2
joseph
Member
 
Registered: Jun 2003
Location: Batam
Distribution: Ubuntu 10 And Linux Mint
Posts: 414

Rep: Reputation: 30
Quote:
Originally Posted by JSLayton
Is there a way for me to write a script that will automatically delete the oldest file in a directory? Thanks in advance.
how do you tell the oldest file in directory?? is it by comparing it to others or how??

if you are comparing the first file with second file so in a simple way the bash script should be look like this

Code:
#
!/bin/bash
if test file1 -ot file2
then
  rm file1
fi
 
Old 05-31-2006, 11:24 PM   #3
JSLayton
Member
 
Registered: Apr 2005
Location: Warrior, AL
Distribution: Slackware 10.1, Fedora Core 4
Posts: 128

Original Poster
Rep: Reputation: 15
It will be a directory with about 90 files and I will just need to delete the oldest file. I have a script that automates creating tar files from a day's worth of hourly backups. After 90 days are built up, I want the script to delete the oldest tar file in the directory each time a new one is created.
 
Old 05-31-2006, 11:39 PM   #4
joseph
Member
 
Registered: Jun 2003
Location: Batam
Distribution: Ubuntu 10 And Linux Mint
Posts: 414

Rep: Reputation: 30
Quote:
Originally Posted by JSLayton
It will be a directory with about 90 files and I will just need to delete the oldest file. I have a script that automates creating tar files from a day's worth of hourly backups. After 90 days are built up, I want the script to delete the oldest tar file in the directory each time a new one is created.
When you created a new tarball with the name same as existing tar file, the tar should overwrite the existing tar file. At least this is what happen to my automate backup script which will overwrite the old tar file

thanks
 
Old 06-01-2006, 06:55 AM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I think the following will do
Code:
ls -t1 | tail -1 | xargs rm
 
  


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
a shell-scripting question: mrchaos Slackware 3 09-22-2005 11:00 AM
Shell Scripting Question Onyx^ Linux - General 5 04-27-2004 10:37 AM
Shell Scripting Question b_vasu Linux - Newbie 1 11-21-2003 02:10 PM
Shell Scripting Question jester_69 Programming 13 11-05-2003 06:55 PM
Shell Scripting Question chrisk5527 Linux - General 12 07-09-2003 03:36 PM

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

All times are GMT -5. The time now is 11:19 AM.

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