I wrote a bash script to run on macOS 10.l0.5. I hoped to simplify the script. In the example script below, the first invocation of the find command gives the desired output. In the original script, I have a multiple find commands with the same set of directories excluded. I hoped to place them in a variable. The second find command show my failed attempt.
I do not know why this failed.
Any way of sharing common options between multiple find commands is acceptable to me. No, I do not want to exclude all hidden directories.
I run this with an external flash drive. This script is all read only.
#! /bin/bash
sourceDir="/Volumes/DOSDISK"
echo "--------------- find works as expected ------------------------------"
find ${sourceDir} \
! -path "${sourceDir}/.Trashes" \
! -path "${sourceDir}/.Trashes/*" \
! -path "${sourceDir}/.Spotlight-V100" \
! -path "${sourceDir}/.Spotlight-V100/*" \
! -path "${sourceDir}/.fseventsd" \
! -path "${sourceDir}/.fseventsd/*" \
-type d \
-exec echo {} \;
echo "----------------------------- does not skip hidden directories -----------"
dirsToSkip=" ! -path \"${sourceDir}\" "
echo "dirsToSkip is -1->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.Trashes\" "
echo "dirsToSkip is -2->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.Trashes/*\" "
echo "dirsToSkip is -3->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.Spotlight-V100\" "
echo "dirsToSkip is -4->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.Spotlight-V100/*\" "
echo "dirsToSkip is -5->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.fseventsd\" "
echo "dirsToSkip is -6->${dirsToSkip}"
dirsToSkip="${dirsToSkip}! -path \"${sourceDir}/.fseventsdf/*\" "
echo "dirsToSkip is -7->${dirsToSkip}"
echo "see in print ---> "find ${sourceDir} \
${dirsToSkip} \
-type d \
-exec echo {} \;
echo -e "\n The non-working thing. "
find ${sourceDir} \
${dirsToSkip} \
-type d \
-exec echo {} \;
the output. When adding debug to the script #! /bin/bash -v -x, I notice the ! printed as '!'.
mac $ ./config/trybash.bash
--------------- find works as expected ------------------------------
/Volumes/DOSDISK
/Volumes/DOSDISK/level2
/Volumes/DOSDISK/level3
----------------------------- does not skip hidden directories -----------
dirsToSkip is -1-> ! -path "/Volumes/DOSDISK"
dirsToSkip is -2-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes"
dirsToSkip is -3-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*"
dirsToSkip is -4-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*" ! -path "/Volumes/DOSDISK/.Spotlight-V100"
dirsToSkip is -5-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*" ! -path "/Volumes/DOSDISK/.Spotlight-V100" ! -path "/Volumes/DOSDISK/.Spotlight-V100/*"
dirsToSkip is -6-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*" ! -path "/Volumes/DOSDISK/.Spotlight-V100" ! -path "/Volumes/DOSDISK/.Spotlight-V100/*" ! -path "/Volumes/DOSDISK/.fseventsd"
dirsToSkip is -7-> ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*" ! -path "/Volumes/DOSDISK/.Spotlight-V100" ! -path "/Volumes/DOSDISK/.Spotlight-V100/*" ! -path "/Volumes/DOSDISK/.fseventsd" ! -path "/Volumes/DOSDISK/.fseventsdf/*"
see in print ---> find /Volumes/DOSDISK ! -path "/Volumes/DOSDISK" ! -path "/Volumes/DOSDISK/.Trashes" ! -path "/Volumes/DOSDISK/.Trashes/*" ! -path "/Volumes/DOSDISK/.Spotlight-V100" ! -path "/Volumes/DOSDISK/.Spotlight-V100/*" ! -path "/Volumes/DOSDISK/.fseventsd" ! -path "/Volumes/DOSDISK/.fseventsdf/*" -type d -exec echo {} ;
The non-working thing.
/Volumes/DOSDISK
/Volumes/DOSDISK/.Trashes
/Volumes/DOSDISK/.Trashes/501
/Volumes/DOSDISK/.Trashes/501/.Spotlight-V100
/Volumes/DOSDISK/.Trashes/501/.Spotlight-V100/Store-V2
/Volumes/DOSDISK/.Trashes/501/.Spotlight-V100/Store-V2/FF996064-BEDD-474E-9E76-7F8ABD688B09
/Volumes/DOSDISK/.Spotlight-V100
/Volumes/DOSDISK/.Spotlight-V100/Store-V2
/Volumes/DOSDISK/.Spotlight-V100/Store-V1
/Volumes/DOSDISK/.fseventsd
/Volumes/DOSDISK/level2
/Volumes/DOSDISK/level3
mac $