14
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ And again a chameleon question. \$\endgroup\$ May 31, 2013 at 11:56
  • 1
    \$\begingroup\$ @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! \$\endgroup\$
    – arshajii
    May 31, 2013 at 12:14
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ May 30, 2014 at 13:46
  • 1
    \$\begingroup\$ I’m voting to close this question because questions which ask for malicious code are off topic and the described task counts as malicious per our guidelines. \$\endgroup\$
    – Wheat Wizard
    Jun 25, 2022 at 6:05

8 Answers 8

14
\$\begingroup\$

zsh, 19

rm -f /**/.DS_Store

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

\$\endgroup\$
12
\$\begingroup\$

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.

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

Ruby: 38 33 characters

File.delete *Dir['/**/.DS_Store']
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Dir[...] is a shortcut for Dir.glob(...), this will save you 5 chars. \$\endgroup\$ May 31, 2013 at 17:34
  • 3
    \$\begingroup\$ Argh! I knew there is a shorter way, but I searched for it in Kernel. Thank you, @padde. \$\endgroup\$
    – manatwork
    May 31, 2013 at 17:38
2
\$\begingroup\$

fish, 16

rm /**/.DS_Store

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

\$\endgroup\$
0
\$\begingroup\$

Shell, 25

locate .DS_Store|xargs rm

Requires working locate database.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Also requires paths without whitespace characters. \$\endgroup\$
    – manatwork
    Jun 17, 2013 at 7:16
0
\$\begingroup\$

Python 3, 80 bytes

import os
d='.DS_Store'
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 :)

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
-1
\$\begingroup\$

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.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.