Grep whole path

2

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

HeyHoLetsGo

Posted 2015-12-22T14:03:24.577

Reputation: 23

So you want to look only in the current directory, but see the full absolute paths to the files, is that correct? – Eric Renouf – 2015-12-22T14:08:42.580

No, I want to look in all subdirectories. Sorry for not being explicit on it. – HeyHoLetsGo – 2015-12-22T14:16:26.733

Answers

2

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

Eric Renouf

Posted 2015-12-22T14:03:24.577

Reputation: 1 548

I made it work this way

find | grep pattern

For me it didn't work find "$PWD" -regex 'pattern' – HeyHoLetsGo – 2015-12-22T14:20:37.623

This last also worked find "$PWD" -name '*bed' – HeyHoLetsGo – 2015-12-22T14:25:11.897

@biorunner88 I suspect the regex one would work if your pattern was '.*bed$', but if the glob works I'd go with that approach myself – Eric Renouf – 2015-12-22T15:42:14.777