DEV Community

Cover image for How to recursively remove .DS_Store
Abubakar Hassan
Abubakar Hassan

Posted on

How to recursively remove .DS_Store

Lately, I've been doing a lot of development on Mac. With that comes a lot of use of the Finder app.

In order keep your repo clean, you need to remove .DS_Store files from the folders created using the Mac GUI. Doing that by hand is really boring, painful, and repetitive. Enter that awesome thing we call the command line.

Recursively Remove .DS_Store

Open up Terminal

In the command line, type:

cd to/your/directory - then press enter.

Finally, in the command line, type:

find . -name '.DS_Store' -type f -delete - then press enter.

Warning: Never use a wildcard (*) with this command, unless you know what you're doing. Bad things can happen if you don't.

This snippet will remove .DS_Store files from the selected folder, as well as all of the folders that it contains. All of them. At once.

This command can be used for other types of files, as well. Just replace '.DS_Store' with whatever file name, or type, that you want to delete.

Other uses

You can do this for other file types, as well.

For example, you could use the below to delete all JavaScript files within a containing folder.

find . -name '*.js' -type f -delete

Note that the asterisk is a wildcard, meaning the Terminal is looking for any file with an extension of .js... it can be dangerous to use wildcards...

be careful.

Top comments (7)

Collapse
 
wraldpyk profile image
Rene Pot

There isn't really a need to remove the files, just add the .DS_Store file to .gitignore and you'll never see it in a repository again.

Collapse
 
itssadon profile image
Abubakar Hassan • Edited

You're right. However, adding .DS_Store to .gitignore after it has been checked in doesn't remove it from the repository. You'll have to use git rm --cached *.DS_Store to remove them.

Collapse
 
wraldpyk profile image
Rene Pot

Yes! Very true!

Collapse
 
seanr profile image
Sean Robertson

In my case, they're not in git, but they can get created in my local copy of the application source and I don't want them copied into in my compiled build. That's a case where I may just want to add this to my build script (I wrote a custom bash script to automate my Bforartists builds).

Collapse
 
jonmcl profile image
Jon McLaughlin 🍄

dot_clean .

Collapse
 
mountlav profile image
mountlav

Thank you!❤️

Collapse
 
itssadon profile image
Abubakar Hassan

You're welcome