When plugging a USB stick into a Mac, OS X creates a number of hidden files on the stick, including a Spotlight index and Trash folder.

Example from the terminal for a USB stick "Untitled":

$ ls -a /Volumes/Untitled
.Spotlight-V100
.Trashes
._.Trashes
.disk
.fseventsd

It even does this on the xD memory card for my camera, so after having copied my pictures and deleted them from the card, the card is still full.

Is it possible to turn this off for USB and memory cards, so OS X either writes these files to the primary disk or doesn't write them at all?

  • Comments are not for extended discussion; this conversation has been moved to chat. – bmike Aug 10 at 0:50

20 Answers 20

up vote 19 down vote accepted

UPDATED March 2018 It seems my solution is not valid anymore, the solution that works right now is proposed by @ElmerCat in one of the answers below.

He is suggesting to use CleanMyDrive 2

Deprecated Solution

As I know you have 2 choices :

  1. TinkerTool (free)

alt text

  1. BlueHarvest (commercial)

alt text

  • 1
    OK, thanks. Was kind of hoping that there would be a built-in setting somewhere, but at least there's a solution available. – j-g-faustus Jan 20 '11 at 18:08
  • 2
    @Philip: Unfortunately Tinkertool only deals with .DS_Store files according to their detail page, which presumably means that this is the only hidden file that can be disabled through the CLI - the other files need custom tools. But BlueHarvest looks nice, and does what I need. (Found some more details on the issue here and here) – j-g-faustus Jan 20 '11 at 18:47
  • 1
    outdate! later answers are better – Dima Tisnek Aug 19 '13 at 9:46
  • 1
    @j-g-faustus Not only Tinkertool only deals with .DS_store files but it does so just for network fileysystems – nhed Apr 14 '14 at 16:23
  • 1
    Tested it. Tinkertool doesn't work on USB drives (on 10.9 at least) – pufferfish Jun 15 '14 at 19:21

For just a particular mounted volume - like a flash drive called yourUSBstick in this example - these commands will remove existing cruft, stop Spotlight indexing now and in the future, stop the related fsevents logging, and disable the Trash feature.

mdutil -i off /Volumes/yourUSBstick
cd /Volumes/yourUSBstick
rm -rf .{,_.}{fseventsd,Spotlight-V*,Trashes}
mkdir .fseventsd
touch .fseventsd/no_log .metadata_never_index .Trashes
cd -

Other unfamiliar stuff you may still see you probably want to keep, like Apple double "._*" files and other Apple DS cruft relating to icons and window placement.

  • 7
    While this may disable indexing, those files and directories will still be present on the volume (which is the annoying part in the first place), and if you delete them .Spotlight* and .fseventd will come back. In fact, .metadata_never_index is one more entry in the file listing than the usual cruft. – ShreevatsaR May 21 '11 at 8:02
  • 1
    +1, Actually, I came up with he idea of touching trashes myself and went here for a better solution, but having seen this one couldn't resist +1 it. It solves the real problem — stopping car audio from playing trashed files ;) – Michael Krelin - hacker Feb 1 '12 at 19:33
  • +1 knew this existed but always have to find it when I need it. This is useful if you have more access to the USB drive than to the OSX system. – mlhDev May 30 '14 at 13:00
  • 2
    Thanks, I didn't know about "cd -". I've still been pushing pushd/popd :). – studgeek Dec 6 '14 at 16:16
  • What about directory ".DS_Store" ? That's still there. Any reason why that was not included in the "rm -rf" line? – Marnix A. van Ammers May 25 '16 at 4:22

To keep Spotlight from indexing non system volumes, add /Volumes to the Privacy list in System Preferences > Spotlight.

/Volumes is the point in the file system where all non-system disks are mounted by default.

enter image description here

  • impossible to add /Volumes in 10.8; it's possible to add individual volumes one at a time though. I suspect it's similar to mdutil -i off /Volumes/xxx – Dima Tisnek Aug 19 '13 at 9:47
  • 14
    @qarma It is very possible to add /Volumes in 10.8 or later. Simply open a Finder window, press Shift+Command+G to bring up the "Go to folder..." window, type /Volumes, and then drag the little folder icon at the top of the Finder window (next to the word "Volumes") into the list in the screenshot above – Chris Mukherjee Nov 20 '14 at 20:01
  • I'll have to try that out... – Dima Tisnek Nov 21 '14 at 9:02
  • 1
    You can add /Volumes, it just doesn't work. – kenny Sep 10 '16 at 17:36
  • didn't work for me either :( – Zilvinas Feb 11 '17 at 21:06
  1. Insert the USB drive.
  2. Navigate to Macintosh HD > Applications > Utilities and open Terminal.
  3. At the Terminal prompt, type the following command, replacing path_to_volume with the real path:

    sudo mdutil -i off /path_to_volume

  4. Press return.

  5. If prompted for a password, type your admin password, then press return. You will receive the response:

    /path_to_volume/: Indexing disabled for volume. in Mac OS X 10.4 or

    /path_to_volume: Indexing disabled. under Mac OS X 10.5 or later.

    Spotlight will immediately cease to index the specified volume.

  6. If you are using Mac OS X 10.5 or later, skip to step 9.

  7. At the Terminal prompt, type the following command, again substituting the correct path:

    sudo mdutil -E /path_to_volume and press return

  8. If prompted for a password, type your admin password, then press return. You will receive the response:

    /path_to_volume/: Volume index removed.

  9. At the Terminal prompt, type exit then press return.
  10. Quit Terminal.

