Skip to main content
incorrect use of match()
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

I'm on RHEL 7. This works for me:

find . -path "*/foo/*" ! -path '*/foo/*/*' -type f

Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)

The DOT says "Start looking in the current directory"

the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".

The -type f says give me only the files in a matching directory.
Looks

Looks like

-path "*/foo/*" ! -path '*/foo/*/*'  

Doesn't get everything as it misses things like ./a/foo/b/foo/c. Not elegant

If you can guarantee that file and directory names won't contain newline characters, but it worksyou could post-process the output with awk like:

find . -path "*/foo/*" -type f | awk -F'F/' '{if (match$'$(NF-1),"foo")) print $0}' == "foo"'

I'm on RHEL 7. This works for me:

find . -path "*/foo/*" ! -path '*/foo/*/*' -type f

Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)

The DOT says "Start looking in the current directory"

the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".

The -type f says give me only the files in a matching directory.
Looks like

-path "*/foo/*" ! -path '*/foo/*/*'  

Doesn't get everything. Not elegant, but it works:

find . -path "*/foo/*" -type f | awk -F'/' '{if (match$(NF-1),"foo")) print $0}'  

I'm on RHEL 7. This works for me:

find . -path "*/foo/*" ! -path '*/foo/*/*' -type f

Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)

The DOT says "Start looking in the current directory"

the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".

The -type f says give me only the files in a matching directory.

Looks like

-path "*/foo/*" ! -path '*/foo/*/*'  

Doesn't get everything as it misses things like ./a/foo/b/foo/c.

If you can guarantee that file and directory names won't contain newline characters, you could post-process the output with awk like:

find . -path "*/foo/*" -type f | awk -F/ '$(NF-1) == "foo"'
Updated for OPs clarification
Source Link
Scottie H
  • 744
  • 3
  • 12

I'm on RHEL 7. This works for me:

find . -path "*/foo/*" ! -path '*/foo/*/*' -type f

Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)

The DOT says "Start looking in the current directory"

the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".

The -type f says give me only the files in a matching directory.
Looks like

-path "*/foo/*" ! -path '*/foo/*/*'  

Doesn't get everything. Not elegant, but it works:

find . -path "*/foo/*" -type f | awk -F'/' '{if (match$(NF-1),"foo")) print $0}'  

I'm on RHEL 7. This works for me:

find . -path "*/foo/*" ! -path '*/foo/*/*' -type f

Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)

The DOT says "Start looking in the current directory"

the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".

The -type f says give me only the files in a matching directory.

I'm on RHEL 7. This works for me:

find . -path "*/foo/*" ! -path '*/foo/*/*' -type f

Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)

The DOT says "Start looking in the current directory"

the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".

The -type f says give me only the files in a matching directory.
Looks like

-path "*/foo/*" ! -path '*/foo/*/*'  

Doesn't get everything. Not elegant, but it works:

find . -path "*/foo/*" -type f | awk -F'/' '{if (match$(NF-1),"foo")) print $0}'  
added exclusion for nested directories
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264

I'm on RHEL 7. This works for me:

find . -path "*/foo/*" ! -path '*/foo/*/*' -type f

Note the DOT before the -path. (Or substitute youyour path there, such as /home/$USER)

The DOT says "Start looking in the current directory"

the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".

The -type f says give me all filesonly the files in a matching directory.

I'm on RHEL 7. This works for me:

find . -path "*/foo/*" -type f

Note the DOT before the -path. (Or substitute you path there, such as /home/$USER)

The DOT says "Start looking in the current directory"

the -path says "Look for anything, followed by a sub-directory named foo, followed by anything"

The -type f says give me all files in a matching directory

I'm on RHEL 7. This works for me:

find . -path "*/foo/*" ! -path '*/foo/*/*' -type f

Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)

The DOT says "Start looking in the current directory"

the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".

The -type f says give me only the files in a matching directory.

Source Link
Scottie H
  • 744
  • 3
  • 12
Loading