2014-10-28 69 views
-1

我有一個目錄中,我將與日期格式(年月日)某些文件夾如下圖所示 -如何查找目錄中的最新日期文件夾,然後在shell腳本中構建命令?

[email protected]:/database/batch/snapshot$ ls -lt 
drwxr-xr-x 2 app kyte 86016 Oct 25 05:19 20141023 
drwxr-xr-x 2 app kyte 73728 Oct 18 00:21 20141016 
drwxr-xr-x 2 app kyte 73728 Oct 9 22:23 20141009 
drwxr-xr-x 2 app kyte 81920 Oct 4 03:11 20141002 

現在我需要從/database/batch/snapshot目錄提取最新日期的文件夾,然後在我的殼構造的命令像這樣的腳本 -

./file_checker --directory /database/batch/snapshot/20141023/ --regex ".*.data" > shardfile_20141023.log 

下面是我的shell腳本 -

#!/bin/bash 

./file_checker --directory /database/batch/snapshot/20141023/ --regex ".*.data" > shardfile_20141023.log 

# now I need to grep shardfile_20141023.log after above command is executed 

如何找到最新的日期文件夾並在shell腳本中構建上述命令?

+0

'我如何找到最新的日期文件夾:'?如果:$(ls-1t | head -n 1)'? – 2014-10-28 04:37:54

+0

[在bash中獲取最新的目錄到變量](http://stackoverflow.com/questions/9275964/get-the-newest-directory-in-bash-to-a-variables) – tripleee 2014-10-28 04:38:25

+0

它有可能重複以日期格式與最近的 – john 2014-10-28 04:42:02

回答

1

看,這是方法之一,只是用grep只能有8位數字的文件夾:

ls -t1 | grep -P -e "\d{8}" | head -1 

或者

ls -t1 | grep -E -e "[0-9]{8}" | head -1 
0

你可以嘗試在你的腳本如下:

PUSHD /數據庫/批號/快照

LATESTDATE =`LS-D * | sort -n |尾-1`

POPD

./file_checker --directory /數據庫/批次/快照/ $ {LATESTDATE}/--regex 「*。數據」> shardfile _ $ {LATESTDATE}的.log

0

參見BashFAQ#099 aka "How can I get the newest (or oldest) file from a directory?"。這就是說,如果你不關心實際的修改時間,只是想根據名字找到最近的目錄,你可以使用數組和globbing(注意:帶有globbing的排序順序取決於LC_COLLATE) :

$ find 
. 
./20141002 
./20141009 
./20141016 
./20141023 
$ foo=(*) 
$ echo "${foo[${#foo[@]}-1]}" 
20141023 
相關問題