简体   繁体   English

从 Mac 上的命令行删除当前文件夹和所有子文件夹中的 .DS_STORE 文件

[英]Delete .DS_STORE files in current folder and all subfolders from command line on Mac

I understand I can use find . -name ".DS_Store"我知道我可以使用find . -name ".DS_Store" find . -name ".DS_Store" to find all the .DS_Store files in the current folder and all subfolders. find . -name ".DS_Store" 查找当前文件夹和所有子文件夹中的所有 .DS_Store 文件 But how could I delete them from command line simultaneously?但是我怎么能同时从命令行中删除它们呢? I found it's really annoying to switch back and forth to all folders and delete it one by one.我发现来回切换所有文件夹并一个一个删除它真的很烦人。

find can do that. find可以做到这一点。 Just add -delete :只需添加-delete

find . -name ".DS_Store" -delete

Extend it even further to also print their relative paths进一步扩展它以打印它们的相对路径

find . -name ".DS_Store" -print -delete

For extra caution, you can exclude directories and filter only for files为了格外小心,您可以排除目录并仅过滤文件

find . -name ".DS_Store" -type f -delete
find . -name ".DS_Store" -print -delete

这将删除当前路径中所有名为.DS_Store的文件,同时显示它们的相对路径

Here is how to remove recursively the .DS_Store file这是递归删除.DS_Store文件的方法

Open up Terminal In the command line, go to the location of the folder where all files and folders are:打开终端在命令行中,转到所有文件和文件夹所在文件夹的位置:

cd to/your/directory

Then finally, type in the below command:最后,输入以下命令:

find . -name '.DS_Store' -type f -delete

Press Enter按回车

Cheers!!干杯!!

You can also use extended globbing ( ** ):您还可以使用扩展通配符 ( ** ):

rm -v **/.DS_Store

in zsh, bash 4 and similar shells (if not enabled, activate by: shopt -s globstar ).在 zsh、bash 4 和类似的 shell 中(如果未启用,请通过以下方式激活: shopt -s globstar )。

The best way to do this cleanly is using:干净地做到这一点的最好方法是使用:

find . -type f \( -name ".DS_Store" -o -name "._.DS_Store" \) -delete -print 2>&1 | grep -v "Permission denied"

This removes the files, hides "permission denied" errors (while keeping other errors), printing out a clean list of files removed.这将删除文件,隐藏“权限被拒绝”错误(同时保留其他错误),打印出删除文件的干净列表。

All the answers above work but there is a bigger problem if one is using mac and still on mac.上面的所有答案都有效,但如果一个人正在使用 mac 并且仍在 mac 上,则会出现更大的问题。 The described lines do delete all the DS_Store files but Finder recreates them immediately again because that is the default behaviour.所描述的行确实删除了所有DS_Store文件,但Finder 会立即再次重新创建它们,因为这是默认行为。 You can read about how this all workshere .您可以在此处阅读有关这一切的工作原理。 To quote from there if you are on mac, you should remove them only if you really need:如果您使用的是 Mac,请从那里引用,只有在您确实需要时才应删除它们:

If you don't have a particular reason to delete these .DS_Store files (windows sharing might be a solid reason,) it's best to leave them “as is.”如果您没有特别的理由删除这些 .DS_Store 文件(Windows 共享可能是一个可靠的理由),最好让它们保持“原样”。 There's no performance benefit in deleting .DS_Store files.删除 .DS_Store 文件没有性能优势。 They are harmless files that don't usually cause any problems.它们是无害的文件,通常不会引起任何问题。 Remember that the .DS_Store file saves your personalized folder settings, such as your icon arrangement and column sortings.请记住,.DS_Store 文件保存了您的个性化文件夹设置,例如您的图标排列和列排序。 And that's why you normally don't want to delete them but rather HIDE them.这就是为什么您通常不想删除它们而是隐藏它们的原因。

If you really do, there is one more way which was not mentioned here:如果你真的这样做了,还有一种方法在这里没有提到:

sudo find / -name “.DS_Store” -depth -exec rm {} \;

Make a new file with a text editor, copy and paste the following text into it, and save it with the ".sh" file extension, then open the file with Terminal.使用文本编辑器创建一个新文件,将以下文本复制并粘贴到其中,并以“.sh”文件扩展名保存,然后使用终端打开该文件。 Make sure the text editor is actually saving the raw text and not saving the file as a Rich Text Format file or some other text file format with additional information in the file.确保文本编辑器实际上保存了原始文本,而不是将文件保存为富文本格式文件或文件中包含附加信息的其他文本文件格式。

#!/bin/bash
echo -e "\nDrag a folder here and press the Enter or Return keys to delete all files whose names begin with a dot in its subfolders:\n"
read -p "" FOLDER
echo -e "\nThe following files will be deleted:\n"
find $FOLDER -name ".*"
echo -e "\nDelete these files? (y/n): "
read -p "" DECISION
while true
do
    case $DECISION in
        [yY]* ) find $FOLDER -name ".*" -delete
        echo -e "\nThe files were deleted.\n"
        break;;
        [nN]* ) echo -e "\nAborting without file deletion.\n"
        exit;;
        * ) echo -e "\nAborting without file deletion.\n"
        exit;;
    esac
done

这不完全是问题,但如果你想真正压缩没有它们的目录.DS_STORE文件,这是一种享受......

zip -r -X archive_name.zip folder_to_compress

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

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