11

My car stereo can play music from USB flash drives, so I asked about copying MP3s from an iTunes playlist. That worked fine, but my stereo wouldn't play the MP3 files. I eventually put the flash drive into my work computer running Windows 7, and I found the issue: a bunch of hidden files in the format ._[filename].mp3 were also loaded.

I know that in Windows, you can delete files based on attributes, and I was wondering if you can do the same in Terminal? Possibly with a script that I can run with the Volume as a parameter?

3
  • I had the exact same problem. If the ._ files where there, my stereo would say "Bad Media", which was totally wrong. It would play the properly named files, but complained about each ._ file. It took me a while to verify the media wasn't bad, the car stereo was!
    – Eric
    Aug 5, 2011 at 17:23
  • But it's not the stereo's fault it sucks... Lol. I just put it in my Win7 computer to put music on at work and saw all of them: A bunch of hidden files that were 4KB each Aug 5, 2011 at 17:31
  • The Car stereo should ignore those files. In your case they don't contain any necessary information. But it's the same problem everywhere. My DLNA media player shows these as well while browsing.
    – slhck
    Aug 5, 2011 at 17:35

5 Answers 5

25

These files are hidden in OS X because they start with . and are therefore not shown in Finder by default. There is no special attribute set in these files apart from that.

A very simplistic approach would be to delete all files that start with a dot and an underscore:

find /Volumes/<your-volume-name> -name '._*' -type f -delete

Safety note: Run that without -delete to see which files would be removed. And don't ever do this on your whole Mac HD, only on your USB drive.


For some additional information: These files are called "Resource Forks" and sometimes contain information you don't want to delete. In your use case, that should be fine though. You can permanently disable the creation of these files using BlueHarvest. There's also an app that claims to clean volumes of them, but I haven't tried it and it's beta, so use with caution: Hidden Cleaner.

1
  • Did not realize that... Good to know. I'll try that tonight when I get home, then mark your answer. Thanks Aug 5, 2011 at 17:01
9

One more solution that worked for me (MacOS 10.6.8) in terminal type:

dot_clean "/Volumes/<your-volume-name>"
4

I had exactly the same problem with my car stereo, 1 file /2 is unplayable, because the player considers that the dot file created by MAC OS is a music file... Then I found this post really useful, thanks.

So I come back now with an improved and free solution for Mavericks:

  1. Create an Automator Application to run an AppleScript script
  2. Use this script:

    on run {input, parameters}
        try
            tell application "Finder" to set allDrives to the name of every disk whose local volume is true and startup is false
            set driveName to {choose from list allDrives with title "DiskCleaner" with prompt "Disk to Clean and Eject:" OK button name "Clean and Eject" cancel button name "Abort"} as text
            set getDriveIdentifier to "diskutil info " & quoted form of driveName & " | grep Ident | awk '{print $3}'"
            set driveIdentifier to do shell script getDriveIdentifier
            try
                do shell script "find /Volumes/" & quoted form of driveName & " -name '.DS_Store' -type f -delete"
                do shell script "find /Volumes/" & quoted form of driveName & " -name '.FBC*' -type f -delete"
                do shell script "find /Volumes/" & quoted form of driveName & " -name '._*' -type f -delete"
                do shell script "rm -rf /Volumes/" & quoted form of driveName & "/.{,_.}{fseventsd,Spotlight-V*,Trashes}"
                display notification "Disk Cleaned"
            on error
                try
                    do shell script "sudo find /Volumes/" & quoted form of driveName & " -name '.DS_Store' -type f -delete" with administrator privileges
                    do shell script "sudo find /Volumes/" & quoted form of driveName & " -name '.FBC*' -type f -delete" with administrator privileges
                    do shell script "sudo find /Volumes/" & quoted form of driveName & " -name '._*' -type f -delete" with administrator privileges
                    do shell script "sudo rm -rf /Volumes/" & quoted form of driveName & "/.{,_.}{fseventsd,Spotlight-V*,Trashes}" with administrator privileges
                    display notification "Disk Cleaned"
                on error
                    display notification "Can't Clean, Permission Denied"
                end try
            end try
            delay 1
            try
                do shell script "diskutil eject " & driveIdentifier
                display notification "Disk Ejected"
            on error
                display notification "Can't Eject, Disk in Use"
            end try
        on error
            display notification "No Disk to Clean and Eject"
        end try
    end run
    
  3. Save the application as DiskCleaner.app for example

This script will ask you to choose the USB disk to clean and eject. Then it will clean the USB disk (tries with admin credentials if failed with current ones), then eject if possible.

Of course there is still some place for improvement, I did this script for myself!

Also you have the flexibility to add code lines for cleaning more files.

Enjoy!

3
  • This is really useful, would you consider posting the full Automator workflow?
    – Konrad
    Dec 17, 2015 at 12:35
  • Awesome, many thanks! Works for me on OSX10.12.1 if I remove {input, parameters} from line 1 (leaving only on run). @Konrad: I opened up 'Script Editor', made the aforementioned edit, and saved to /Applications as an app; job done. I love being able to see exactly what is happening, which is trickier with other free solutions.
    – ptim
    Dec 10, 2016 at 6:15
  • Thanks. I tried your script. When I ran it from the Automator app to test it I clicked the abort option at the disk selection dialog and still got a sudo prompt and a message about failing to eject the disk. I think that needs to be fixed. I'd suggest you go a little further in your answer with explaining how to test and configure the script to be usable from a services menu in Finder. Jan 1, 2017 at 19:45
2

You can also use Automator to accomplish the same thing if the terminal scares you:

Delete ._*.mp3 in an Automator workflow.

4
  • Terminal doesn't scare me unless I do rm -rf / and press Enter :P But I would like to learn Automator, looks cool Aug 5, 2011 at 17:06
  • Ah, yeah, that's a good idea as well.
    – slhck
    Aug 5, 2011 at 17:32
  • Is Get Selected Finder Items useful here?
    – Daniel Beck
    Aug 5, 2011 at 18:17
  • Not really. I was on someone else's Mac and had to work hastily.
    – digitxp
    Aug 5, 2011 at 18:30
-2

This script is used to delete hidden files or directories in USB drives. https://github.com/ulasyurtsever/delete-hidden-files-or-directories

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.