Normally, you don't. I mean sure, you can play around with -print0
and then -0
with xargs
, but why? Just use find
's -exec
option which is designed for this sort of thing. From man find
:
-exec command ;
Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ;
is encountered.
So you can just do this to open one file:
find . -iname 'my*file.txt' -exec vim {} \;
Or, if can you have many matching files and want to open all of them:
-exec command {} +
This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines.
In your case, that would look like this:
find . -iname 'my*file.txt' -exec vim {} +
If you had to use xargs
and your xargs
happened to be the one from GNU findutils, from a shell with support for Korn-style process substitution, the equivalent would look like:
xargs -r0a <(find . -iname 'my*file.txt' -print0) vim
Where:
- we use
-print0
to match the format expected by xargs -0
(standard equivalent of xargs --null
).
- we use
-r
to avoid running vim
without argument if there's no matching file.
- we pass the list as a file (here a sort-of-named-pipe) argument to
-a
instead of xargs
stdin so as to preserve vim
's stdin.
*
,because it does not expand*
inside quotes. NUL is entirely different to whitespace.