• Uncategorized

About shell : find-full-path-given-partial-path

Question Detail

I know how to find files using

find . -name "file_name"

But if I am given one part of a path, say “folder1/subfolder2/”, how do I get all the full path that contains this partial path?

Example

partial path: folder1/subfolder2/

desire result:

/bob/folder1/subfolder2/yo/
/sandy/folder1/subfolder2/hi/

Question Answer

Use the -path option:

find . -path '*/folder1/subfolder2/*'

You may do it like below:

find . -path "*folder1/folder2" -prune -exec find {} -type f -name file.txt \;

With -prune you don’t recurse after first match in a directory

This one worked for me (using bash)

ls -l /**/folder1/subfolder2/**

I came up with this, other solutions did not work for me,

find1 is a function

find1 ()
{
    for file in `find -name $1`;
    do
        full_path=$PWD/$file;
        echo $full_path;
    done
}

If you dont want to stay posix-compliant, at least on Linux you can also use the -regex (and -regextype) option for this purpose.

For instance:

find folder/ -regextype posix-extended -regex "(.*/)?deer/(.*/)?beer"

will match

folder/deer/beer
folder/deer/dir/forest/beer/
folder/forest/deer/dir/forest/beer/

etc.

See linux man for details.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.