简体   繁体   English

删除除每个前缀的最近n个文件以外的所有文件(基于文件日期)?

[英]Delete all except the most recent n files of each prefix (based on file date)?

I'm running this command in my script, and it's deleting all but 5 of each file, but it's not keeping the last 5 files based on date: 我正在脚本中运行此命令,它将删除每个文件中除5个文件以外的所有文件,但不会根据日期保留最后5个文件:

ls -rv | awk -F_ '++n[$1]>5' | xargs rm

**underscore is the delimiter between the prefix (hostname) and the filenames. **下划线是前缀(主机名)和文件名之间的分隔符。

I've tried some other stuff too, but it didn't seem to do the trick. 我也尝试过其他一些东西,但似乎并不能解决问题。

I'm running this to delete all but the last n files in a folder and it works correctly: 我正在运行此命令以删除文件夹中除最后n个文件以外的所有文件,它可以正常运行:

rm -f $(ls -1t -I "*.sh" | tail -n +21)

This applies to everything though. 这适用于所有内容。

I have another folder full of backups from various devices that all have hostname prefixes. 我还有另一个文件夹,其中充满了来自所有具有主机名前缀的各种设备的备份。 The format is: hostname_config.backup-timestamp. 格式为:hostname_config.backup-timestamp。

@rici solved it. @rici解决了它。 Dumping the -rv flags and using -t created the behavior I was looking for. 转储-rv标志并使用-t创建了我想要的行为。

ls -t | awk -F_ '++n[$1]>5' | xargs rm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM