2

I am looking for a Linux command in RHEL v6.x to search a particular directory structure in the current directory.

Though I know the below command which will search a particular single directory in the current directory which is working for a single directory but not for the directory structure.

Working:

find /home/dir1/* -name "def"


Not Working:

find /home/dir1/* -name "abc/def"

I also tried below command but it is also listing the files inside this directory But I don't want to list the file inside this directory. I only want to list the full path of all the directory which has got this abc/def directory structure.

locate abc/def/

Can anyone please help me.

Thanks In Advance.

2
  • How the manual describes -name: "Base of file name (the path with the leading directories removed) matches shell pattern pattern."
    – Quasímodo
    Commented Sep 25, 2020 at 12:35
  • Under man find for -name, it explicitly states: Because the leading directories are removed, the file names considered for a match with -name will never include a slash, so `-name a/b' will never match anything (you probably need to use -path instead). Commented Sep 25, 2020 at 21:43

3 Answers 3

8

The -name test only matches the last path component. To match something like abc/def you will need -path:

$ mkdir -p somedir/otherdir/abc/def/ghi
$ find somedir -path '*/abc/def'
somedir/otherdir/abc/def
1
  • Thanks. It worked. :)
    – Varun Jain
    Commented Sep 28, 2020 at 10:41
0

How about this.

find /home/dir1/ -type d | grep 'abc/def'

or ff you only want to list directories up to a certain depth.

find /home/dir1/ -maxdepth 2 -type d | grep 'abc/def'

-d - directory

-maxdepth levels of directories below the starting-points.

2
  • If POSIX find provides -path, there is no advantage in piping find's output to a separate process.
    – Quasímodo
    Commented Sep 25, 2020 at 12:40
  • Maybe so, but it works and should not be discounted as an option.
    – canon
    Commented Sep 25, 2020 at 12:44
0
tree <base dir>
       -L level
             Max display depth of the directory tree.
       -P pattern
              List  only those files that match the wild-card pattern.

1
  • 4
    Perhaps your answer could be enhanced to address "I only want to list the full patch of all the directory which has got this abc/def directory structure" instead of just being a copy of the man-page?
    – Jeff Schaller
    Commented Sep 25, 2020 at 17:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .