Grep whole path

Hi I would like to do this

ls * | grep pattern

But I would like instead of just showing the file with the pattern the whole path of the files that match the patterns

2

1 Answer

One way would be to use find to do your matching, though you might have to change the pattern to match the find syntax instead of greps as I think they are not identical.

find "$PWD" -maxdepth 1 -regex 'pattern'

should show you the full path and the -maxdepth will prevent it from going into subdirectories. If you just want to use globs instead of regex you can use -name 'glob' syntax instead, and then use the * as you would with ls.

Here's an example output I see:

$ find "$PWD" -maxdepth 1 -regex '.*sh$'
/home/erenouf/tmp/scratch/t.sh
/home/erenouf/tmp/scratch/parseIW.sh
/home/erenouf/tmp/scratch/output.sh

since I was in ~/tmp/scratch at the time and looked for files that ended in sh

In this case I can get the same output with

find "$PWD" -maxdepth 1 -name '*sh'

To have it look in subdirectories you can just remove teh -maxdepth 1 part of each command

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like