删除除最后一个文件以外的旧文件系列

时间:2018-05-15 07:29:36

标签: php linux filter

文件按类别列出。 以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

1 个答案:

答案 0 :(得分:0)

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