I ran into the following problem:
user@machine:/$ echo ./dir/fil | xargs -I {} bash -c "echo $(basename {})"
./dir/fil
Why is it not just printing fil?
So basename seems to get the expected parameter ./dir/fil but somehow assumes it is all the filename. Conversly when used with dirname it only prints out .. It feels as if the /s are somehow escaped.
Why do I care?
I actually need to run something like this:
find -name "*.foo" | xargs -I {} bash -c "cd $(dirname \"{}\"); thirdpartytool $(basename \"{}\") 2>&1 > /dev/null | sort"
- I need a non-zero return code if any call of
thirdpartytoolreturns one, sofind ... -exec ...does not work for me. - I need output redirection (discard stdout and sort stderr) so I need to call another shell.
- I need to
cdbecausethirdpartytoolmust be called from the directory where the file is, so I needdirnameandbasenamein a subshell.