Find the answer to your Linux question:
Results 1 to 7 of 7
Noob alert... I'm trying to filter out files that match certain criteria... e.g. I want to list all files that contain the character sequences "pol" and "rim", find . -type ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    19

    piping find to grep - syntax problem

    Noob alert...

    I'm trying to filter out files that match certain criteria...

    e.g. I want to list all files that contain the character sequences "pol" and "rim",

    find . -type file | grep "pol"

    shows all files containing pol in their name...

    find . -type file | grep "(pol|rim)"

    shows nothing... how can I grep for various character sequences in this manner, or am I barking up the wrong tree?

    Cheers

    Steve

  2. #2
    Just Joined! cheapscotchron's Avatar
    Join Date
    Dec 2008
    Location
    swamps of jersey
    Posts
    68
    This will find any file on your root drive that has pol in the filename
    # find / -name *pol* -print 2>/dev/null

  3. #3
    Just Joined!
    Join Date
    Dec 2008
    Posts
    19
    Quote Originally Posted by cheapscotchron View Post
    This will find any file on your root drive that has pol in the filename
    # find / -name *pol* -print 2>/dev/null
    Yep... I know that much... What I'm trying to do is find files that match multiple criteria - thus the pipe to grep...

    Any more ideas?

  4. #4
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Using just find
    Code:
    find . -type f \( -name "*pol*" -o -name "*rim*" \)
    Using grep basic regular expresions
    Code:
    find . -type f | grep -e pol -e rim
    Using grep extended regular expressions
    Code:
    find . -type f | grep -E  '(pol|rim)'

  5. #5
    Just Joined!
    Join Date
    Dec 2008
    Posts
    19
    That's great thanks!!!

    Now I have had time to sit down and read the man page for find and grep, your replies are making more sense to me.

    The -E option for grep... I can read this that is needed when extended regex evaluations are required... I'm just not sure what the diffeerence is between the abilities of basic and extended expressions. More reading required.

    Anyway - your answer has also helped me to use grep to greater effect in general searching of my server logs now

    Thanks!

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    if you are not recursing directories, just use shell wild cards
    Code:
    ls *rim* && ls *pol*

  7. #7
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    ghostdog74,

    Your example won't produce the desired results. He wants filenames with rim OR pol in them. Your command will execute 'ls *pol*' only if 'ls *rim*' is successful. If there are no files with rim in their names then the 'ls *pol*' won't execute.

    If he wants find to stay within the current directory then he can use the '-maxdepth 1' option

Posting Permissions

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