Christian Tjhai
Christian Tjhai

Reputation: 111

Using Ag ("The Silver Searcher") to find hex codes

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.

Upvotes: 0

Views: 2431

Answers (2)

pixelbeat
pixelbeat

Reputation: 31768

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)

Upvotes: 0

Christian Tjhai
Christian Tjhai

Reputation: 111

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.

Upvotes: 1

Related Questions