Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
Hi folks, What will be an easy and effective way searching file content? E.G I need to find a WORD or a PHRASE on a file? TIA B.R. satimis...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,814

    How to search file content


    Hi folks,


    What will be an easy and effective way searching file content? E.G I need to find a WORD or a PHRASE on a file? TIA


    B.R.
    satimis

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,260
    You use grep. Open a terminal and type grep word filename. It will print out the line containing the search term (if it is present).

    For a phrase containing spaces, you will need to put the whole thing in quotes.

    You can also search for strings with partial wildcards. Read the man page for grep for more info.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  3. #3
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,814
    Quote Originally Posted by hazel View Post
    You use grep. Open a terminal and type grep word filename. It will print out the line containing the search term (if it is present).

    For a phrase containing spaces, you will need to put the whole thing in quotes.

    You can also search for strings with partial wildcards. Read the man page for grep for more info.
    Hi hazel,


    Thanks for your advice.


    I tried to find files containing the word 'namecard' on all files /path/directory;

    # grep -iR namecard /media/usbdisk/Documents_2006_2008/
    Code:
    /media/usbdisk/Documents_2006_2008/Fedora/Server/Gani/gani_20070326.txt:pendrive and namecard CD.  The complete OS is limited to 
    50MB.
    The output just repeats with the mouse pointer pending there.


    Could you please shed me some light what does it means? How to proceed?

    TIA


    B.R.
    satimis

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined! garry_3peace's Avatar
    Join Date
    Oct 2008
    Posts
    67
    As far as I know, that's mean a file name gani_20070326.txt does contain word "namecard" which in the sentence "pendrive and namecard CD. The complete OS is limited to 50MB." Maybe to make grep give more clearer result you may add -n. That's for line number.

    So the syntax will be like this:
    grep -niR namecard /media/usbdisk/Documents_2006_2008/

  6. #5
    Just Joined!
    Join Date
    Aug 2008
    Posts
    9
    one could also do

    cat /media/usbdisk/Documents/*.* | grep -niR 'whatever'

  7. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    If you want to know just the name of the files that contain the phrase then use the -l option:

    Code:
    grep -liR namecard /media/usbdisk/Documents_2006_2008/

  8. #7
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,814
    Quote Originally Posted by garry_3peace View Post
    As far as I know, that's mean a file name gani_20070326.txt does contain word "namecard" which in the sentence "pendrive and namecard CD. The complete OS is limited to 50MB." Maybe to make grep give more clearer result you may add -n. That's for line number.

    So the syntax will be like this:
    grep -niR namecard /media/usbdisk/Documents_2006_2008/
    Hi garry_3peace,


    Your advice works. Thanks


    satimis

  9. #8
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,814
    Quote Originally Posted by vsemaska View Post
    If you want to know just the name of the files that contain the phrase then use the -l option:

    Code:
    grep -liR namecard /media/usbdisk/Documents_2006_2008/
    Hi vsemaska,


    Your advice works here. The output is very precise.

    If I only need knowing the directory named "namecard" can I run grep to do the job instead of using "find".

    Can I use wildcard *namecard* ? TIA


    satimis

  10. #9
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,814
    Quote Originally Posted by mike171562 View Post
    one could also do

    cat /media/usbdisk/Documents/*.* | grep -niR 'whatever'
    Hi mike171562,


    The command does not work here. I test it. Because there are subdirectories under.


    satimis

  11. #10
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Quote Originally Posted by satimis View Post
    Hi vsemaska,


    Your advice works here. The output is very precise.

    If I only need knowing the directory named "namecard" can I run grep to do the job instead of using "find".

    Can I use wildcard *namecard* ? TIA


    satimis
    I don't quite understand. First you were searching for the string 'namecard' in files and now you want to search a directory called namecard? If so, and that directory is a sub-directory of the previous one then the command would look like:

    Code:
    grep -li namecard /media/usbdisk/Documents_2006_2008/namecard/*
    
    or 
    
    grep -li namecard /media/usbdisk/Documents_2006_2008/*namecard*/*
    Notice that I took out the -R option so as not to do recursive directory searches. This required adding an '*' to the end so it'll search all files.

Page 1 of 2 1 2 LastLast

Posting Permissions

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