LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-23-2014, 02:30 PM   #1
linuxiel
LQ Newbie
 
Registered: Jul 2014
Posts: 10

Rep: Reputation: Disabled
BASH Compare date


BASH compare list of strings from date
Hi again
I have a list of backup tar files made from the command:
tar -cvpz /mnt/bak/bast-$(date +%Y-%m-%d_%H_%M).tar.gz /home

The list looks like this:

bast-2014-09-08_20_58.tar.gz
bast-2014-09-09_20_20.tar.gz
bast-2014-09-10_20_20.tar.gz
bast-2014-09-11_20_20.tar.gz
bast-2014-09-12_20_20.tar.gz
bast-2014-09-13_20_20.tar.gz
bast-2014-09-14_20_20.tar.gz
bast-2014-09-15_20_20.tar.gz
bast-2014-09-16_20_20.tar.gz
bast-2014-09-17_20_20.tar.gz

I need a bash command or script to work out which is the earliest one?
I looked into sed a bit and got confused with the regex type syntax.
Is there another way or can someone help me with a sed command?

Thanks in advance
 
Old 09-23-2014, 02:46 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Since it's year-month-day-hour-minute, all you need to do is sort it alphabetically and grab the first one. It would be more complicated if it was month-day-year, but as-is it should be very simple.
 
Old 09-23-2014, 02:53 PM   #3
linuxiel
LQ Newbie
 
Registered: Jul 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks but im looking for specific syntax. Im not so good at bash yet and a little lost.

I guess I need something like

for a in $(ls /mnt/bak)
do echo "whichever is the earliest of $a"
done


lol I know I know, my scripting sucks.
 
Old 09-23-2014, 03:19 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by linuxiel View Post
Thanks but im looking for specific syntax. Im not so good at bash yet and a little lost.

I guess I need something like

for a in $(ls /mnt/bak)
do echo "whichever is the earliest of $a"
done


lol I know I know, my scripting sucks.
How about...

Code:
for a in $(ls -1 /mnt/bak/bast*.gz | sort | head -1); do  echo "$a" is the earliest ; done
I'm a terrible hack at bash scripting so this is mere butchery.
Expect better answers 1 minute after I Submit Reply.

output:
Code:
bast-2014-09-08_20_58.tar.gz is the earliest
 
Old 09-23-2014, 03:22 PM   #5
naitso
LQ Newbie
 
Registered: Aug 2010
Posts: 14

Rep: Reputation: 2
BASH Compare date

ls -lt /mnt/bak/ | head -n 1
 
Old 09-23-2014, 03:55 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by naitso View Post
ls -lt /mnt/bak/ | head -n 1
It's a bad idea to assume the file's timestamp is the time it was originally created. backups get moved and copied all the time, you can't rely on that.

---------- Post added 09-23-14 at 02:56 PM ----------

Quote:
Originally Posted by linuxiel View Post
Thanks but im looking for specific syntax. Im not so good at bash yet and a little lost.

I guess I need something like

for a in $(ls /mnt/bak)
do echo "whichever is the earliest of $a"
done


lol I know I know, my scripting sucks.
You mean something like
Code:
echo $(ls /mnt/bak/bast* | head -1) is the earliest file
?

ls already sorts the output alphabetically, so all you need is a "head -1" to grab the first one.

Last edited by suicidaleggroll; 09-23-2014 at 03:57 PM.
 
Old 09-23-2014, 07:00 PM   #7
linuxiel
LQ Newbie
 
Registered: Jul 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks guys, that worked.

I marked as solved, but secondary if you feel like it: what If I wanted to choose a particular date.

listing the directory, giving a prompt to chose a date?

doesnt matter if theres no replys to this, I might have a play around a bit more myself
 
  


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
BASH compare list of strings from date linuxiel Linux - Software 1 09-23-2014 02:59 PM
[bash] help compare date LadyE Programming 10 09-07-2012 02:49 AM
BASH ... Date /days subtraction Addition and compare em31amit Programming 5 05-24-2012 01:12 PM
how to compare date string?? loplayers Linux - Newbie 1 11-29-2007 08:40 PM
php date compare omarswan Programming 2 10-02-2002 04:41 PM

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

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