find Command

find Command

In an attempt to remove .DS_Store files that Finder creates on network shares, I learned about the -exec option in the find command. Sometimes find can feel overwhelming, especially when you've been spoiled by Spotlight or Google, but basically it's formatted this way:

find [options] [path] [expression]

Back to our story about the dot underbar files on network shares. Once upon a time, you had to run defaults write com.apple.desktopservices DSDontWriteNetworkStores true (from https://support.apple.com/en-us/HT1629) for each user to prevent AppleDouble/dot underbar files from clobbering your network shares (in some companies, uniformed looking shares are important). Sometimes you had users that didn't get that defaults written to their user profile and you had to clear out either .DS_Store and AppleDouble files.

Now, in good practice, you should never have to delete the dot underbar and .DS_Store files!

Here are some commands I've used and please be very careful when you use them. You've been warned, and I'm not responsible for your accident disclaimer goes here.

First let's start with a simple search:

Find icons:

find /Library/Printers -name *.icns

In the above example, we're looking for icons to use with our Munki 2 installation, specifically icons that are related to printers (self-service printer installation, perhaps). It says to:
Find in the directory /Library/Printers recursively for files with the name *.icns, standard wildcards apply.

You should never have to delete the dot underbar and .DS_Store files!

.DS_Store cleaning:

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

In the above example, it says:
Find from the Current Directory, recurisvely (it is recursive by default) that has a file named exactly ".DS_Store" and perform the command rm from the results of the find, and perform it for each result.

AppleDouble cleaning:

find . -type d -exec dot_clean -f "{}" \;

In the above example, it says:
Find from the Current Directory, recursively, that are Directories and perform the command dot_clean -f from the results of the find. The "{}" carries the results and ; breaks the line so it performs the command in every line of the result as we see in the last example. (Added -f to dot_clean so it doesn't recursively search as well, this is just to show our new find prowlness, I'm so punny!) There are more options for dot_clean, so make use of man dot_clean!

There are way more powerful things you can do with find that I haven't even scratched yet. You can always man find for more file types to search for and do different searches with -iname or -xattr.