Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I use find . -type f -name "*.xml" -exec grep -i "some text" {} \; -print in order to search with decendants folders for files with this content. however ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    4

    explanation for: find . -type f -name "*.xml" -exec grep -i "some text" {} \; -print

    Hi,

    I use

    find . -type f -name "*.xml" -exec grep -i "some text" {} \; -print
    in order to search with decendants folders for files with this content.
    however i dont understand this command.
    why exec and not pipe | ?
    what is the {}
    {} \;
    how come the print knows to print the resolutions? why does the print has a - before it?

    Thanks for anyone who could help me.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,970
    Quote Originally Posted by tomerbd View Post
    Hi,

    I use



    in order to search with decendants folders for files with this content.
    however i dont understand this command.
    why exec and not pipe | ?
    what is the {}
    {} \;
    how come the print knows to print the resolutions? why does the print has a - before it?

    Thanks for anyone who could help me.
    1. The {} is a symbol that is replaced with each file found that matches the -name argument.
    2. The -name argument tells find to only match the files found with the name pattern. BTW, you need to use single quotes and not double quotes as shown. Ie, -name '*.xml' and not -name "*.xml"
    3. I would assume that you just want a list of files that contain the string specified by grep? Then you should execute the command like this: find . -type f -name '*.xml' -exec grep -i -l "some text" {} \;
    4. The -exec tells find to execute the specified command for each file that matches -name 'pattern', replacing the {} with each file name.

    Doing it the way I show above, you will get a list of the .xml files that contain the string you are searching for, ignoring the upper/lower case differences. That's the -i argument to grep. The -l argument just tells grep to output the file name if the pattern is found in the file. I use this construct very frequently to find files with a specific pattern in an entire directory tree.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    4

    Thanks alot another small question

    Thanks alot that was very helpful.
    Isn't pipe used to take the output of one command as input to another? so why do we use exec and not pipe to pipe the output of the find into the grep?

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,970
    Quote Originally Posted by tomerbd View Post
    Thanks alot that was very helpful.
    Isn't pipe used to take the output of one command as input to another? so why do we use exec and not pipe to pipe the output of the find into the grep?
    Because find only returns file names, normally. Piping the output from find to grep would be like using the -name argument in find. IE, "find . -type f -name '*.xml'" is more or less the same as "find . -type f | grep '\.xml$'". Since you want to search within each file, you need to run grep against each candidate file, hence the -exec argument to find.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...