dirname appears not to work with xargs

dirname on macOS only takes a single pathname, whereas basename is able to work with multiple pathnames. It is however safest to call basename with a single pathname so that it does not accidentally try to remove the the second pathname from the end of the first, as in

$ basename some/file e
fil

When calling these utilities from xargs you may ask xargs to run the utility with a single newline-delimited string at a time:

printf '%s\n' some arguments | xargs -I {} basename {}

or,

printf '%s\n' some arguments | xargs -I {} dirname {}

You could also use xargs -L 1 utility rather than xargs -I {} utility {}.


dirname only takes 1 argument; while the GNU/coreutils version of dirname can take more than 1 argument, that is a non-standard extension:

SYNOPSIS

    dirname string

Tags:

Xargs

Dirname

Osx