Does anyone have a Mac Terminal script to remove hidden files?

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?


Solution 1:

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.

Solution 2:

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

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

Solution 3:

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!

Solution 4:

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

Delete ._*.mp3 in an Automator workflow.