DIGIT-ARC read-only archive · snapshot: July 23, 2026

using the ls command and grep to get the whole path

8 posts · originally from https://geek.digit.in/community/threads/using-the-ls-command-and-grep-to-get-the-whole-path.59272/

ilugd
ilugd Original post #1
Jun 1, 2007 at 2:43 PM
I have been using the following command to search for files on my hard disk

ls -R | grep pattern
to find files named pattern or something.

But the problem is that i am unable to find the whole path. So then I have to search again trying in increments of 5 lines or so before that. Like
ls -R |grep -B 5 pattern
and if the path is not listed then
ls -R |grep -B 10 pattern
and so on. finally I get the path.

There has got to be a simpler way right?
I tried searching but am not able to make much sense out of them. Can someone please help?
kalpik
kalpik #2
Jun 1, 2007 at 2:48 PM
Re: using the ls command and grep to get the shole path

Try the locate/slocate/find commands :)
it_waaznt_me
Jun 1, 2007 at 2:55 PM
Re: using the ls command and grep to get the shole path

Why dont you use a command like this :

find . -ls -name searchpattern
Sykora
Sykora #4
Jun 1, 2007 at 3:01 PM
Re: using the ls command and grep to get the shole path

I use the locate command to search the filesystem. However, if you've just created a file which you want to search for, you'll have to rebuild the database with the rehash command. The only disadvantage with locate is that you can't tell it to search just under a particular directory, ie it will always search the entire filesystem. However, this can be remedied with a grep.

eg,

Code:
This will locate PATTERN wherever it occurs in any filename :
$ locate PATTERN

This will locate PATTERN if it is regex :
$ locate --regex PATTERN

This will filter based on a directory :
$ lcoate --regex PATTERN | grep <directory path>

EDIT : It's updatedb, and not rehash, sorry. rehash is for completion.
praka123
Jun 1, 2007 at 3:43 PM
Re: using the ls command and grep to get the shole path

^is it slocate now?and u need to run or cron "updatedb" .
ilugd
ilugd #6
Jun 1, 2007 at 5:44 PM
Re: using the ls command and grep to get the shole path

i most probably will be disabling any indexing services (locate uses indexes, right?) so i don't really want to do that. What i am really wondering about is whether there is a way to make ls or ll or any such directory listing commands list the whole path of a file in each line. Grep will take care of the rest, i hope.
Sykora
Sykora #7
Jun 1, 2007 at 6:20 PM
Re: using the ls command and grep to get the shole path

AFAIK, the cron job for the database is enabled by default. You don't have to do anything.
eddie
eddie #8
Jun 6, 2007 at 2:36 AM
Try the following script. It will not give you exact path but if you run it in a folder along with less command then you can know the exact location and the depth of the file in the current directory.