Find the answer to your Linux question:
Results 1 to 3 of 3
Could use some help with a project im working on: As an example, I created the following folder structure on my system: /test/NA/123tst/some files /test/PM/145test/some files /test/GN/1236test/some files /test/RG/234test/some files ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    2

    Search pattern from my file?

    Could use some help with a project im working on:

    As an example, I created the following folder structure on my system:
    /test/NA/123tst/some files
    /test/PM/145test/some files
    /test/GN/1236test/some files
    /test/RG/234test/some files
    ...


    I created a file named "pattern" with the following directories that i know will always exist in the path i am searching. (Normally i would extract this list from a database)

    123tst
    145test
    1236test
    234test
    ...

    I want to run a search (find, grep?) in order to search each line my above pattern file, locate the complete directory path for every file that matches the directory and append it to a "result" file like this:

    /test/NA/123tst/some files
    /test/NA/123tst/some files
    /test/NA/123tst/some files
    /test/NA/123tst/some files
    /test/PM/145test/some files
    /test/PM/145test/some files
    /test/PM/145test/some files
    /test/GN/1236test/some files
    /test/GN/1236test/some files
    /test/GN/1236test/some files
    /test/RG/234test/some files
    /test/RG/234test/some files
    /test/RG/234test/some files


    What would be the best way to complete this?


    I am playing around with:
    find . -type d -iname "directory for every line in pattern file" but this only returns the path without any files under it and i cant figure out how to make it loop for every line in my pattern file. Maybe you guys have some ideas using a loop statement in Bash to accomplish this?


    Thanks

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    If you don't have unwanted folders in /test, the following should do the job :

    Code:
    find /test -type f > out.file
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So you want to find every file under /test such that the absolute pathname contains a line from your pattern file.

    This is a bit complicated: find can't canonicalize paths, and it doesn't support taking regular expressions from a file. Therefore, we need to use a bit of a script here.

    For the following, I will assume that the pattern file contains no "strange" characters (that is, it's all alphanumeric).

    Code:
    #!/bin/bash
    
    IFS=$'\012'
    for file in $(find /test -type f); do
        fullpath=$(readlink -m "$file")
    
        exec 3< "pattern"
    
        while read -u 3 pattern; do
            if echo "$fullpath" | grep -q "$pattern"; then
                echo "$fullpath"
                break
            fi
        done
    
        exec 3<&-
    done
    So what does this do? Well, it starts by using find to get every file in the tree starting at /test. For each of these, it gets the absolute path, and then it opens the "pattern" file and looks at each pattern in it to match against the file path.

    If you could create a single regular expression of the patterns ("123tst|145test|1236test|234test"), and were careful about always using absolute paths to run find, this could be dramatically simplified to:
    Code:
    find /path/to/search -path "$singleregex" -type f
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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