How to remove sidecar files that MacOS stores on external media like USB Sticks, ….

With this shell script one can list or remove all files of a directory (or volume) that match one of the criteria of the following list:

  • .Spotlight-V100
  • .Trashes
  • .fseventsd
  • .TemporaryItems
  • .DocumentRevisions-V100
  • .DS_Store
  • ._*
#!/bin/bash
#===============================================================================
# File:         macosfiles.sh
# description:  shows or removes sidecar files that MacOS stores on external media
#-------------------------------------------------------------------------------
# settings:     tab width = 4 spaces
#-------------------------------------------------------------------------------
# Copyright:    Copyright Balanced Solutions, 2010-19
#===============================================================================

#====== Settings ===============================================================

SCRIPT_DIR=`dirname ${0}`
SCRIPT_NAME=`basename ${0}`

#====== Functions ==============================================================

function printUsage() {
    echo "usage: sudo ${SCRIPT_NAME} (\"help\" |\"list\" | \"remove\") <directory> "
}

function printError() {
    echo $1
    printUsage
}

#====== Main ===================================================================

command=$1
clean_up_dir=$2

case $command in 
"help")
    printUsage
    exit 0
    ;;
"list")
    operation="-ls"
    ;;
"remove")
    operation="-exec rm -rf {} +"
    ;;

*)
    printError "Unsupported command: ${operation}"
    exit -1
esac


declare -a macOSEntries=(   ".Spotlight-V100" \
                            ".Trashes" \
                            ".fseventsd" \
                            ".TemporaryItems" \
                            ".DocumentRevisions-V100" \
                            ".DS_Store" \
                            "._*" \
                        )

for macOSEntry in "${macOSEntries[@]}"
do
    find ${clean_up_dir} -name ${macOSEntry} ${operation}
done
© 2018 Balanced Solutions Contact us