Delete those pesky ".DS_Store" files



If you use OS X then you might be familiar with the infamous hidden ".DS_Store" files that are produced as a result of using Finder.

The challenge is to write code that will go through the entire file system and delete any file named ".DS_Store".

Classic code golf - shortest code wins.


  • The code does not need to delete any .DS_Store files that require superuser; assume no such files exist.

  • The code should only delete the relevant .DS_Store files and nothing else.


Posted 2013-05-30T21:24:15.953

Reputation: 2 142

3Does the code need to delete .DS_Store files that require superuser (do I need to invoke sudo)? – Kaya – 2013-05-30T22:08:14.357

1@Kaya Good question: no it does not need to delete such files. – arshajii – 2013-05-30T22:12:33.640


Is it okay to delete files not named .DS_Store also؟

– Joey Adams – 2013-05-31T01:55:13.977

And again a chameleon question. – Johannes Kuhn – 2013-05-31T11:56:22.720

1@JohannesKuhn The latest edit should have been self-evident, I would assume. I added it for the sake of completeness. But I appreciate the clever answer, nonetheless! – arshajii – 2013-05-31T12:14:45.547

1@Joey — Transpose the question into the military domain — for example, hmm… the military of the USA, at random ^^ . And replace “file named ".DS_Store"” with “person to kill”. And you will see that the requirement “only” is far from being evident. – Nicolas Barbulesco – 2014-05-30T13:46:46.160



zsh, 19

rm -f /**/.DS_Store

(Make that 16 if it's ok to leave out -f.)


Posted 2013-05-30T21:24:15.953

Reputation: 6 466


Bash*, 40 30

find / -name .DS_Store -delete

find / -name .DS_Store -exec rm -f {} \;

This should handle it (not very golfed, marvelously slow). Bash seems right because we don't have to deal with any "import system" nonsense. If you want to require execution in any environment, add 4 chars for bash and 1 for a line feed.

*any shell I guess, just can't get out of the habit assuming the bourne again shell is the only one.


Posted 2013-05-30T21:24:15.953

Reputation: 710

1Bash is exactly the right tool for that. – Johannes Kuhn – 2013-05-30T22:32:12.997

1What kind of find has OS X? With GNU find is even shorter: find / -name .DS_Store -delete. – manatwork – 2013-05-31T05:42:28.227

Umm, find does have -delete yes. I guess that I assumed it wouldn't act as the -f option does with rm. After a test, it is most satisfactory--Cheers. – Kaya – 2013-05-31T05:54:59.250

1Subject to “Argument list too long” error and requires either bash 4 or newer with globstar shell option turned on, or zsh: rm /**/.DS_Store. – manatwork – 2013-05-31T10:13:53.000

GNU bash, version 3.2.48 is what I am currently using courtesy of osx.6 – Kaya – 2013-05-31T14:28:42.900

1You're not doing anything bash-specific, so it could as well be sh. – Peter Taylor – 2013-06-03T09:05:36.287

10It removed my .DS_Store directory where I had important stuff. – Konrad Borowski – 2013-06-14T12:19:28.057

@KonradBorowski Why would you name a directory .DS_Store, and expect a program explicitly designed to delete things named .DS_Store to not delete that directory? – pppery – 2019-08-21T21:06:59.450


Ruby: 38 33 characters

File.delete *Dir['/**/.DS_Store']


Posted 2013-05-30T21:24:15.953

Reputation: 17 865

2Dir[...] is a shortcut for Dir.glob(...), this will save you 5 chars. – Patrick Oscity – 2013-05-31T17:34:07.673

3Argh! I knew there is a shorter way, but I searched for it in Kernel. Thank you, @padde. – manatwork – 2013-05-31T17:38:54.627


fish, 16

rm /**/.DS_Store

(Make that 19 if -f is required to delete .DS_Store in read-only directories.)

Pierre Carrier

Posted 2013-05-30T21:24:15.953

Reputation: 191


Shell, 25

locate .DS_Store|xargs rm

Requires working locate database.


Posted 2013-05-30T21:24:15.953

Reputation: 101

3Also requires paths without whitespace characters. – manatwork – 2013-06-17T07:16:48.537


Python 3, 80 bytes

import os
for r,_,f in os.walk('/'):
 if d in f:os.remove(r+'/'+d)

Try it online!

By no means competable, but just for the sake of it :)


Posted 2013-05-30T21:24:15.953

Reputation: 635


CMD, 17

del/s "/.DS_Store

Pretty straight-forward.

/s is required for searching within subdirectories as well.
" is used to escape the / char which would otherwise be interpreted as an (invalid) argument.


Posted 2013-05-30T21:24:15.953



find, 38

The previus find solution is plain wrong: it's mean to delete everything named .DS_Store including directory, links, special file etc.


find / -type f -name .DS_Store -delete

it's the minimal solution using find.

DISCLAMER: I strongly advice against really using this solution on a real *nix box: trying to delete something recursively from / is a very bad idea. You are warned.


If the differences among files, directorys, links (hard and symbolic), named pipes, sockets, special files, pseudo-file systems is not clear to you, I suggest to google some of this unknown terms. You'll be surprise, wiser, and less prone to (catstrofic) errors.

EDIT2 : Even more relevant : the OP wrote "The code should only delete the relevant .DS_Store files and nothing else." : so, if someone care to read the boring requirements, all solutions that remove more than this files hare funny, smart, ect. but wrong.


Posted 2013-05-30T21:24:15.953

Reputation: 357

The previous find solution is correct. The question says “delete any file named ".DS_Store"”. So no special requirement based on file type. – manatwork – 2013-06-20T13:27:11.097

@ manatwork : nonsense: a file is a different thing from a directory. Along your twisted line, the best solution is still rm -rf / – DavAlPi – 2013-06-20T13:32:27.167

and thanks for downvote me: you guys do it because you dont know the difference between a file and a directory or it's just because I'm unpleasant to you ? – DavAlPi – 2013-06-20T13:37:02.527

@DavAlPi: Your solution won't delete named pipes, sockets, directories, and other special files. .DS_Store files are normally normal files though, so here's a +1 from me. – None – 2013-06-23T21:55:12.930

@ Evan: In a unix filesystem everthing is a file except directories, named pipe, sockets, and other special files that are NOT files. Try to google some of this unknown terms, you'll be surprise (and wiser). – DavAlPi – 2013-06-25T07:10:35.463

oh, I forgot to mention file system links (hard and symbolic). But certainly by now you have googled etc. etc. :) – DavAlPi – 2013-06-25T07:17:20.387


Ohh, you did not google:

– Johannes Kuhn – 2013-06-25T08:49:15.027

3Ohh, and an other thing: In code golf the goal is to get the shortest solution, so if you can save a few chars by trating the requirements in your favor, well, so be it. You could save 8 chars by treating everything as file, like unix does. PS: where was the surprise? The fact that some people deny the basic UNIX principe? – Johannes Kuhn – 2013-06-25T09:08:45.737

Dear Johannes, the OP wrote "The code should only delete the relevant .DS_Store files and nothing else." : The requirements rules. – DavAlPi – 2013-06-25T14:33:31.893

johannes, have a nice day :-) – DavAlPi – 2013-06-25T17:33:05.220


Now I got a better answer , something that everyone here can understand.

– DavAlPi – 2013-07-05T10:59:15.673