Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to remove .DS_Store files on any *NIX Machine UNIX
There have been hints here about removing those pesky .DS_Store files, but many fail under certain conditions (usually when there are special characters in a file or folder name), or require BSD find. This script has been tested on Debian Linux (Woody, 3.0r1). It does NOT work on Jaguar (never tested on Panther), since it requires GNU find (tested w/ version 4.1.7).

This removes all .DS_Store files in the current directory and its subdirectories. rm gets a little upset if there are no such files present, but this is minor (really only a problem if you put it in a crontab or something like that).

#!/bin/bash
find . -name .DS_Store -printf \"%p\"\ \  | xargs rm
    •    
  • Currently 2.70 / 5
  You rated: 3 / 5 (10 votes cast)
 
[52,350 views]  

A script to remove .DS_Store files on any *NIX Machine | 22 comments | Create New Account
Click here to return to the 'A script to remove .DS_Store files on any *NIX Machine' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to remove .DS_Store files on any *NIX Machine
Authored by: gospodin_david on Nov 28, '03 11:56:09AM

Why not just use "find . -name .DS_Store -print -delete"?



[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: durin on Nov 28, '03 05:06:00PM

Because -delete isn't available in GNU find.

---

Go not to the elves for council, they will say both no and yes



[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: gospodin_david on Jan 07, '04 08:23:25AM

PLEASE, DON'T USE THE ABOVE FIND COMMAND, IT WILL DELETE EVERYTHING! For those who are interestsed, it is missing '-name' right before '.DS_Store'



[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: robg on Jan 05, '05 10:39:35PM

I have now edited the original comment, so the above warning is not required...

-rob.



[ Reply to This | # ]
DELETED 4 GIGS OF EXPENSIVE INSTALLERS
Authored by: bradsm87 on Feb 24, '04 01:43:12AM

I was just getting ready to burn ALL my installers to DVD when i ran this to clean it up and when I went there next, it was all GONE. AND, it was ALL downloaded with a 56K modem!!!!!! :',',-(



[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: buserror on Nov 28, '03 11:56:34AM
Not what I would call a "script", thats a oneliner!

find . -name .DS_Store -exec rm -f {} \;
Beside, one this will not break if there are more files than the allowed number of arguments to xargs.

[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: jantonc on Nov 28, '03 12:01:52PM

You can do it in Mac OS X using this

find . -name ".DS_Store" -print0 | xargs -0 rm

the -print0 says use NUL to separate the names for find.
the -0 tells xargs to expect NUL as the separator of the words



[ Reply to This | # ]
A better use of find
Authored by: rebar on Nov 28, '03 12:04:56PM

instead of using printf and xargs which will get upset if your directories have spaces or if you have a LOT of them, why not use -exec instead?

example:

find . -name .DS_Store -ls -exec rm {} \;

the -ls will list them out just before they are removed.

This should work well on any modern UNIX. You can run just about anything like this; suppose you want to batch-encode all your aiff files or whatever, find is very powerful with the -exec argument.



[ Reply to This | # ]
A better use of find
Authored by: tiph on Nov 28, '03 12:59:08PM

The main reason is that -exec will be slower, since a rm process is started for each file, whereas xargs will invoke rm by batch.



[ Reply to This | # ]
A better use of find
Authored by: durin on Nov 28, '03 05:08:09PM

using xargs doesn't break with spaces in the names(look carefully, I enclosed the path in quotes), and it is probably a little faster because only one rm is spawned, instead of one per file.

---

Go not to the elves for council, they will say both no and yes



[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: Anonymous on Nov 28, '03 01:21:21PM

The "-f'" option will supress most warnings from rm.

[ Reply to This | # ]

A script to remove .DS_Store files on any *NIX Machine
Authored by: merlyn on Nov 28, '03 01:34:54PM
print0 is your friend. xargs -0 is your friend. Use them wisely.
find /top/dir -name '.DS_Store' -print0 | xargs -t0 rm


[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: sjk on Nov 28, '03 09:14:10PM

Ahh, I'd never noticed the -t option for xargs... thanks for moving it out of my blind spot.



[ Reply to This | # ]
Or, you could use "Chop"...
Authored by: zenox on Nov 29, '03 03:50:55AM
...hold down simultaneously the Command, Option and Control keys while you drag & drop a folder onto the Chop application icon: it will delete all the invisible ".DS_Store" files enclosed in that particular folder and its subfolders. You can find Chop here: www.zenonez.com/chop

[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: Frogman on Nov 29, '03 04:52:08AM

Is there any harm in just deleteing all the .ds_store files on my machine weekly?



[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: scottgfx on Nov 30, '03 06:19:27AM

After going through the O'Reilly book on Samba, I've set up Jaguar to not serve up the .DS_Store files. So from a Windows machine, I don't see the extra files when I browse to the Mac's share.

What I would like to find now, is a way to make Windows 2000 not allow ANY "dot" files to be written.

The only solution I've found is to either use the CP command or a program I just found on VersionTracker, called KopyMac.



[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: a1291762 on Nov 30, '03 07:57:42PM

using xargs might work... but there are many cases where this will generate too many files for the rm command. There are hard limits on the length of a command string and on a large disk with many directories you'll come across this.

The correct syntax is then:
find . -name .DS_Store -exec rm "{}" ';'

The quotes are needed in case the file has a space in it. The ; can be escaped with a backslash rather than quotes but it's hard to put backslashes into this forum.



[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: xSmurf on Dec 01, '03 05:25:41PM

I would be interested in knowing what do these .DS_Store file do? Anyone?

---
PM G4 DP 800 / 256Mb / 80Gb+40Gb /SuperDrive / SCSI: AGFA SnapScan 1236s / Jaz 1Gb / Zip 100Mb
- The only APP Smurf ;P



[ Reply to This | # ]
.DS_Store files and invisibles
Authored by: 18spheres on Mar 27, '04 01:13:18PM

Can somebody just tell me why all of a sudden I'm getting .DS_Store appear on my desktop and other places? I delete them all the time with DS_Store Cleaner and have also used script advised in this forum but it re-appears again and again.

I also noticed files/folders with my harddrive with names like .Trashes, .volume, .hidden, bin, dev. etc, var, usr, mach, tmp which I've not seen before? When I try and send to trash a message says that I either don't have sufficient privileges or they cannot be trashed because they are invisible.



[ Reply to This | # ]
A solution to invisible files showing up all over
Authored by: DanPritchard on Mar 31, '04 02:56:43AM
You seem to have triggered the viewing of invisible files. Best to just re-hide invisibles instead of deleting. Do this in the Terminal (Tested myself):

defaults write com.apple.finder AppleShowAllFiles OFF

Then relaunch the Finder, or log out and back in.
If later on you need to view invisibles again, just repeat the process with "ON" instead of "OFF".

[ Reply to This | # ]

A script to remove .DS_Store files on any *NIX Machine
Authored by: montalvd on May 15, '04 09:27:39PM

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

---
Don, NYC



[ Reply to This | # ]
A script to remove .DS_Store files on any *NIX Machine
Authored by: srparker on Apr 16, '08 01:44:20PM
Now in leopard, you can just call the "dot_clean" command. See

http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/dot_clean.1.html

SRP

---
--
Steve

[ Reply to This | # ]