用于递归遍历文件夹并删除文件的 Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3614394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:33:19  来源:igfitidea点击:

Bash script to recursively step through folders and delete files

linuxbash

提问by dnatoli

Can anyone give me a bash script or one line command i can run on linux to recursively go through each folder from the current folder and delete all files or directories starting with '._'?

谁能给我一个 bash 脚本或一行命令,我可以在 linux 上运行以递归遍历当前文件夹中的每个文件夹并删除所有以“._”开头的文件或目录?

回答by Brandon Horsley

Change directory to the root directory you want (or change .to the directory) and execute:

将目录更改为您想要的根目录(或更改.为目录)并执行:

find . -name "._*" -print0 | xargs -0 rm -rf

xargsallows you to pass several parameters to a single command, so it will be faster than using the find -execsyntax. Also, you can run this once without the |to view the files it will delete, make sure it is safe.

xargs允许您将多个参数传递给单个命令,因此它比使用find -exec语法更快。此外,您可以运行一次而无需|查看它将删除的文件,确保它是安全的。

回答by Vadim Shender

find . -name '._*' -exec rm -Rf {} \;

回答by Jan

Instead of deleting the AppleDouble files, you could merge them with the corresponding files. You can use dot_clean.

您可以将它们与相应的文件合并,而不是删除 AppleDouble 文件。您可以使用dot_clean.

dot_clean -- Merge ._* files with corresponding native files.

For each dir, dot_clean recursively merges all ._* files with their corresponding native files according to the rules specified with the given arguments. By default, if there is an attribute on the native file that is also present in the ._ file, the most recent attribute will be used.

If no operands are given, a usage message is output. If more than one directory is given, directories are merged in the order in which they are specified.

dot_clean -- 将 ._* 文件与相应的本机文件合并。

对于每个目录,dot_clean 根据给定参数指定的规则递归地将所有 ._* 文件与其对应的本机文件合并。默认情况下,如果 ._ 文件中也存在本机文件上的属性,则将使用最新的属性。

如果没有给出操作数,则输出使用消息。如果给出了多个目录,目录将按照指定的顺序合并。

Because dot_clean works recursively by default, use:

由于默认情况下 dot_clean 以递归方式工作,因此请使用:

dot_clean <directory>

If you want to turn off the recursively merge, use -ffor flat merge.

如果要关闭递归合并,请使用-ffor flat merge。

dot_clean -f <directory>

回答by houbysoft

I've had a similar problem a while ago (I assume you are trying to clean up a drive that was connected to a Mac which saves a lot of these files), so I wrote a simple python script which deletes these and other useless files; maybe it will be useful to you:

不久前我遇到了类似的问题(我假设您正在尝试清理连接到 Mac 的驱动器,该驱动器保存了很多这些文件),所以我写了一个简单的 python 脚本来删除这些和其他无用的文件; 也许它对你有用:

http://github.com/houbysoft/short/blob/master/tidy

http://github.com/houbysoft/short/blob/master/tidy

回答by ghostdog74

find /path -name "._*" -exec rm -fr "{}" +;

回答by KAction

find . -name '.*' -delete

find . -name '.*' -delete

A bit shorter and perform better in case of extremelylong list of files.

在文件列表非常长的情况下,更短一些并且性能更好。