[英]Delete all files except the latest 10 in subdirectories
I have some BackUp-Files from machines which store their Backup in different folders. 我有一些来自计算机的备份文件,这些文件将其备份存储在不同的文件夹中。 Additionally the files are not created at the same time (Machine 1: every sunday, Machine 2: every first monday of the month, etc.).
此外,在同一时间(:每个星期天,机器2:每月的第一个星期一,等机1)不创建的文件。
I need to keep the latest 10 files in each folder and delete all the others. 我需要在每个文件夹中保留最新的10个文件,并删除所有其他文件。 Because of the different backup-intervals I can't just delete all files older than x days.
由于备份间隔不同,我不能只删除所有早于x天的文件。
The folder-structure is like this: 文件夹结构如下:
./<SystemType>/<FQDN_Machine1>/backup_2015_09_08_02_00_00.zip
./<SystemType>/<FQDN_Machine2>/backup_2015_09_01_14_00_00.zip
IFS='
'
for i in dir/*; do
ls -d1t $i/* | head -n-10
done | xargs rm
List all subdirs excluding latest ten and send it by xargs
to rm
. 列出除最新十个以外的所有子目录,并通过
xargs
将其发送到rm
。
This is my solution: 这是我的解决方案:
#!/bin/bash
cat find ./ -type f | while IFS= read -r line
do
find "$line" -type f | head -n -10 | while read file
do
rm -f "$file"
done
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.