1

I need to list all directory paths including a text which is part of thier sub-directories. For example, the text is "tpcc" and I have two paths including tpcc as following:

/home/arghy/sampledir1/**tpcc**-uva/subdir1/subdir2
/home/arghy/sampledir2/**tpcc**-postgre/subdir1/subdir2

I want to list these paths using a command and give "tpcc" as the aforementioned text. What is the command to this?

4

To find all directory pathnames from the current directory downwards that contain the particular string tpcc:

find . -type d -path '*tpcc*'

The -path predicate will match the pattern against the pathnames that find comes across, and if it matches the pattern, the current pathname will be printed.

If you don't want the pattern to be matched at the end of the current pathname, use *tpcc*/* as the pattern instead. Matching a / somewhere after tpcc would cause find to find directories strictly beneath any directory whose name contains tpcc.

The -type d restricts the search to directories only.

Would you want to wrap this into a simple to use shell function:

pathfind () {
    case $# in
        1)  # only a string was given
            searchpath=.
            searchstring=$1
            ;;
        2)  # search path and string was given
            searchpath=$1
            searchstring=$2
            ;;
        *)  # anything else is an error
            echo 'Expected one or two arguments' >&2
            return 1
    esac

    find "$searchpath" -type d -path "*$searchstring*"
}

You could use this as either

pathfind tpcc

or as

pathfind /some/path tpcc

or

pathfind /some/path 'tpcc*/'
0

The task you describe would be satisfied with

find . -type d | grep tpcc

but this will also list the "branch" directories, not just the "leaf" directories:

/home/arghy/sampledir1/tpcc-uva
/home/arghy/sampledir1/tpcc-uva/subdir1
/home/arghy/sampledir1/tpcc-uva/subdir1/subdir2

Is this okay or do you want to skip directories with more subdirs inside?

0
sudo find DIRECORY  -type f -exec grep -Iq . {} \; -exec dirname {} \; | sort|uniq

check this Link: URL

فارسی: با دستور بالا میتوانید پاسخ سوال خود را دریافت کنید. موفق و پیروز باشید

یاعلی ع

  • 1
    This does not answer the question of how to find pathnames containing a particular string. – Kusalananda Apr 30 at 12:24
  • now it is reformed. – Behrooz Mohamadi nasab Apr 30 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.