Thanks to thexlab.com, their troubleshooting Mac OS X e-books, and their website for the detailed explanation of why other methods sort of work.

  • 1
    Note sudo asks for the current user's password – Mark Feb 16 '13 at 8:21
  • This worked for me. Disk usage by Spotlight went from 2GB to 200k. The fact that spotlight takes up so much disk usage on an empty disk is insane. I wonder if we can pull together this solution and some kind of USB-based trigger script, which detects which kind of USB device is in there and runs this command automagically. Any ideas on that? – Otheus Sep 30 '16 at 0:37

Another way to deal with (just the) spotlight files, is to add that volume to your Spotlight exclude list. Plug the device in, and go to the Spotlight prefpane in System Preferences. Select the Privacy tab. Now drag that volume from your desktop up into the privacy list.. or use the + button at the bottom to add it. No more spotlight indexing will happen on that volume.

  • Unfortunately, volumes seem to get removed from the privacy list whenever they're unmounted. So this would have to be repeated every time you plug your memory card or USB stick back in. – LarsH Sep 11 at 17:45

2017 December

You'd think after all these years, Apple would build something into the Finder to deal with this. It's still a very common problem for people using USB disks or SD cards to play media in their cars or other devices.

Nonetheless, developers have filled the void with numerous Apple-approved apps. The apps listed in previous answers might have been good at the time, but they haven't been maintained to work with modern versions of macOS.

I'd also be a bit wary of installing something that will wield total control of the filesystem, written by unidentified developers. Not wanting to deal with the App Store is one thing, but not wanting to register with Apple as a developer is another. Moreover, something from the AppStore has undergone at least minimal auditing by Apple and can be removed from your computer as easily as it is installed.

So, whenever you happen to read this "answer", its advice to you is to search the App Store for something up-to-date, well reviewed, and free.

TL;DR:

My choice on December 20th, 2017 is "CleanMyDrive 2" from the App Store. Solves the problem, lovely interface, completely free. (Offers in-app purchases of customized icons. Otherwise, everything works for free.)

Tomorrow, something better may come along, but the bottom line is: the App Store has free, easy solutions to j-g-faustus' original but enduring question, posed here so many years ago — a question I had myself today.

So I apologize if this seems more like a rant than an answer, but indeed, all of the previous answers were out-of-date and didn't lead to a useful solution. Not saying something would lead more people to waste time fiddling with the Terminal or installing questionable apps. Just go to the App Store — you won't need to pay.

I use the MacOS Terminal command line to list and delete all these files and folders before ejecting the device from the desktop. For some files, you may have to sudo the /bin/rm command.

  • That's what I do too. Although it would be even better if I didn't need to, so I'm investigating the alternatives. – j-g-faustus Jan 20 '11 at 20:26
  • By the way, the device is found somewhere at /Volumes/<name> – DerMike Jan 21 '11 at 11:31

I use Clean Eject (free) and a custom Automator Service (still private) so I can assign a hotkey to clean & eject a volume using the app.

  • Without access to the Automator Service you mentioned this answer isn't actually very helpful. Can you share the service as well? – nohillside Jan 9 '14 at 10:54
  • The answer is absolutely helpful - you can use the app without the Automator action. If you want to add a special shortcut key for Clean Eject then you have lots of options: Alfred, Keyboard Maestro. The Automator action is not essential, but is useful. I will upload it when I can to: gingerbeardman.com/services – Matt Sephton Jan 10 '14 at 12:35
  • The service I use to Clean Eject selected finder volumes is now uploaded to by site (see above for link). – Matt Sephton Jan 10 '14 at 12:56
  • Thanks for the upload, makes the answer much more complete. – nohillside Jan 10 '14 at 13:25

@ Miles Leacy's post

and @ qarma's comment:

No, this is still possible even in OSX 10.9, but you need to do a few extra steps now:

