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
  • 24
    \$\begingroup\$ Is it okay to delete files not named .DS_Store also؟ \$\endgroup\$ – Joey Adams May 31 '13 at 1:55
  • \$\begingroup\$ And again a chameleon question. \$\endgroup\$ – Johannes Kuhn May 31 '13 at 11:56
  • 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\$
51
\$\begingroup\$

Bash, 8

Inspired by Joey Adams comment

rm -rf /

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

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

bash, 9

A probabilistic logic approach:

sleep 9e9

Rationale: wait natural Hard Disk destruction:

Ispired by Johannes Kuhn answer.

References: Age of the universe

\$\endgroup\$
  • 6
    \$\begingroup\$ Absolutely brilliant! \$\endgroup\$ – gilbertohasnofb Mar 22 '14 at 0:30
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\$

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 :)

\$\endgroup\$
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – daniero Jun 22 '13 at 22:08
  • 3
    \$\begingroup\$ xargs, properly used (find -print0 | xargs -0), is perfectly safe (as far as ugly names are concerned). \$\endgroup\$ – Kevin Jun 22 '13 at 22:48
-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.