Find the answer to your Linux question:
Results 1 to 7 of 7
How to write a command so that it will find the match to get related search word? Code: grep abc -f *.* How can I search inside the directory of ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    5

    Linux command using grep

    How to write a command so that it will find the match to get related search word?

    Code:
    grep abc -f *.*
    How can I search inside the directory of the subdirectory?

    What argument that should I add?

    -Henry

  2. #2
    Linux User dxqcanada's Avatar
    Join Date
    Sep 2006
    Location
    Canada
    Posts
    259
    See "man grep" for the -R switch



    Men occasionally stumble over the truth,
    but most of them pick themselves up
    and hurry off as if nothing had happened.

    Winston Churchill


    ... then the Unix-Gods created "man" ...

  3. #3
    Just Joined!
    Join Date
    Feb 2008
    Posts
    5
    Thank guys...

    I put all the pattern into a text files...

    Code:
    grep -f ./test.txt -r -l *.*

    which:
    -f, --file=FILE obtain PATTERN from FILE
    -r, --recursive equivalent to --directories=recurse.
    -l, --files-with-matches only print FILE names containing matches

    After I execute the command I will found all the file and it will print out the files which matches with the pattern.

    Can I enhance this command so that it will take out all the files that contain matched pattern then copy and put it into a temporary place let say into a /home/fsloke/RESULT folder.

    Thank you

    -fsloke

  4. #4
    Linux User dxqcanada's Avatar
    Join Date
    Sep 2006
    Location
    Canada
    Posts
    259
    You could pipe the output to the move command.
    You should also look at xargs.



    Men occasionally stumble over the truth,
    but most of them pick themselves up
    and hurry off as if nothing had happened.

    Winston Churchill


    ... then the Unix-Gods created "man" ...

  5. #5
    Just Joined!
    Join Date
    Nov 2008
    Posts
    4
    A couple comments 1st.

    a) In general, "grep -r *.*" may miss a number of files: It won't recurse into directories that don't have a '.' in their name. Even plain '*' would miss the hidden files/dirs. (only under DOS '*.*' means also '*'
    I would step up to the parent directory and use "grep -r dirName" (plus you other options)

    b) If your subdirs contain several files of the same name, and you are trying to copy them into one flat directory, you are attempting to overwrite the previous by the next. Even if you are using cp -R since the source for each operation is a single path/file.

    Here is one way of copying selected (by grep) subtrees; it's a double tar from stdout to stdin (argument ' - ').
    You run it from above your project tree.

    tar -cf - $(grep -rl searchTerm aDir) | (cd /somePath ; tar -xf -)
    tar -cf - $(grep -rl searchTerm aDir) | (cd /somePath ; tar -xvf -) # to see it all

    I didn't add your "-f ./test.txt" to the example. Good luck.

    P.S. What's 'tag' or trick to publish text in code box style?

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Quote Originally Posted by horsu View Post
    P.S. What's 'tag' or trick to publish text in code box style?
    Start with a line that's left square bracket '[' followed by 'code' followed by right square bracket ']'.

    End with a line that's left square bracket '[' followed by '/code' followed by right square bracket ']'.

    You can use the 'Preview Post' button to see if it works before submitting the post.

  7. #7
    Just Joined!
    Join Date
    Nov 2008
    Posts
    4
    Thanks! (in hind side it's obvious...)

    So let's see how well nesting is handled -- something like square Russian dolls?

    '['code']'BIG BOX'['code']'small box '['tiny']' '[/code]' '['/code']' '['/code']'
    Code:
    BIG BOX
    
    Code:
    small box
    
    Code:
    tiny]
    I guess if I continue playing with dolls the admin will kick me out eventually,
    like "More than three dolls and you are out !"

    After some searching I figured it's just "square HTML" with xml consistancy (phpBB?)

    Thanks again. DONE.

Posting Permissions

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