0

I used to use find command for linux to find hex code in php code as the following:

find "/www_root/myfile" -type f -name "*.php" | xargs grep -il x29

The above command works like a charm, but I may need a faster searcher as my files are keep growing.

I would like to test using The Silver Searcher ag Command, and I have been searching around of how to find hex code using "ag" command, but cannot find any in their documentation. So, here I am in this forum, seeking for answer in the hope someone has experienced on searching hex code using the silver searcher ag.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 5
    I'm confused. You appear to be searching for a literal string of `x29` not a "hex code". Does that not just work with `ag`? Or is that command not what you were actually doing? – Etan Reisner Mar 30 '15 at 15:34
  • 1
    Indeed, the search string is a _literal_ string that just _happens to be_ the textual representation of a hex. code. – mklement0 Mar 31 '15 at 02:51

2 Answers2

1

Somehow, I did not found this a bit earlier, but now has been found. What I did not understand was actually the "ag" command and functions that could be used but now it's ok.

I will share the answer here just in case someone might need it. In order to replace the find command as I questioned previously, we can use this:

ag --php -l 'x29' "/www_root/myfile"

Output of the previous command would be something like:

/www_root/myfile/menus.php

If it found more files, then it will list files separated by new line "\n":

/www_root/myfile/menus.php
/www_root/myfile/contents.php

If you would like to know which line numbers, you can then drop the "-l" attribute and then replace it with "--numbers":

ag --php --numbers 'x29' "/www_root/myfile"

Output:

30:matches string found here
89:matches string found here

Ok, now we can combine it with "cut" command to get only the line numbers:

ag --php --numbers 'x29' "/www_root/myfile" | cut -f1 -d:

Output:

30
89

Most of the commands are similar to "ack", so for you who wants to use "ag" can find "ack" documentation as they have documented everything.

Last but not least, if you would like to know the statistic, you can add --stats to the above command as the following:

ag --php -l --stats 'x29' "/www_root/myfile"

That simple command will output as the result shown:

31 matches
1624 files searched
18686162 bytes searched
0.094022 seconds

So, yes, choosing The Silver Searcher was the right choice. That's all and have a great day.

  • 1
    Nice. While `ag`'s own documentation is scant, it _does_ come with a `man` page. `--php` is an example of a _file type_ supported by `ag`, which matches _multiple_ filename extensions (suffixes). Run `ag --list-file-types` to see all available types. `ag`, similar to its progenitor `ack`, searches _recursively_ by default. – mklement0 Mar 31 '15 at 02:55
0

Also have a look at findrepo which you could use like:

cd /www_root/myfile && findrepo -n 'x29' '*.php'

That should be faster than ag (and I'm surprised you said that your initial find | xargs grep command was slower than ag)

pixelbeat
  • 30,615
  • 9
  • 51
  • 60
  • Thanks for the link, but can you provide more info on how to use? Regarding find | xargs grep I have tested by executing time command which shows: real 0m0.915s The following was executed by using ag: real 0m0.087s – Christian Tjhai Mar 31 '15 at 07:17