LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-13-2015, 05:17 PM   #1
epipko
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Rep: Reputation: Disabled
How to select newest "dated" directory?


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?

Thanks,
 
Old 03-13-2015, 06:03 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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:

Code:
#!/bin/bash
backupdir=$(ls -lt | grep -v ^total | head -n1 | awk '{print $9}')
echo $backupdir
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

Last edited by Kustom42; 03-13-2015 at 06:09 PM.
 
Old 03-14-2015, 06:32 PM   #3
epipko
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank you very much for quick answer. I don't know (yet) what the parts of the expressions are, but will try to break it apart to learn.
 
Old 03-15-2015, 04:46 PM   #4
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
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.

Last edited by joe_2000; 03-15-2015 at 04:50 PM.
 
Old 03-15-2015, 06:21 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
http://mywiki.wooledge.org/ParsingLs ergo +1 for using "date --date='1 day ago'".
 
1 members found this post helpful.
Old 03-16-2015, 01:40 PM   #6
epipko
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
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?

This does not work:

Code:
newest_dir=$(ssh root@rem_server ls -1rt /path/to/dir | tail -n 1)
scp -r root@rem_server:/path/to/$newestdir

Thank you
 
Old 03-16-2015, 03:46 PM   #7
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by epipko View Post
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?

This does not work:

Code:
newest_dir=$(ssh root@rem_server ls -1rt /path/to/dir | tail -n 1)
scp -r root@rem_server:/path/to/$newestdir

Thank you
The first part works for me. You can test it by running only the first line and then outputting the variable.
Code:
echo $newest_dir
The second line looks wrong. You are providing only one command to your scp command, which does not make any sense.
 
Old 03-16-2015, 04:16 PM   #8
epipko
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
I made a mistake pasting second line. It should read:
Code:
newest_dir=$(ssh root@rem_server ls -1rt /path/to/dir | tail -n 1)
scp -r root@rem_server:/path/to/$newest_dir /path/to/local_dir
You said that first line works for you, but it does not return anything back for me.
 
Old 03-16-2015, 04:53 PM   #9
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by epipko View Post
I made a mistake pasting second line. It should read:
Code:
newest_dir=$(ssh root@rem_server ls -1rt /path/to/dir | tail -n 1)
scp -r root@rem_server:/path/to/$newest_dir /path/to/local_dir
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)
 
Old 03-16-2015, 05:30 PM   #10
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by unSpawn View Post
http://mywiki.wooledge.org/ParsingLs ergo +1 for using "date --date='1 day ago'".
Some very good points made in that link. You'll definitely want to take a look! Thanks for sharing this unSpawn!
 
Old 03-17-2015, 12:37 AM   #11
epipko
LQ Newbie
 
Registered: Mar 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-17-2015, 06:29 AM   #12
ehdf
LQ Newbie
 
Registered: Mar 2015
Posts: 1

Rep: Reputation: Disabled
at first i was posting the same question into a new thread but thank God i got it resolved here.
 
Old 03-17-2015, 04:12 PM   #13
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by epipko View Post
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
 
  


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 script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Socket Programming making use of "select()" and "writefds" johncsl82 Programming 10 11-13-2011 12:27 PM
".config" and ".kde" folders are being created under root directory (Slack Current) piratesmack Slackware 8 03-12-2011 11:06 PM
Skip "Installation Method" and "Select Partition" screens using boot options tclappsdba Linux - Newbie 1 11-26-2010 05:31 AM
[SOLVED] just installed Slackware....Lilo>Select "Ubuntu">>> "Ubuntu is in low graphics mode"? Ubunoob001 Slackware 14 09-16-2010 04:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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