Author Topic: bash - find text in a file on system w/o knowing file location  (Read 1316 times)

0 Members and 1 Guest are viewing this topic.

Offline frog

  • Knight
  • **
  • Posts: 232
  • Cookies: 16
    • View Profile
This line alone will cat every file that the user has permission to read.
Code: (bash) [Select]

for item in $(find /* -name "*"); do cat $item; done


Now we add another loop inside of this mechanism along with a grep and some other things to find what we want.
Code: (bash) [Select]
for item in $(find /* -name "*")
  do
    for filtered in `cat $item | grep x`
      do
        echo "Filename: "; echo $item; echo ""; echo "Data:"; echo $filtered; echo ""; echo ""
    done
done
Change the parameters of grep inside the second 'for' loop to modify the search.

This script takes forever to run depending on the performance of your system. It also makes your terminal program shit its pants when you run it. It's just for fun, and I was thinking you could use `hexdump -C | grep x` to find certain patterns of bytes in any given file.
« Last Edit: June 01, 2012, 10:57:54 pm by frog »

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: bash - find text in a file on system w/o knowing file location
« Reply #1 on: May 25, 2012, 01:28:38 pm »
Code: [Select]
$ grep -rn findme /
or using find/grep:

Code: [Select]
$ find / -exec grep -Hn findme {} \;

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: bash - find text in a file on system w/o knowing file location
« Reply #2 on: May 25, 2012, 02:18:11 pm »
Nice to see you and your one-liners again, xzid :D
« Last Edit: May 25, 2012, 02:18:36 pm by Kulverstukas »

Offline frog

  • Knight
  • **
  • Posts: 232
  • Cookies: 16
    • View Profile
Re: bash - find text in a file on system w/o knowing file location
« Reply #3 on: May 25, 2012, 09:45:16 pm »
That's much cleaner. I guess that's what I get for not using the man pages.