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

Clarifications:

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

• Does the code need to delete .DS_Store files that require superuser (do I need to invoke sudo)? – Kaya May 30 '13 at 22:08
• @Kaya Good question: no it does not need to delete such files. – arshajii May 30 '13 at 22:12
• Is it okay to delete files not named .DS_Store also؟ – Joey Adams May 31 '13 at 1:55
• And again a chameleon question. – Johannes Kuhn May 31 '13 at 11:56
• @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 May 30 '14 at 13:46

# zsh, 19

rm -f /**/.DS_Store


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

## Bash, 8

rm -rf /


Has some bad side effects, but who cares, job done.
CW because it is not my idea.

• Doesn't work: rm: it is dangerous to operate recursively on /', use --no-preserve-root to override this failsafe – Konrad Borowski Jun 14 '13 at 11:51
• @GlitchMr Then you are using the wrong version. Downgrade. – Johannes Kuhn Jun 14 '13 at 11:56
• I think I'm in love with this site. – user8429 Jun 23 '13 at 21:48
• this answer can "solve" a lot of puzzles – bgs Jun 25 '13 at 2:26
• What a good belly laugh! – ojblass Jul 6 '13 at 19:32

# bash, 9

A probabilistic logic approach:

sleep 9e9


Rationale: wait natural Hard Disk destruction:

References: Age of the universe

• Absolutely brilliant! – gilbertohasnofb Mar 22 '14 at 0:30

### Bash*, 40 30

find / -name .DS_Store -delete


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


This should handle it (not very golfed, not.to.mention 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.

• Bash is exactly the right tool for that. – Johannes Kuhn May 30 '13 at 22:32
• What kind of find has OS X? With GNU find is even shorter: find / -name .DS_Store -delete. – manatwork May 31 '13 at 5:42
• Subject 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 May 31 '13 at 10:13
• You're not doing anything bash-specific, so it could as well be sh. – Peter Taylor Jun 3 '13 at 9:05
• It removed my .DS_Store directory where I had important stuff. – Konrad Borowski Jun 14 '13 at 12:19

# Ruby: 38 33 characters

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

• Dir[...] is a shortcut for Dir.glob(...), this will save you 5 chars. – Patrick Oscity May 31 '13 at 17:34
• Argh! I knew there is a shorter way, but I searched for it in Kernel. Thank you, @padde. – manatwork May 31 '13 at 17:38

# fish, 16

rm /**/.DS_Store


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

# Shell, 25

locate .DS_Store|xargs rm


Requires working locate database.

• Also requires paths without whitespace characters. – manatwork Jun 17 '13 at 7:16

We should be far more specific with find and not use xargs as it can be dangerous if there are spaces in the path which could lead people to inadvertently nuking stuff they did not want to remove. Here's a good and specific best practice method for a type of scenario like this:

since we don't require super user we'll assume this is for only one user. So I'll be using the username foobar as the example username in my command.

find ~ -user foobar -type f -name '.DS_Store' -exec rm -fv {} \;

We're being user specific, and not using xargs(which is dangerous with mass deletions). You will also find that this will run much faster :)

• In codegolf nobody cares about safety or run time (despite the newly added clarifications in the question, look what answer has the most votes). Besides, the challenge is to go though the entire file system. – daniero Jun 22 '13 at 22:08
• xargs, properly used (find -print0 | xargs -0), is perfectly safe (as far as ugly names are concerned). – Kevin Jun 22 '13 at 22:48

# find, 38

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

But:

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.

EDIT :

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.

• The previous find` solution is correct. The question says “delete any file named ".DS_Store"”. So no special requirement based on file type. – manatwork Jun 20 '13 at 13:27
• @ manatwork : nonsense: a file is a different thing from a directory. Along your twisted line, the best solution is still rm -rf / – DavAlPi Jun 20 '13 at 13:32
• Ohh, you did not google: en.wikipedia.org/wiki/Everything_is_a_file – Johannes Kuhn Jun 25 '13 at 8:49
• Ohh, 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 Jun 25 '13 at 9:08
• Now I got a better answer , something that everyone here can understand. – DavAlPi Jul 5 '13 at 10:59