简体   繁体   English

创建一个bash脚本以删除不包含特定文件类型的文件夹

[英]Create a bash script to delete folders which do not contain a certain filetype

I have recently run into a problem. 我最近遇到了一个问题。

I used a utility to move all my music files into directories based on tags. 我使用实用程序将所有音乐文件移动到基​​于标签的目录中。 This left a LOT of almost empty folders. 这留下了很多几乎空的文件夹。 The folders, in general, contain a thumbs.db file or some sort of image for album art. 通常,文件夹包含thumbs.db文件或用于专辑封面的某种图像。 The mp3s have the correct album art in their new directories, so the old ones are okay to delete. mp3在他们的新目录中有正确的专辑封面,所以旧的可以删除。

Basically, I need to find any directories within D:/Music/ that: 基本上,我需要在D:/ Music /中找到任何目录:

-Do not have any subdirectories - 没有任何子目录

-Do not contain any mp3 files - 不包含任何mp3文件

And then delete them. 然后删除它们。

I figured this would be easier to do in a shell script or bash script or whatever else linux/unix world than in Windows 8.1 (HAHA). 我认为这在shell脚本或bash脚本或其他任何linux / unix世界中比在Windows 8.1(HAHA)中更容易。

Any suggestions? 有什么建议? I'm not very experienced writing scripts like this. 我写这样的剧本并不是很有经验。

This should get you started 这应该让你开始

find /music -mindepth 1 -type d |
while read dt
do
  find "$dt" -mindepth 1    -type d | read && continue
  find "$dt" -iname '*.mp3' -type f | read && continue
  echo DELETE $dt
done

Here's the short story... 这是短篇小说......

find . -name '*.mp3' -o -type d -printf '%h\n' | sort | uniq > non-empty-dirs.tmp
find . -type d -print | sort | uniq > all-dirs.tmp
comm -23 all-dirs.tmp non-empty-dirs.tmp > dirs-to-be-deleted.tmp

less dirs-to-be-deleted.tmp

cat dirs-to-be-deleted.tmp | xargs rm -rf

Note that you might have to run all the commands a few times (depending on your repository's directory depth) before you're done deleting all recursive empty directories... 请注意,在完成删除所有递归空目录之前,可能必须运行几次所有命令(取决于存储库的目录深度)...

And the long story goes... 长话故事......

You can approach this problem from two basic perspective: either you find all directories, then iterate over each of them, check if it contain any mp3 file or any subdirectory, if not, mark that directory for deletion. 您可以从两个基本角度处理此问题:要么找到所有目录,然后遍历每个目录,检查它是否包含任何mp3文件或任何子目录,如果没有,请将该目录标记为删除。 It will works, but on large very large repositories, you might expect a significant run time. 它可以工作,但是在大型非常大的存储库中,您可能会期望大量的运行时间。

Another approach, which is in my sense much more interesting, is to build a list of directories NOT to be deleted, and subtract that list from the list of all directories. 另一种方法,在我看来更有趣,是建立一个不被删除的目录列表,并从所有目录列表中减去该列表。 Let's work the second strategy, one step at a time... 让我们一起做第二个策略,一步一步......

First of all, to find the path of all directories that contains mp3 files, you can simply do: 首先,要查找包含mp3文件的所有目录的路径,您可以简单地执行以下操作:

find . -name '*.mp3' -printf '%h\n' | sort | uniq

This means "find any file ending with .mp3, then print the path to it's parent directory". 这意味着“找到以.mp3结尾的任何文件,然后打印到其父目录的路径”。

Now, I could certainly name at least ten different approaches to find directories that contains at least one subdirectory, but keeping the same strategy as above, we can easily get... 现在,我当然可以命名至少十种不同的方法来查找包含至少一个子目录的目录,但是保持与上面相同的策略,我们可以很容易地得到......

find . -type d -printf '%h\n' | sort | uniq

What this means is: "Find any directory, then print the path to it's parent." 这意味着:“找到任何目录,然后打印它的父路径。”

Both of these queries can be combined in a single invocation, producing a single list containing the paths of all directories NOT to be deleted.. Let's redirect that list to a temporary file. 这两个查询都可以在一次调用中组合,生成一个包含所有不被删除目录的路径的列表。让我们将该列表重定向到一个临时文件。

find . -name '*.mp3' -o -type d -printf '%h\n' | sort | uniq > non-empty-dirs.tmp

Let's similarly produce a file containing the paths of all directories, no matter if they are empty or not. 让我们类似地生成一个包含所有目录路径的文件,无论它们是否为空。

find . -type d -print | sort | uniq > all-dirs.tmp

So there, we have, on one side, the complete list of all directories, and on the other, the list of directories not to be deleted. 所以,我们一方面有所有目录的完整列表,另一方面,我们有不要删除的目录列表。 What now? 现在怎么办? There are tons of strategies, but here's a very simple one: 有很多策略,但这里有一个非常简单的策略:

comm -23 all-dirs.tmp non-empty-dirs.tmp > dirs-to-be-deleted.tmp

Once you have that, well, review it , and if you are satisfied, then pipe it through xargs to rm to actually delete the directories. 一旦你有了,那么, 检查它 ,如果你满意,然后通过xargs管道到rm实际删除目录。

cat dirs-to-be-deleted.tmp | xargs rm -rf

暂无
暂无

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

相关问题 保留最近的3个文件夹,然后在bash脚本中删除其余的文件夹? - Keep the recent 3 folders and delete the rest in bash script? 如何制作 bash 脚本,在其中我可以将某些文件移动到基于文件中的字符串命名的某些文件夹? - How can I make a bash script where I can move certain files to certain folders which are named based on a string in the files? bash脚本来创建文件夹和移动文件 - bash script to create folders and move files bash housekeeing脚本需要权限才能删除不同的文件夹和文件夹中的文件 - bash housekeeing script needed permission to delete different folders and files in folder Bash脚本以递归方式逐步浏览文件夹并删除文件 - Bash script to recursively step through folders and delete files 在bash脚本中基于名称作为日期删除文件夹(以mm / dd / yy为单位) - delete folders based on name as date in mm/dd/yy in bash script 如何在bash上使用filetype将文件排序到文件夹中(使用'file'命令)? - How to sort files into folders by filetype on bash (with 'file' command)? 查找并删除不包含某些文件类型的所有目录 - Find and delete all directories that do NOT contain certain file types 如何使用 bash 脚本通过比较文件夹名称和系统时间戳作为参考来删除多个文件夹? - How to delete multiple folders by comparing folder name and system timestamp as reference, using a bash script? 使用 Bash 脚本从文本文件中创建/删除用户 - Create/delete users from text file using Bash script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM