Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Hi,
I have a /backup directory which contains 5 days worth of backups:
2015-03-12_03-01-07
2015-03-11_03-01-07
2015-03-10_03-01-07
2015-03-09_03-01-07
2015-03-13_03-01-07
I need to copy the content of the NEWEST backup. Ideally, I'd like to find newest backup and assign it to a variable for later use.
I know I can
Code:
ls -lt
to sort directories, but how do I pick the first one and store it into a variable?
There are several ways to do this. You could split the results out in perl or awk and do some crazy sorting. But from your structure it looks like you always just want to find the folder that was backed up most recently which will always be the one that was modified the same day.
Couple of ways I could do it.
Easily with ls and some piping, could have some reliability issues based upon when it is ran and whether there are other folders or files that could be in there that would have modified data within the past 24 hours:
Also if you always just want to find yesterdays backup:
Code:
#!/bin/bash
yesterday=$(date --date='1 day ago' +Y-%m-%d)
echo $yesterday
backupdir=$(find /backup -type d -name $yesterday*)
echo $backupdir
Again neither of these are very advanced but based on your OP I thought it would be best to keep it simple so that you can understand what you are doing and decipher it instead of spending 20 minutes on a perl/awk script that would just be confusing and overkill.
Let us know if you have any other questions and good luck
You can get away with a little less piping when using
Code:
ls -1rt
Mind the first option flag is the number 1, not the letter L. (That way you only get names, rather than the whole other stuff that the -l flag produces).
The -rt part will inverse the order by age. If you pipe that into tail you can reduce the output to the newest directory:
Code:
ls -1rt | tail -n 1
Lastly, as already suggested, you can put the output into a variable like so:
Code:
newest_dir=$(ls -1rt | tail -n 1)
All of this obviously assumes you are in the right directory. If you run these commands in a script make sure to have the right cd command before them.
EDIT: Closer to Kustom42's suggestion you could also remove the -r flag and use head instead of tail. The advantage of the -r / tail combi is that when you run them manually you get to see the newest stuff at the bottom which avoids having to scroll back. For scripts obviously there is not difference between the two.
Thanks again for your help.
One more question regarding the same issue:
Since I need to copy the content of that directory, how to store the result of the remote server search to a local variable so I can scp the content of it?
Thanks again for your help.
One more question regarding the same issue:
Since I need to copy the content of that directory, how to store the result of the remote server search to a local variable so I can scp the content of it?
You said that first line works for you, but it does not return anything back for me.
The first line is only a variable assignment. It's not supposed to produce any output. Have you tried to echo it? You'll want to develop some debugging skills for such problems. If something does not work, inspect the code and what it does step by step.
Issue the first command in a terminal, then check the content of variable $newest_dir with echo as suggested before.
If it is what you want it to be, move on to the second step. Does the scp command run if you hard-code the directory? And so on.
If you can't debug it yourself you'll need to give us some more info.
What output are you getting when running the commands? Any error messages? If so, post them please. If you do not want to post the actual directory names, please create dummy directories / files for testing, and post the real output of the commands you run.
This is to avoid that while anonymizing the commands and their output you introduce differences which are obscuring relevant information. (As just happened with the second command)
Thank you very much for your help and guidance. It works and I've learn something on the way. As far as unSpawn's link, I think we'll be ok using your solution as directory names are programmatically made, so we won't have weird cases described in the article. Thanks again.
Thank you very much for your help and guidance. It works and I've learn something on the way. As far as unSpawn's link, I think we'll be ok using your solution as directory names are programmatically made, so we won't have weird cases described in the article. Thanks again.
Glad you made it work. Please mark the thread as solved if you consider your question as answered.
Last edited by joe_2000; 03-17-2015 at 04:12 PM.
Reason: typo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.