Yes, find ./work -print0 | xargs -0 rm
will execute something like rm ./work/a "work/b c" ...
. You can check with echo
, find ./work -print0 | xargs -0 echo rm
will print the command that will be executed (except white space will be escaped appropriately, though the echo
won't show that).
To get xargs
to put the names in the middle, you need to add -I[string]
, where [string]
is what you want to be replaced with the argument, in this case you'd use -I{}
, e.g. <strings.txt xargs -I{} grep {} directory/*
.
What you actually want to use is grep -F -f strings.txt
:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by
newlines, any of which is to be matched. (-F is specified by
POSIX.)
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing. (-f is
specified by POSIX.)
So grep -Ff strings.txt subdirectory/*
will find all occurrences of any string in strings.txt
as a literal, if you drop the -F
option you can use regular expressions in the file. You could actually use grep -F "$(<strings.txt)" directory/*
too. If you want to practice find
, you can use the last two examples in the summary. If you want to do a recursive search instead of just the first level, you have a few options, also in the summary.
Summary:
# grep for each string individually.
<strings.txt xargs -I{} grep {} directory/*
# grep once for everything
grep -Ff strings.txt subdirectory/*
grep -F "$(<strings.txt)" directory/*
# Same, using file
find subdirectory -maxdepth 1 -type f -exec grep -Ff strings.txt {} +
find subdirectory -maxdepth 1 -type f -print0 | xargs -0 grep -Ff strings.txt
# Recursively
grep -rFf strings.txt subdirectory
find subdirectory -type f -exec grep -Ff strings.txt {} +
find subdirectory -type f -print0 | xargs -0 grep -Ff strings.txt
You may want to use the -l
option to get just the name of each matching file if you don't need to see the actual line:
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match. (-l is specified by
POSIX.)
Finding with ls
: first things first, ls | grep cisco
is a bit verbose, since cisco
isn't a regular expression. Try:
ls *cisco*
Using find
: along the same lines, -regex
is overkill with a simple, static pattern. How about:
find -name '*cisco*'
The quotes are required so the glob is interpreted by find
, not the shell. Also, -print
is required for many versions of find
, but is optional (and the default predicate) for others (e.g. GNU find
). Feel free to add it if you need it.
If you need to search for ‘cisco’ in the full pathname, you could try this:
find -path '*cisco*'
which is equivalent to find | fgrep cisco
.
Using find
with regular expressions: let's do that anyway, since this is what you want. Shamelessly copying from the GNU find
manpage:
-regex pattern
File name matches regular expression pattern. This is a match
on the whole path, not a search. For example, to match a file named
`./fubar3', you can use the regular expression `.*bar.' or `.*b.*3',
but not `f.*r3'.
What this means is that your regular expression is wrapped in an invisible ^...$
, so it must match every character in the full pathname of the file. So, as nwildner and otokan said in the comments, you should use something like:
find -regex '.*cisco.*'
And you don't even need the -regextype
for something this simple.
Best Answer
Use
find
as usual and delimit your lines with NUL. GNUsort
can handle these with the -z switch: