php - 删除除最后一个文件之外的旧文件系列

标签 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

最佳答案

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

关于php - 删除除最后一个文件之外的旧文件系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50344612/

相关文章:

android - USB 调试未列出我的设备

python - 如何在 Django 中结合过滤和分页?

php - 选择要发送电子邮件的按钮

php - 从 Array PHP 插入表

php - 抑制生产服务器上的 php 错误的最佳方法

linux - 我们如何在 AWS 中基于 ubuntu 的 EC2 机器中永久设置环境变量?

linux - 如何确定纯系统文件系统?

iphone - 将 UIImage 转换为黑白而非灰度以使用超正方体

PHP 查询过滤多对多 MySQL

php - 使用 Yii 中的 updateAll 方法按某个数字更新字段值