ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.