2014-10-28 428 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 
相关问题