First, the -execdir command looks wonky (2x $SHELL -c?):
$SHELL -c '[ ! -d ".git" ] && echo "not git repo:" {}' $SHELL -c '{}' ';'
Also, -execdir runs the command in the directory containing the matched entity, (so the parent directory of the directory that it is checking right now), which is . for all subdirectories. The test is run in the wrong directory. And:
- You shouldn't reuse
{} in exec/-execdir commands.
- There's no reason to use
$SHELL. That's the user's login shell, and it doesn't have any special significance for usage in scripts and such. Just use sh, or bash or ksh if those are your shells of choice, directly.
This might work:
find . -maxdepth 1 -type d -exec sh -c '! [ -d "$1/.git" ] && echo "not git repo: $1"' _ {} \;
The -exec command checks for the existence of .git in the argument passed in, which is each subdirectory in turn. You might also want to use -mindepth 1 to exclude the current directory:
find . -maxdepth 1 -mindepth 1 -type d -exec sh -c '! [ -d "$1/.git" ] && echo "not git repo: $1"' _ {} \;
Or, just using bash, enabling dotglob to match hidden directories:
(shopt -s dotglob; for d in ./*/; do [[ -d $d/.git ]] || echo "not git repo: $d"; done)
$SHELLbit (somewhere...) as a solution to "you can't dotestusingexec" but as these answers prove, that appears to have been incorrect. Can you explain why I shouldn't reuse{}inexec/execdir? – Victoria Drake Jun 18 '19 at 11:52find: https://stackoverflow.com/a/12965441/2072269 – muru Jun 18 '19 at 11:56