You are not logged in.
Pages: 1
Hi. Let's say i have 10 folders with names "backup_$date". How can i compare them to find oldest one?
Offline
Sorry, shame on me, probably found it myself:
awk -F_ '{ if($2 > "some date") print $2 }'
Offline
if you used e.g. YY-MM-DD format, you could simply pick the last one.
Offline
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
@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 :-)
========
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
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
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
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
Pages: 1