如何使用shell脚本中的find获取文件名

时间:2014-06-18 12:17:23

标签: shell

我有名为SMS_18_06_2014_15_03_43.log, SMS_18_06_2014_15_03_00.log, SMS_18_06_2014_15_03_21.log的日志文件。

当我使用find *18_06_2014_15_03*.log*时,我能够找到带有此时间戳的所有文件名。但是当使用shell脚本时,它给了我0。

这是我的脚本

LOGGER_PATH=/FULL_LOGGER_PATH

file_stamp_1=$(date --date="75min ago" +"%d_%m_%Y_%H_%M")
file_stamp_2=$(date --date="74min ago" +"%d_%m_%Y_%H_%M")
file_stamp_3=$(date --date="73min ago" +"%d_%m_%Y_%H_%M")
file_stamp_4=$(date --date="72min ago" +"%d_%m_%Y_%H_%M")
file_stamp_5=$(date --date="71min ago" +"%d_%m_%Y_%H_%M")
file_stamp_6=$(date --date="70min ago" +"%d_%m_%Y_%H_%M")
file_stamp_7=$(date --date="69min ago" +"%d_%m_%Y_%H_%M")
file_stamp_8=$(date --date="68min ago" +"%d_%m_%Y_%H_%M")
file_stamp_9=$(date --date="67min ago" +"%d_%m_%Y_%H_%M")
file_stamp_10=$(date --date="66min ago" +"%d_%m_%Y_%H_%M")
file_stamp_11=$(date --date="65min ago" +"%d_%m_%Y_%H_%M")
file_stamp_12=$(date --date="64min ago" +"%d_%m_%Y_%H_%M")
file_stamp_13=$(date --date="63min ago" +"%d_%m_%Y_%H_%M")
file_stamp_14=$(date --date="62min ago" +"%d_%m_%Y_%H_%M")
file_stamp_15=$(date --date="61min ago" +"%d_%m_%Y_%H_%M")

for i in {1..15}
do
    set file_stamp_$i
    echo ${!1}
    Mins_log_files=`find -name $LOGGER_PATH/*${!1}*.log* -atime -75 | sort | uniq -d`;
    Log_file_count=`echo "$Mins_log_files" | wc -w`
    echo "Log_file_count = $Log_file_count"
done

请指导我如何获得带时间戳的特定文件名。

1 个答案:

答案 0 :(得分:0)

最后我得到了这个答案,所以这里有更新和工作脚本。 我删除-atime -75 | sort | uniq -d这部分命令。

LOGGER_PATH=/FULL_LOGGER_PATH

file_stamp_1=$(date --date="75min ago" +"%d_%m_%Y_%H_%M")
file_stamp_2=$(date --date="74min ago" +"%d_%m_%Y_%H_%M")
file_stamp_3=$(date --date="73min ago" +"%d_%m_%Y_%H_%M")
file_stamp_4=$(date --date="72min ago" +"%d_%m_%Y_%H_%M")
file_stamp_5=$(date --date="71min ago" +"%d_%m_%Y_%H_%M")
file_stamp_6=$(date --date="70min ago" +"%d_%m_%Y_%H_%M")
file_stamp_7=$(date --date="69min ago" +"%d_%m_%Y_%H_%M")
file_stamp_8=$(date --date="68min ago" +"%d_%m_%Y_%H_%M")
file_stamp_9=$(date --date="67min ago" +"%d_%m_%Y_%H_%M")
file_stamp_10=$(date --date="66min ago" +"%d_%m_%Y_%H_%M")
file_stamp_11=$(date --date="65min ago" +"%d_%m_%Y_%H_%M")
file_stamp_12=$(date --date="64min ago" +"%d_%m_%Y_%H_%M")
file_stamp_13=$(date --date="63min ago" +"%d_%m_%Y_%H_%M")
file_stamp_14=$(date --date="62min ago" +"%d_%m_%Y_%H_%M")
file_stamp_15=$(date --date="61min ago" +"%d_%m_%Y_%H_%M")

for i in {1..15}
do
    set file_stamp_$i
    echo ${!1}
    Mins_log_files=`find $LOGGER_PATH/${!1}*.log*`;
    Log_file_count=`echo "$Mins_log_files" | wc -w`
    echo "Log_file_count = $Log_file_count"
done

但接下来的问题是,为什么find没有使用sortuniq命令检查文件名。任何人都能解释一下吗?