Languages
[Edit]
EN

Bash - detect PNG, JPG, GIF file using file bytes (magic numbers in file)

10 points
Created by:
Pearl-Hurley
369

In this short article, we would like to show how in Bash, detect PNG, JPG, or GIF files when we don't know file extension - that king of detection uses magic numbers located inside the file).

Example detect_image_type.sh file content:

#!/bin/bash

function is_jpg()  # args: file_path
{
	#              JFIF (chars)  =  4a464946 (in hex)
	[ "$(xxd -s 6 -l 4 -p "$1")" = "4a464946" ] && return 0 || return 1
}

function is_png()  # args: file_path
{
	#               PNG (chars)  =  504e47 (in hex)
	[ "$(xxd -s 1 -l 3 -p "$1")" = "504e47" ] && return 0 || return 1
}

function is_gif()  # args: file_path
{
	#               GIF (chars)  =  474946 (in hex) 
	[ "$(xxd -s 0 -l 3 -p "$1")" = "474946" ] && return 0 || return 1
}


# Usage example:

echo -e "\n--------------------------------\nJPG files:\n";
for file_name in *.*
do
	is_jpg "$file_name" && echo "$file_name"
done

echo -e "\n--------------------------------\nPNG files:\n";
for file_name in *.*
do
	is_png "$file_name" && echo "$file_name"
done

echo -e "\n--------------------------------\nGIF files:\n";
for file_name in *.*
do
	is_gif "$file_name" && echo "$file_name"
done

echo -e "\n--------------------------------\n"
echo -e "DONE!\n"

Running:

./detect_image_type.sh

Example output:

--------------------------------
JPG files:

picture.jpg
mountains.jpeg
river.tmp
sun.tmp

--------------------------------
PNG files:

ball.png
square.unknown

--------------------------------
GIF files:

animation.dat

--------------------------------
DONE!

 

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join