0

Based on the question asked Here

I have few query:

  1. What is the + mean

    find . -name "*ABC*" -exec grep -H 'XYZ' {} +
    

    In the above line the last part +, what does it mean?

  2. How to find the meaning of any Linux command or syntax which i am not aware of by my own, for example + in the above example. I tried this command man exec but didn't find any help from that documentation.

Vinay
  • 13
  • 2

1 Answers1

4

You can find all this information in the man pages.

I always search for the argument i need, for example I just opened the man page for find and searched for the + sign.

I was able to find:

-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. Only one instance of '{}' is allowed within the command. The command is executed in the starting directory.

Perhaps it becomes more clear when you look at the entry before:

-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. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a '\') or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.

reference man find

alpha
  • 1,994
  • Thanks for the response. So basically it say if i run ** find . -name "ABC" -exec grep -H 'XYZ' {} + ** and it give me 2 files then the command line will be like ** grep -H 'XYZ' ABC.txt;grep -H 'XYZ' ABC2.txt **

    Feel free to correct me if I am wrong.

    – Vinay Jul 25 '18 at 08:21
  • 2
    No, the command line would be grep -H 'XYZ' ABC.txt ABC2.txt. You’d get the equivalent of grep -H 'XYZ' ABC.txt; grep -H 'XYZ' ABC2.txt if you used ; instead of +. – Stephen Kitt Jul 25 '18 at 08:22
  • One last question but not the least. How to write code if I want to pipe the output of 1st grep to another grep. I tried this code find . -name "ABC" -exec grep -H 'XYZ' {}|grep -v 'DEF' + But give me error. – Vinay Jul 25 '18 at 08:41
  • find . -name "ABC" -exec grep -H 'XYZ' {} + | grep -v 'DEF' – alpha Jul 25 '18 at 08:45
  • @alpha The command in your latest comment would find all files that contained the string XYZ, and would then remove the lines from that that matched DEF in either the text or the pathname (because of the -H used with the first grep). – Kusalananda Jul 25 '18 at 08:58
  • 1
    find -exec {} + has been introduced by David Korn in 1989, but it was undocumented until 1995 when it was added to POSIX. The people from the GNU universe rarely know it since GNU find was the last find implementation that added support for it, so people from that universe frequently use the inferior and vendor unique -print0. – schily Jul 25 '18 at 09:22