简体   繁体   English

递归删除所有目录中除一个最新文件之外的所有文件

[英]Recursively delete all but the one newest file throughout all directories

My system depends on only having one file (PDF, DOCX) per subdirectory.我的系统依赖于每个子目录只有一个文件(PDF、DOCX)。 There are thousands and thousands of subdirectories.有成千上万的子目录。 Due to a permission error, in some of them, I have ended up with more than one file.由于权限错误,在其中一些文件中,我得到了多个文件。 In these instances, I only want to keep the one most recently modified file.在这些情况下,我只想保留一个最近修改的文件。

I was able to export a list of directories that contain more than one file successfully:我能够成功导出包含多个文件的目录列表:

find . -type f -printf '%h\n' | sort | uniq -d >test.txt

So I end up with a nice list of all those directories that I need to look at.因此,我最终得到了一个很好的列表,列出了我需要查看的所有目录。 But it's rather long.但是比较长。

I was also able to automate the deletion of everything but the most recently modified file in a directory:除了目录中最近修改的文件之外,我还能够自动删除所有内容:

ls -t | tail -n +2 | xargs -d '\n' rm -f

That does remove all files but the most recently modified one.这确实删除了所有文件,但最近修改的文件除外。

The problem I am running into is that the second command only works within that directory.我遇到的问题是第二个命令仅在该目录中有效。 I have not figured out a way to apply it recursively to all directories.我还没有想出一种将它递归地应用于所有目录的方法。

I have attempted:我尝试过:

find /data/test/CONTAINER/SANDBOX -type f -exec sh -c 'ls -t | tail -n +2 | xargs -d '\n' rm -f ' {} \;

but that just yielded xargs: argument line too long但这只是产生了 xargs: argument line too long

I have tried to adjust the xargs parameters, but I am sure there must be a better way to perform this?我曾尝试调整 xargs 参数,但我确信一定有更好的方法来执行此操作? Perhaps a shell scrip that pipes the test.txt file fo the folders to cd into and then perform command two in each of these?也许一个 shell 脚本将文件夹的 test.txt 文件通过管道传输到 cd 中,然后在其中执行命令二? Or simply a way to recursively apply command 2 to all subfolders, regardless of how many files are contained within that folder?或者只是一种将命令 2 递归应用到所有子文件夹的方法,而不管该文件夹中包含多少文件?

The last thing I was thinking of is that perhaps the command 3 I had tried applies from the main directory, where I have hundreds of thousands of directories, no wonder the argument line could be too long - but -mindepth 2 didnt change a thing.我想到的最后一件事是,也许我尝试过的命令 3 适用于我有数十万个目录的主目录,难怪参数行可能太长 - 但 -mindepth 2 并没有改变任何事情。

Thank you谢谢

I think the following script should do the trick for you.我认为以下脚本应该为您解决问题。

#!/bin/bash

DIR_TO_FIND="/path/to/dir"

find "$DIR_TO_FIND" -type d | while read -r DIR; do
    cd "$DIR"
    ls -t | tail -n +2 | xargs -d '\n' rm -f
    cd "$DIR_TO_FIND" 
done

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

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