LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2006, 07:13 PM   #1
thedude2010
LQ Newbie
 
Registered: Oct 2002
Posts: 12

Rep: Reputation: 0
bash sort files by date in file name


I have a bash script that does daily backups of a public html folders

there is a backup directory with files inside labeled "Wed May 10 20:14:36 EDT 2006-domain.tgz" and so on.
I want to have 5 daily backups and on the 6th remove the first one.

I'm fairly new to bash scripting and can't quite to seem to figure out how to sort by the date in the file names in order to delete the earliest one.

I would appreciate any help

Thanks
Tom
 
Old 05-10-2006, 07:43 PM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Hi,

So, you want to delete the oldest file on the 6th time it makes a tarball right? Simple enough:

[root@linux mnt]# ls -alrt
total 20
-rw-r--r-- 1 root root 0 Mar 3 2004 file1
-rw-r--r-- 1 root root 0 Apr 3 2004 file2
-rw-r--r-- 1 root root 0 May 3 2004 file3
-rw-r--r-- 1 root root 0 Jun 3 2004 file4
-rw-r--r-- 1 root root 0 Jul 3 2004 file5
drwxr-xr-x 2 root root 4096 Nov 19 2004 floppy
drwxr-xr-x 2 root root 4096 Nov 19 2004 cdrom
drwxr-xr-x 22 root root 4096 Mar 8 10:07 ..
drwxr-xr-x 2 root root 4096 Apr 17 12:23 test
-rw-r--r-- 1 root root 0 May 10 16:21 file6
drwxr-xr-x 5 root root 4096 May 10 16:22 .

The files I made are "file[123456]" as you can see and I changed the time to reflect which one is older than the other. Once file6 is created, you can just run this to delete file1 (which is the oldest file)

Code:
rm -f `ls -alrt | head -2 | grep -v total | awk '{print $9}'`
Of course, you can make this a bit more prettier and have check conditions and whatnot. This is just a head start for you . Let us know if you still need more help in creating your bash script.

-twantrd

Last edited by twantrd; 05-10-2006 at 07:47 PM.
 
Old 05-10-2006, 07:55 PM   #3
thedude2010
LQ Newbie
 
Registered: Oct 2002
Posts: 12

Original Poster
Rep: Reputation: 0
Let me give that a shot. Thanks
 
Old 05-10-2006, 08:50 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
FYI, some bash cmds don't handle filenames with spaces in them properly, without extra fooling around. I highly recommend you use underscores or somesuch instead.
Also,
ls -1|sort
will sort the files by name, regardless of timestamp if you want that, otherwise use
ls -1t
sorts by date stamp
 
Old 05-12-2006, 03:49 AM   #5
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
nice article on (possibly automatic) backup schedules (using rsync and cron):
http://spinink.net/2006/03/29/automa...-cron/#more-55
 
Old 05-12-2006, 10:02 AM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
FWIW -
Code:
stat --format=%Y <filename>
gives the time (seconds since Epoch) the file was last modified.
This is a large number that sorts perfectly.
 
Old 05-12-2006, 11:07 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
you can try this:
http://www.linuxquestions.org/questi...highlight=time

my suggestion starts in post #10
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get file modification date/time in Bash script cmfarley19 Programming 16 04-15-2015 06:25 PM
bash script to sort files by extension otheralex Programming 7 08-19-2005 02:40 AM
How to sort files in a directory by date in KDE? Ace_Azzameen Linux - Software 7 05-24-2004 02:49 PM
howto create a file based on date in bash rohan208 Linux - Newbie 2 05-07-2004 03:54 PM
Set name of tar file as date using bash otisthegbs Linux - Software 5 12-18-2003 03:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10: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