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