12
\$\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\$
  • 3
    \$\begingroup\$ Does the code need to delete .DS_Store files that require superuser (do I need to invoke sudo)? \$\endgroup\$ – Kaya May 30 '13 at 22:08
  • 1
    \$\begingroup\$ @Kaya Good question: no it does not need to delete such files. \$\endgroup\$ – arshajii May 30 '13 at 22:12
  • 25
    \$\begingroup\$ Is it okay to delete files not named .DS_Store also؟ \$\endgroup\$ – Joey Adams May 31 '13 at 1:55
  • 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 '13 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\$ – Nicolas Barbulesco May 30 '14 at 13:46
13
\$\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\$
  • 1
    \$\begingroup\$ Bash is exactly the right tool for that. \$\endgroup\$ – Johannes Kuhn May 30 '13 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 '13 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 '13 at 10:13
  • 1
    \$\begingroup\$ You're not doing anything bash-specific, so it could as well be sh. \$\endgroup\$ – Peter Taylor Jun 3 '13 at 9:05
  • 10
    \$\begingroup\$ It removed my .DS_Store directory where I had important stuff. \$\endgroup\$ – Konrad Borowski Jun 14 '13 at 12:19
8
\$\begingroup\$

Ruby: 38 33 characters

File.delete *Dir['/**/.DS_Store']
\$\endgroup\$
  • 2
    \$\begingroup\$ Dir[...] is a shortcut for Dir.glob(...), this will save you 5 chars. \$\endgroup\$ – Patrick Oscity May 31 '13 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 '13 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\$
  • 3
    \$\begingroup\$ Also requires paths without whitespace characters. \$\endgroup\$ – manatwork Jun 17 '13 at 7:16
-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\$
  • \$\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 '13 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 '13 at 13:32
  • 2
    \$\begingroup\$ Ohh, you did not google: en.wikipedia.org/wiki/Everything_is_a_file \$\endgroup\$ – Johannes Kuhn Jun 25 '13 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\$ – Johannes Kuhn Jun 25 '13 at 9:08
  • 1
    \$\begingroup\$ Now I got a better answer , something that everyone here can understand. \$\endgroup\$ – DavAlPi Jul 5 '13 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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