删除除最后一个匹配项外的所有匹配项



我想删除目录中每个文件夹中存在的与file*匹配的一组文件中除最后一个匹配外的所有文件。

例如:

Folder 1
file
file_1-1
file_1-2
file_2-1
stuff.txt
stuff
Folder 2
file_1-1
file_1-2
file_1-3
file_2-1
file_2-2
stuff.txt
Folder 3
...

等等。在每个子文件夹中,我只想保留最后一个匹配的文件,因此对于Folder 1,这将是file_2-1,在Folder 2中,它将是file_2-2。每个子文件夹中的文件数量通常不同。

因为我有一个非常舒适的文件夹结构,我想使用find命令像这样

find . -type f -name "file*" -delete_all_but_last_match

我知道如何删除所有匹配项,但不知道如何排除最后一个匹配项。

我还发现了下面一段代码:

https://askubuntu.com/questions/1139051/how-to-delete-all-but-x-last-items-from-find

但是当我将修改后的版本应用到测试文件夹

find . -type f -name "file*" -print0 | head -zn-1 | xargs -0 rm -rf

在大多数情况下删除所有匹配项,只有在某些情况下最后一个文件被保留。所以它对我不起作用,大概是因为每个文件夹中的文件数量不同。

编辑:

文件夹不包含进一步的子文件夹,但它们通常位于几个子文件夹级别的末尾。因此,如果脚本可以在上面的某些级别执行,那将是一个好处。

#!/bin/bash
shopt -s globstar
for dir in **/; do 
files=("$dir"file*)
unset 'files[-1]'
rm "${files[@]}"
done

尝试使用awk和xargs的以下解决方案:

find . -type f -name "file*" | awk -F/ '{ map1[$(NF-1)]++;map[$(NF-1)][map1[$(NF-1)]]=$0 }END { for ( i in map ) { for (j=1;j<=(map1[i]-1);j++) { print """map[i][j]""" } } }' | xargs rm

解释:

find . -type f -name "file*" | awk -F/ '{                               # Set the field delimiter to "/" in awk
map1[$(NF-1)]++;                                     # Create an array map1 with the sub-directory as the index and an incrementing counter the value (number of files in each sub-directory)
map[$(NF-1)][map1[$(NF-1)]]=$0                       # Create a two dimentional array with the sub directory index one and the file count the second. The line the value
}
END { 
for ( i in map ) { 
for (j=1;j<=(map1[i]-1);j++) { 
print """map[i][j]"""                         # Loop through the map array utilising map1 to get the last but one file and printing the results
} 
} 
}' | xargs rm                                     # Run the result through xargs rm

删除xargs的管道,以验证文件是否按预期列出,然后再添加回实际删除文件。

最新更新