【问题标题】:delete old files series except the last one file删除除最后一个文件以外的旧文件系列
【发布时间】:2018-05-15 07:29:36
【问题描述】:

文件按类别列出。 在YYYYMMDDHHMM 格式的文件名中处理。 如何删除除此文件名的最新文件之外的最旧文件?

"10MX232 .201801070702.txt" >>old file
"10MX232 .201801080703.txt" >>old file
"10MX232 .201801161651.txt" >>old file
"10MX232 .201801181531.txt" >>old file
"10MX232 .201805120846.txt" >>----new file


"1465LY1 .201804051311.txt" >>old file
"1465LY1 .201805101441.txt" >>old file
"1465LY1 .201805110912.txt" >>old file
"1465LY1 .201805111419.txt" >>----new file

"17NX232 .201801250054.txt" >>old file
"17NX232 .201801260055.txt" >>old file
"17NX232 .201801270055.txt" >>old file
"17NX232 .201801280056.txt" >>old file
"17NX232 .201801290057.txt" >>old file
"17NX232 .201801300058.txt" >>----new file

我想要这个结果

ls
10MX232 .201805120846.txt
1465LY1 .201805111419.txt
17NX232 .201801300058.txt

【问题讨论】:

  • 欢迎来到 Stack Overflow。请展示您解决此问题的最新非工作尝试。通常,查看代码的修复比阅读其他人的解决方案更有指导意义。见stackoverflow.com/help/how-to-ask

标签: php linux filter


【解决方案1】:
run_path=./testlog/
declare -a file_nm
declare -a file_id
declare -i x
declare -i id_count
x=0

ls -1 $run_path | while read -r idpc; do
 x+=1
 idpc2=$(echo $idpc | cut -c1-7)
 file_id[$x]=$idpc2
 file_nm[$x]=$idpc
 id_count=$(ls -l $run_path${file_id[x]}* | grep -v ^l | wc -l)
 if [ "$id_count" -ge 2 ];then
  rm $(echo "$run_path${file_nm[x]}")
 fi
done

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    相关资源
    最近更新 更多