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?
Lots of ways to do this, but the way I'd do as a quick & dirty is to use a combination of find
and sort
and tail
:
dir=`find $LOCATION -type d | sort | tail -1`
By specifying $LOCATION
as the path for find
, you'll automatically get the directory prepended to your output, which should let sort
and tail
give you the answer without any further interpolation of current directory.
我想,您正在寻找特定目录上的最新目录,请使用以下命令获取最新的Director并分配任何变量
ls | sort | tail -1
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:
Is there a reason why ls does not have a --zero or -0 option
ls authors: "ls is really a tool for direct consumption by a human"
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\0' |sort -znr | sed -zE 's/^[^ ]+ //;q')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.