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

LinuxMacosCommand Line

Linux Problem Overview


I understand I can use find . -name ".DS_Store" to find all the .DS_Store files in the current folder and all subfolders. 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.

Linux Solutions


Solution 1 - Linux

find can do that. Just add -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

Solution 2 - Linux

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

This will delete all the files named .DS_Store in the current path while also displaying their relative paths

Solution 3 - Linux

Here is how to remove recursively the .DS_Store file

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!!

Solution 4 - Linux

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).

Solution 5 - Linux

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.

Solution 6 - Linux

All the answers above work but there is a bigger problem if one is using mac and still on mac. The described lines do delete all the DS_Store files but Finder recreates them immediately again because that is the default behaviour. You can read about how this all works here. To quote from there if you are on mac, you should remove them only if you really need:

>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.

If you really do, there is one more way which was not mentioned here:

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

Solution 7 - Linux

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. 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

Solution 8 - Linux

This wasn't exactly the question, but if you wanna actually zip the directory without them .DS_STORE files, this works a treat...

zip -r -X archive_name.zip folder_to_compress

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSixSigmaView Question on Stackoverflow
Solution 1 - LinuxchaosView Answer on Stackoverflow
Solution 2 - LinuxKirill MalakhovView Answer on Stackoverflow
Solution 3 - LinuxTejinderView Answer on Stackoverflow
Solution 4 - LinuxkenorbView Answer on Stackoverflow
Solution 5 - LinuxfrankywView Answer on Stackoverflow
Solution 6 - LinuxMy WorkView Answer on Stackoverflow
Solution 7 - LinuxSparkyView Answer on Stackoverflow
Solution 8 - LinuxshrameeView Answer on Stackoverflow