Mac 从命令行删除当前文件夹和所有子文件夹中的 .DS_STORE 个文件
Delete .DS_STORE files in current folder and all subfolders from command line on Mac
我知道我可以在当前文件夹和所有子文件夹中使用 find . -name ".DS_Store"
到 find all the .DS_Store files。但是我怎么能同时从命令行删除它们呢?发现所有文件夹来回切换,一个一个删除,真的很烦人
find
可以做到。只需添加 -delete
:
find . -name ".DS_Store" -delete
进一步扩展它以打印它们的相对路径
find . -name ".DS_Store" -print -delete
为格外小心,您可以排除目录并仅过滤文件
find . -name ".DS_Store" -type f -delete
您还可以使用扩展的 globbing (**
):
rm -v **/.DS_Store
在 zsh、bash 4 和类似的 shell 中(如果未启用,请通过以下方式激活:shopt -s globstar
)。
用文本编辑器创建一个新文件,将以下文本复制并粘贴到其中,并以“.sh”文件扩展名保存,然后用终端打开该文件。确保文本编辑器实际保存的是原始文本,而不是将文件保存为 RTF 格式文件或文件中包含附加信息的其他文本文件格式。
#!/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
find . -name ".DS_Store" -print -delete
这将删除当前路径中所有名为 .DS_Store
的文件,同时显示它们的相对路径
以下是递归删除 .DS_Store
文件的方法
打开终端
在命令行中,转到所有文件和文件夹所在的文件夹位置:
cd to/your/directory
最后,输入以下命令:
find . -name '.DS_Store' -type f -delete
按回车键
干杯!!
干净地执行此操作的最佳方法是使用:
find . -type f \( -name ".DS_Store" -o -name "._.DS_Store" \) -delete -print 2>&1 | grep -v "Permission denied"
这将删除文件,隐藏“权限被拒绝”错误(同时保留其他错误),打印出已删除文件的干净列表。
这不完全是问题所在,但如果您想在没有 .DS_STORE
文件的情况下实际压缩目录,这很有用...
zip -r -X archive_name.zip folder_to_compress
上面的所有答案都有效,但如果使用 mac 并且仍在使用 mac,则会出现更大的问题。所描述的行 do 删除所有 DS_Store
文件,但 Finder 会立即重新创建它们 因为这是默认行为。您可以阅读了解这一切的工作原理 here。如果您在 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.” There’s no performance benefit in deleting .DS_Store files. 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. And that’s why you normally don’t want to delete them but rather HIDE them.
如果你真的这样做,还有一种这里没有提到的方法:
sudo find / -name “.DS_Store” -depth -exec rm {} \;
我知道我可以在当前文件夹和所有子文件夹中使用 find . -name ".DS_Store"
到 find all the .DS_Store files。但是我怎么能同时从命令行删除它们呢?发现所有文件夹来回切换,一个一个删除,真的很烦人
find
可以做到。只需添加 -delete
:
find . -name ".DS_Store" -delete
进一步扩展它以打印它们的相对路径
find . -name ".DS_Store" -print -delete
为格外小心,您可以排除目录并仅过滤文件
find . -name ".DS_Store" -type f -delete
您还可以使用扩展的 globbing (**
):
rm -v **/.DS_Store
在 zsh、bash 4 和类似的 shell 中(如果未启用,请通过以下方式激活:shopt -s globstar
)。
用文本编辑器创建一个新文件,将以下文本复制并粘贴到其中,并以“.sh”文件扩展名保存,然后用终端打开该文件。确保文本编辑器实际保存的是原始文本,而不是将文件保存为 RTF 格式文件或文件中包含附加信息的其他文本文件格式。
#!/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
find . -name ".DS_Store" -print -delete
这将删除当前路径中所有名为 .DS_Store
的文件,同时显示它们的相对路径
以下是递归删除 .DS_Store
文件的方法
打开终端 在命令行中,转到所有文件和文件夹所在的文件夹位置:
cd to/your/directory
最后,输入以下命令:
find . -name '.DS_Store' -type f -delete
按回车键
干杯!!
干净地执行此操作的最佳方法是使用:
find . -type f \( -name ".DS_Store" -o -name "._.DS_Store" \) -delete -print 2>&1 | grep -v "Permission denied"
这将删除文件,隐藏“权限被拒绝”错误(同时保留其他错误),打印出已删除文件的干净列表。
这不完全是问题所在,但如果您想在没有 .DS_STORE
文件的情况下实际压缩目录,这很有用...
zip -r -X archive_name.zip folder_to_compress
上面的所有答案都有效,但如果使用 mac 并且仍在使用 mac,则会出现更大的问题。所描述的行 do 删除所有 DS_Store
文件,但 Finder 会立即重新创建它们 因为这是默认行为。您可以阅读了解这一切的工作原理 here。如果您在 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.” There’s no performance benefit in deleting .DS_Store files. 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. And that’s why you normally don’t want to delete them but rather HIDE them.
如果你真的这样做,还有一种这里没有提到的方法:
sudo find / -name “.DS_Store” -depth -exec rm {} \;