• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

search files in Unix quick question

ntrights

Senior member
Hi evryone

im looking for a file or a text string within a file. The text string or filename is 'lookup'

ls | grep 'lookup'

find / -name 'look*

grep -w '\<look' *

du | grep 'lookup' | more

none of these searches rendered anything. are there any other good search commands that can scan the whole hd for a filename or txt within file that says 'lookup' or starts with 'look'

Thanks in advance

Cheers
ntrights





 
cst@unix47~% find |grep plan
./bin/plan_update
./public/.plan
./public/plan

cst@unix47~% grep -r spam *
public/spam.script: reject "This message looks like spam to me.
public/spam.script:2. You sent the email to me and to other andrew IDs similar to mine. Too many spammers email cst, cstanley, ctf, and so on in one email, and legitimate mail like that usually uses bcc.
public/spam.script:3. This domain has sent too much spam mail. Mail is no longer
public/spam.script:I get tons of spam there. You can also reach me on AIM (CTho9305), ICQ
public/spam.script~: reject "This message looks like spam to me.
public/spam.script~:2. This domain has sent too much spam mail. Mail is no longer
public/spam.script~:but you may have to try a few times - I get tons of spam there.
 
They're kinda two different things so you'd use two different commands.

find . | grep -i spam

grep -ir spam .

(. is better than * with grep -r because it will search everything under the current directory, while * will miss the dotfiles)
 
Excellant!

find |grep lookup

and

grep -r look *


worked out nicely (added to my unix notes)

Thanks!

Cheers,
ntrights

 
Originally posted by: BingBongWongFooey
They're kinda two different things so you'd use two different commands.

find . | grep -i spam

grep -ir spam .

(. is better than * with grep -r because it will search everything under the current directory, while * will miss the dotfiles)


Added to my Unix notes!

I found the file i was looking for 😀

Thanks BBWF!



 
I personally like:


du -a /folder/ 2> /dev/null |grep lookup

That will find filenames.... du with out the -a modifier only shows directories. The -a is for ALL files. It's also fast enough that I often run it from / to find files.

If you don't want the sizes of the text files, like if your going to output them into file I would use awk to edit the stuff.


Searching for text strings???

Well I wouldn't try this from root, but if you know generally what folder it is in. Still can take a while if you have bunches of files to look thru.

for i in `du -a ./ | awk 'FS=" " {print $2" "$3" "$4" "$5" "$6" "$7" "}'`; do if grep lookup $i > /dev/null 2> /dev/null; then echo $i; fi ; done

That would output the filename, eliminate the "> /dev/null" behind grep and it will output he line of text then the filename.

Don't worry if it looks compilated. I am a bad programmer/scripter and bad code is hard to understand. If you want me to explain what is going on then I will.


Probably would be a better idea to use Python or Perl to do this, because my method is VERY slow.
 
One thing about "find"...

Doesn't find use some sort of cache for the directories or something like that? Once I was looking for some new files that I misplaced and "find" couldn't find it, that's why I generally use du -a method.

edit: Or maybe I am just smoking crack.
rolleye.gif


for i in `find /directory/path/`; do if grep lookup $i > /dev/null 2> /dev/null; then echo $i; fi ; done

For searching for specific text strings in text files, then it echos the name and location of the file were it finds the text string.

A lot nicer. 🙂

edit x2: forgot the " " around the $i for filenames with spaces.

so Drag's stupid script part 3 is:

for i in `find /directory/path/`; do if grep lookup "$i" > /dev/null 2> /dev/null; then echo $i; fi ; done
 
Hm, your script is printing the filenames that contain certain text.

I would do:

grep -rH path pattern | sed -e 's/:.*$//g' | uniq

You were thinking of locate. The locate database is updated usually by a cron job every night, and it allows for fast searching of filenames that you expect have been there at least a day. But find searches the hard drive manually.
 
There's also the -l flag to gnu grep:

-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would
normally have been printed. The scanning will stop on the first match.

I don't know if that's standard or not though.
 
Back
Top