Gilmantas Morkus
Gilmantas Morkus

Reputation: 5

Bash: How to use 'find' for a file with specific extension but that it would also give its previous directory

I'm quite stuck on a piece of code where I have to use 'find' to locate all files with a specific extension but that the output would also give the particular directory that the specific file is located in. For example:

find . -name "*.exe" -o -name "*.bat" -type f

I use this code to find all files with the extension .exe and .bat and the output it gives me is:

./test/test1/test2/hello.exe

My question is would it be possible for the command to only give me the output like this:

test2/hello.exe

As in only the directory that the file is located in.

Thanks a lot in advance!

Upvotes: 0

Views: 68

Answers (1)

filzerbino
filzerbino

Reputation: 111

Try this:

find . -name "*.exe" -o -name "*.bat" -type f | grep -Po '[^/]*?/[^/]+$'

Upvotes: 1

Related Questions