You are not logged in.

#1 2012-05-02 08:50:00

funkypotatoe
Member
Registered: 2012-03-23
Posts: 47

comparing dates in filenames

Hi. Let's say i have 10 folders with names "backup_$date". How can i compare them to find oldest one?

Offline

#2 2012-05-02 09:12:15

funkypotatoe
Member
Registered: 2012-03-23
Posts: 47

Re: comparing dates in filenames

Sorry, shame on me, probably found it myself:

awk -F_ '{ if($2 > "some date") print $2 }'

Offline

#3 2012-05-02 10:10:05

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: comparing dates in filenames

if you used e.g. YY-MM-DD format, you could simply pick the last one.

Offline

#4 2012-05-13 15:40:45

debdj
Member
Registered: 2012-01-19
Posts: 163

Re: comparing dates in filenames

depending upon the date format, if its DD/MM/YYYY,

for i in `ls`; do echo "$i"; done|sort -t '_' -k 2.7,11 -k 2.4,5 -k 2.1,2

@karol  dates are sorted in the order year,month,date.  Why date first?

Last edited by debdj (2012-05-13 16:21:38)

Offline

#5 2012-05-13 16:00:04

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: comparing dates in filenames

debdj wrote:

@karol  dates are sorted in the order year,month,date.

How do you know what does OP use as $date?

$ touch backup_$(date +"%Y%d%m)
$ ls
backup_20121305

========
Edit: Yes, I know you can parse the format, but 'year, month, date' would allow a simple numerical sort :-)
========

debdj wrote:

Why date first?

I don't understand this question. How else do you compare old v. new?


He seem to have found an answer already so I'm not sure if our discussion will help anyone.
funkypotatoe, please mark the thread as solved.

Last edited by karol (2012-05-13 16:03:08)

Offline

#6 2012-05-13 16:20:30

debdj
Member
Registered: 2012-01-19
Posts: 163

Re: comparing dates in filenames

I don't understand this question. How else do you compare old v. new?

I meant if there are more that just days in the date.

He seem to have found an answer already so I'm not sure if our discussion will help anyone.
funkypotatoe, please mark the thread as solved.

yeah, i guess so.

Offline

#7 2012-05-13 16:40:04

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: comparing dates in filenames

debdj wrote:

I don't understand this question. How else do you compare old v. new?

I meant if there are more that just days in the date.

I'm using

backup_$(date +"%Y%m%d-%H%M%S")

and I can simply

ls -1 | tail -1

to get the last one (assuming there's nothing else in this folder but my backups) or

$ ls -1 backup_20120513-182220 backup_20120513-182356 | tail -1
backup_20120513-182356

to get the oldest of the n ones I'm interested in or

find <path> -type d -name "backup_*" | tail -1

Offline

#8 2012-05-13 16:47:30

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: comparing dates in filenames

karol wrote:
debdj wrote:

I don't understand this question. How else do you compare old v. new?

I meant if there are more that just days in the date.

I'm using

backup_$(date +"%Y%m%d-%H%M%S")

and I can simply

ls -1 | tail -1

to get the last one (assuming there's nothing else in this folder but my backups) or

$ ls -1 backup_20120513-182220 backup_20120513-182356 | tail -1
backup_20120513-182356

to get the oldest of the n ones I'm interested in or

find <path> -type d -name "backup_*" | tail -1

None of this is actually safe, since it uses ls... There's far more robust solutions available using mtime, e.g. the simple GNU-coreutils based bash version or the full blown python version. The latter was something I wrote for work and has been tremendously useful.

Offline

Board footer

Powered by FluxBB