I have a directory which will have some folders and some files within it. Suppose this is my current directory as shown below with pwd
command –
david@slc4b03c:/bat/data/snapshot$ pwd /bat/data/snapshot
And I am logged into machineA
.
This /bat/data/snapshot
directory will have some folders and some files within it as shown below –
david@slc4b03c:/bat/data/snapshot$ ls -lt total 22400264 drwxr-xr-x 2 cronusapp app 102400 2013-12-22 04:01 20131222 drwxr-xr-x 2 cronusapp app 102400 2013-12-16 03:00 20131215 -rw-r--r-- 1 cronusapp app 26412620 2013-12-13 02:04 weekly_003_5.data -rw-r--r-- 1 cronusapp app 26492037 2013-12-13 02:02 weekly_003_5.data -rw-r--r-- 1 cronusapp app 26480695 2013-12-13 02:02 weekly_003_5.data -rw-r--r-- 1 cronusapp app 26475266 2013-12-13 02:02 weekly_003_5.data -rw-r--r-- 1 cronusapp app 26471366 2013-12-13 02:02 weekly_003_5.data -rw-r--r-- 1 cronusapp app 26455311 2013-12-13 02:02 weekly_003_5.data
So the two folders I have is 20131222
and 20131215
as shown above.. Now I want to extract the latest folder from it, which will be 20131222
as it is more recent as compared to 20131215
and then make a full path like this –
/bat/data/snapshot/20131222/
As I need to use this full path while doing the scp from that machine to another machine,
Below is my shell script in which I have hardcoded the recent path currently with 20131222
but in general, I need to make the full path by seeing which folder is the latest one by using the below shell script..
#!/bin/bash readonly LOCATION=/bat/data/snapshot scp david@slc4b03c.slc.host.com:/bat/data/snapshot/20131222/weekly_003_5.data /data01/primary/.
The above shell script I won’t be running from the machineA. I will be running from different machine, let’ say it is machineB
.
Is it possible to do this in shell script?
Advertisement
Answer
Simple but unreliable solution
dir=$(ls -dt1 "$LOCATION"/*/ | head -n1) scp "david@slc4b03c.slc.host.com:$dir"/weekly_003_5.data /data01/primary/.
The above would fail if the name of the most recent directory contains a newline or other difficult characters.
If you want to restrict the directory name to be in the form of a date (YYYYMMDD), as per your examples, then try:
dir=$(ls -dt1 "$LOCATION"/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/ | head -n1)
For more info on the problems with using ls
in this way, see:
More Robust solution (GNU tools required)
This code uses find
with NUL-separated output to handle difficult file names:
dir=$(find "$LOCATION" -mindepth 1 -maxdepth 1 -type d -printf '%T@ %p' |sort -znr | sed -zE 's/^[^ ]+ //;q')