Last Updated: December 28, 2020
·
14.7K
· amoniker

Clean up OSX .dotfiles

When using Finder, OSX generates some hidden files. These files are usually fine in the context of OSX, but may not be appropriate on remote servers, mounted drives, or in git repos.

They include .DS_Store and ._* files, as well as the .Trashes directory.

You could spend money on software like BlueHarvest to clean these files up automatically, or you could create a single alias that does it for free:

alias sweep="find . -name .DS_Store -type f -delete ; find . -type d | xargs dot_clean -m"

The first part (find . -name .DS_Store -type f -delete) will simply find all .DS_Store files (recursively) and delete them.

The second part (find . -type d | xargs dot_clean -m) uses an OSX builtin command called dot_clean which will clean up ._* files (which are called AppleDouble files and contain resource forks). dot_clean does not work recursively, so first we list all subdirectories underneath the current directory with find . -type d, then pipe the list to xargs which runs dot_clean -m on each item in the list.

If you'd also like to remove .Trashes directories, you could add one more command:

alias sweep="find . -name .DS_Store -type f -delete ; find -name .Trashes -type d -delete ; find . -type d | xargs dot_clean -m"

With .Trashes included, you might need to call the alias using sudo

Now, whenever you need to clean things up, just sweep!

Enjoy!

3 Responses
Add your response

As a workaround for various scenarios (file list too long, whitespace in filename, relative path used) I found the following worked better for removing the ._* files:

find "$(pwd -P)" -type d -exec dot_clean -m "{}" \;
over 1 year ago ·

The second alias throws an error for both find and xargs. I noticed that it's missing a dot after find. However, I'm not sure how to fix the xargs. It says unterminated quote.

over 1 year ago ·

@jl303
find . -type d -print0 | xargs -0 dot_clean

over 1 year ago ·