We want to grep the corresponding files that including some fixed strings, and then output those files just only in ls -l format.
For example:
A) the following method with multiple -exec output both the grep and ls -l.
#
#
# find /usr/bin -type f -exec grep -Eil '#\!\/usr\/bin\/csh' {} \; -exec ls -l {} \; >scripts_csh_list 2>&1
#
#
#
# cat scripts_csh_list
/usr/bin/which
-r-xr-xr-x 1 bin bin 1191 Sep 06 2007 /usr/bin/which
#
#
B) But we want to the better method output only with ls -l ... , and then we failed via sh -c 'grep ... {}'
Notes: all the following different combined commands for the find -exec ... output the same failed result.
#
#
# find /usr/bin -type f -exec sh -c 'grep -Eil '\/usr\/bin\/csh' {} && ls -ltr {}' \; >scripts_csh_list 2>&1t
#
#
# tail -6 scripts_csh_list
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
#
#
#
# find /usr/bin -type f -exec sh -c ' grep -Eil '\/usr\/bin\/csh' {} ' \; >scripts_csh_list 2>&1
#
#
# tail -6 scripts_csh_list
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
#
#
# find /usr/bin -type f -exec sh -c ' grep -Eil '\/usr\/bin\/csh' "{}" ' \; >scripts_csh_list 2>&1
#
#
# tail -6 scripts_csh_list
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
#
#
#
# find /usr/bin -type f -exec sh -c " grep -Eil '\/usr\/bin\/csh' {} " \; >scripts_csh_list 2>&1
#
#
# tail -6 scripts_csh_list
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
#
#
#
# find /usr/bin -type f -exec sh -c " grep -Eil '\/usr\/bin\/csh' {} " \; >scripts_csh_list 2>&1
#
#
# tail -6 scripts_csh_list
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open {}.
#
#
grep
to output anything, why are you using-l
? Why not use-q
? – muru Jun 15 '23 at 13:04-exec grep ... \;
works, why change it to-exec sh -c 'grep ...' \;
? Especially whenexec grep \; -exec ls \;
pretty much does the same thing as-exec sh -c 'grep && ls' \;
, i.e. runls
only if grep succeeds – ilkkachu Jun 15 '23 at 13:08find
is one of those that doesn't expand{}
embedded in other strings. Which is a good thing, as it's a very error-prone way to do anything. Pass the filename as an argument to the-exec sh -c ...
script – ilkkachu Jun 15 '23 at 13:09