1) In Finder click Go then click Go To Folder...

2) Type /Volumes and click Go.

3) A Finder window will open, and it should say Volumes at the top.

This is the most important step:

4) Next to where it says Volumes at the top of the Finder window, there is a tiny blue folder icon. Click and drag this icon left into your Favorites panel.

5) Now you will have access to your Volumes folder anywhere, including in Spotlight settings like Miles Leacy suggested. (Whenever you need it, just click on the Favorites link to select it.)

Hope this helps,

Best,

Vlad :)

~ ~ ~

How to add the Volumes folder to your Favorites so that you can access it in the Spotlight settings

~ ~ ~

What it looks like after adding the Volumes folder to the Spotlight exceptions list:

Notice in the background you can see my post in Safari. ;)

What it looks like after adding the Volumes folder to the Spotlight exceptions list

  • 1
    FWIW, I think this is a fine answer and does not need to be a comment. I'd remove that part from it and leave it as is. – Mark Edington Jan 1 '17 at 20:09
  • Since the Mac HD is a subfolder of Volumes, doesn't this disable Spotlight on the Mac HD too? – LarsH Sep 11 at 17:46

Actually touching the .Trashes file will be the best way to solve your main problem since .Trashes is now a file instead of a folder. This means that Apple can't relocate the files to the .Trashes folder when you delete them and your drive is no longer full.

Another option is to hit Cmd-Opt-Shift-Backspace to force Finder to empty the .Trashes content on the card before you eject it.

The first method is really the best as the second affects all Trash contents on all drives.

However, it seems from your post that you are more worried about the pollution of the drive by the various dot files. If you follow the steps mentioned above, you'll save your disk space, but there will be a minimum of dot files created.

An easy way to stop my car audio trying to read hidden Mac OS files is to remove them in Windows OS. Simply copy your MP3 music to the USB stick from iTunes. Swap the stick into Windows OS and select view hidden files from folder options. This will then allow you to delete every single hidden file that your trusty Mac placed on your USB stick including those pesky .trashes files. Finally a use for Windows OS!

  • +1 It is a bit tedious, but sure, why not, it does get the job done haha. – Vladimir Dec 10 '14 at 6:08

I ended up using a free app "Hidden Cleaner". My car's MP3 player was trying to read .(MP3filename).mp3 (hollow, empty mp3 files) as well. Go to Macintosh HD in Devices section on the Finder left hand menu and drag your USB drive and drop onto the Hidden Cleaner app. It will cleanup the hollow files and leave the real MP3s and will eject your USB.

Note: That is not a permanent solution. You need to do above everytime you copy files. I don't mind though.

Old question, but I, finally, discovered Asepsis. This is an open source utility that solves this age-old problem by confining all the .DS_STORE directories in one place, by default /usr/local/.dscage
After installation, and a reboot, no more .DS_STORE on USB drives, with the advantage (for some of us) of not having to disable indexing on external drives. Since version 1.4 it also supports OS X Mavericks.

Update from Aepsis website: "Warning: Asepsis is no longer under active development and supported under OS X 10.11 (El Capitan) and later.

  • Cool! I wonder how this program will handle a system crash or extreme CPU load. In other words, I wonder if a disycronization will bet automatically handled, or will need to do that your selves? Automaticlly is good because we don't need to worry about it, but it also means that the program will have to do a lot more checking. – Vladimir Nov 22 '14 at 7:41

Most of the solutions here are 'clean up dotfiles before eject', rather than 'prevent dotfile creation'.

In my search for a free solution for the former, I've tried a few options, and settled on the applescript here: SuperUser: Does anyone have a Mac Terminal script to remove hidden files? because it allows me to see exactly what's happening along the way.

Note in my comment on that answer that I made a small edit to get it working on OSX 10.12.1. (I'm not reposting the source in this case, as this seems a pretty 'link friendly' question).

MacOS now provides this direct flag that you can toggle from Terminal:

defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • Good answer for part of the question. This disables the DSStore, but the question was specifically concerning Trash and Spotlight files, which your solution (from what I can read) does notthing about. – Otheus May 7 at 7:41

Dumbest thing ever that I can't simply contribute this to Metaxis' answer. But it's easy to create a bash script that automatically handles this for all folders in /Volumes (or a specific one if you specify). Should be able to invoke it with Automator or Applescript when a folder appears under /Volumes, then you have automatic disabling of indexing.

#!/bin/bash

if [ -n "$1" ]; then
    if [ ! -e "${1}/.metadata_never_index" ]; then
        echo "mdutil -i off $1"
        mdutil -i off "$1"
        cd "$1"
        rm -rf .{,_.}{fseventsd,Spotlight-V*,Trashes}
        mkdir .fseventsd
        touch .fseventsd/no_log .metadata_never_index .Trashes
    fi
else
#    echo "finding Volumes"
    find /Volumes -type d -maxdepth 1 -mindepth 1 -print0 | xargs -0 -n 1 "$0"
fi

I found this App in the Apple App Store for free and use it, and it has worked great for me. You have to search "cleanusbdrive" all one word without the quotes in the App Store.

It's by José A. Jiménez Campos.

Another OSX utility for removing these extra files is DOTCLEANER which can be found on the OSX App Store.

https://itunes.apple.com/us/app/dotcleaner-ds-store-remover/id1113480556?mt=12

Currently, Cleanmydrive2 is available on the app store for free and handles this problem for user up to high sierra.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

I got this to work on Sierra 10.12.3 in Automator.

First, I made this version of the script: It functions the same way as freefly42's, just a different way of writing the same thing.

To translate for non-native bash speakers, what it does is:

If run with a commandline argument, it tries to see if the argument is a directory, and if that directory contains a file named ".disable_osx_metadata".

If the special file is not there, then nothing happens. That drive is ignored.

If the special file is there, the script deletes all the osx metadata and creates a few small new items which prevents anything else. For instance, creating a file named .Trashes prevents the OS from creating a directory named .Trashes and then writing files in there.

If run with no arguments, it runs itself once for each directory in /Volumes.

So, if you run it manually or click on it, it checks all attached drives. If you create a workflow in Automator, then Automator runs it whenever a drive is attached, just for that drive.

#!/bin/bash
x=.disable_osx_metadata
[[ "$1" ]] || exec find /Volumes -type d -maxdepth 1 -mindepth 1 -exec $0 {} \;
[[ -e "$1/$x" ]] || exit 0
mdutil -i off "$1"
rm -rf "$1"/.{,_.}{fseventsd,Spotlight-V*,Trashes}
mkdir "$1/.fseventsd"
touch "$1/.fseventsd/no_log" "$1/.Trashes" "$1/$x"

Save as "disable_osx_metadata", chmod 755, copy to /usr/local/bin .

$ cd Documents/disable_osx_metadata/
$ chmod 755 disable_osx_metadata
$ sudo cp disable_osx_metadata /usr/local/bin

Then open Automator, New, Folder Action

Then I found that even in Sierra 10.12.3 you CAN still add the /Volumes directory in Automator, by a non-obvious way.

Go to Finder, File, "Go To Folder...", manually write in "/Volumes" and hit Enter.

Now this shows you a window with your HD and usb drives, but no obvious "/Volumes" folder to click on. But the title bar says "/Volumes", and it appears at the bottom too.

You can drag the folder icon next to "/Volumes", either from the title bar or from the bottom, over to the Automator, and drop it on "Folder action receives files and folders added to:[______]"

Drag /Volumes from Finder Title Bar

Close Finder.

Back in Automator: on the left, scroll down and drag "Get Folder Contents" to the right and drop it.

Then on the left again, scroll down and drag "Run Shell Script" to the right and drop it below "Get Folder Contents".

Change "Pass input:" to "as arguments". Shell:[ /bin/bash ] Pass input:[ as arguments ]

Then drag the script from /usr/local/bin onto the box under "Run Shell Script" so it says "/usr/local/bin/disable_osx_metadata"

File, save, disable_osx_metadata.workflow

Automator Folder Action Workflow

For reference, this gets saved in: /Users/YOUR_NAME/Library/Workflows/Applications/Folder Actions If you save something wrong, I think you have to manually navigate there in Finder to delete it.

Finally, for each new usb drive you want to protect, you have to create a file named ".disable_osx_metadata" in the root folder.

You can do it in Terminal: $ touch /Volumes/NO\ NAME/.disable_osx_metadata

Or just keep a small text file around with a visible name (no leading dot) and copy it to the root of any new usb drive and rename it .disable_osx_metadata after copying.

That drive now gets cleaned each time you attach it from now on.

Not as clean as I'd like. You have to pollute the drive a little, in order to tell the OS not to pollute it more. There seems to be no way make the OS simply leave it alone and not add any files that you didn't ask for.

TODO: Add enable/disable functions to create/remove the dot-file.

TODO: This does not prevent the .DS_Store files in every directory.

TODO: Is it possible to package up the script and the foo.workflow file so a user can skip most of these manual directions? I see there is an "export" option in Automator that creates some sort of package file.

TODO: Possibly obsolete this whole post. This applescript + automator workflow might be better: https://superuser.com/questions/319553/does-anyone-have-a-mac-terminal-script-to-remove-hidden-files/814104#814104

protected by nohillside Jun 10 at 8:54

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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