Agree with @an9wer, as a pipeline is a lot easier (to type, to remember, to read, etc) though I'd try to adhere to posix xargs and use nil character as separator; xargs on alpine, for example, doesn't have the -L1
flag, which means that you must explicitly flag for nil in both the find
and xargs
command.
If you're talking about running adhoc commands, then ignore this, but if this is a script that is developed on something that is not your production environment, and assuming your production environment never changes, then this is a really big deal.
Same as @an9swer, but with nil character as delimiter, using -n
flag to feed one result, at a time, as an argument of basename
. We also don't need the template flag -I
, since its inferred and the default behavior.
find . -iname '*.txt' -print0 | xargs -0 -n1 -- basename
An additional advantage to the above, is that xargs allows for executing the passed statement, in parallel, which means you could run the above in parallel, up to the number of cores that you have, to feed to some downstream process. If you are dealing with a number-approaching-infinity files or if workflow optimization is important, this is a big, low-hanging-fruit win
And yes, I am showing off a little, but its ok, because I am fine with being petty (at times).
basename
, you can use-execdir
instead of-exec
as that holds the current file (not path) in{}
(source)