using the ls command and grep to get the whole path

Status
Not open for further replies.

ilugd

Beware of the innocent
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

In Pursuit of "Happyness"
Re: using the ls command and grep to get the shole path

Try the locate/slocate/find commands :)
 

it_waaznt_me

Coming back to life ..
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

I see right through you.
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.
 
Last edited:

praka123

left this forum longback
Re: using the ls command and grep to get the shole path

^is it slocate now?and u need to run or cron "updatedb" .
 
OP
ilugd

ilugd

Beware of the innocent
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

I see right through you.
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

El mooooo
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.
 
Status
Not open for further replies.
Top Bottom