[SOLVED] Searching for a specific hex string or byte array in various images
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Searching for a specific hex string or byte array in various images
Hi guys,
I am doing some research on image processing and I wanted to know if its possible to search for a specific hex string/byte array in various images. It would be great if it gives me a list of images that has that specific string. Basically what grep -r "" does. For some reason grep doesn't do the job. I am not familiar with strings. I did had a look at "man strings" but it didn't help much. Anyways, I would like to look for a specific hex string say "0002131230443" (or even specific byte array i.e. base64 string) in several images in the same folder. Any help would be highly appreciated.
I found this code which exactly does what I want using xxd and grep. Please find the command below:
FYI:
It'll return 1 if the content matches, 0 else.
xxd -p converts the file to plain hex dump, tr -d '\n' removes the newlines added by xxd, and grep -c counts the number of lines matched.
Does anyone know how to run the code above in a specific directory using batch script. I have around 400 images and I want it to return only 400 if all the 400 images have that particular string. I found this script code below but it runs the same code over and over for 400 times returning either 0 or 1 each time:
Code:
#!/bin/bash
FILES=/FILEPATH/*
for f in $FILES
do
echo "PROCESSING $f FILES"
echo "-------------------"
XXD -u $f | grep ABCD
echo "-------------------"
done
Its still not working for me. I am getting output stating no such file or directory. All my images have a file extension of TIF but it still says no such file or directory. The output that I get is as follows:
Code:
PROCESSING $f FILES
-------------------
cat: *tif No such file or directory
1
1
-------------------
PROCESSING $f FILES
-------------------
cat: *tif No such file or directory
1
1
-------------------
.
.
.
.
PROCESSING $f FILES
-------------------
cat: *tif No such file or directory
1
1
-------------------
It appears that the files are not there. This would indicate that you need to supply the correct path to the files in your cat command. That should do it.
Note number of "nibbles" must be even. Count could also be added if required.
Hi syg,
Your code worked perfectly but how do I get the total number of count at the end after the last line of output using "FILENAME". Thanks. I have around 400 images and your code is helping me find that string in all of them and so I need a final output of 400 at the end. How can I do that, please?
I entered and got this output. Output for first run was 1014, second criteria was 46.
Direct copy and paste from terminal.
Your output is confusing because it cannot be generated by that code. Did you insert the line somewhere in your do loop? The do loop only inputs one file at a time.
The total count your looking for is generated by this command without any loop.
Code:
david@david-Dell-System-XPS-L702X /media/david/Windows8/Documents and Settings/david/Pictures $ pwd
/media/david/Windows8/Documents and Settings/david/Pictures
david@david-Dell-System-XPS-L702X /media/david/Windows8/Documents and Settings/david/Pictures $ cat *png | xxd -p $f | tr -d '/n'|grep -c '1234' | awk '{ SUM += $1}; END { print SUM }'
1014
david@david-Dell-System-XPS-L702X /media/david/Windows8/Documents and Settings/david/Pictures $ cat *png | xxd -p $f | tr -d '/n'|grep -c '12345' | awk '{ SUM += $1}; END { print SUM }'
46
Last edited by DavidPhillips; 05-07-2016 at 01:11 AM.
Thank you for the wonderful code. It works without any issues but still giving me the Search string count as 1 for some reason. My total files were 3 out of which two files should definitely give me a genuine "Search string count" of 2 but instead its giving me 1. I tried on all the 400 images but grep couldn't handle such a large amount of files and gave me a error of "grep: memory exhausted"
Sigh...
Anyways the code given by syg00 works fine but how do I convert that code according to your way such that it gives me "Total String search count", please. I will give it a shot too.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.