Americas

  • United States
sandra_henrystocker
Unix Dweeb

Working with image files on the Linux command line

How-To
Jan 23, 20237 mins
Linux

There's a lot to learn about image files on the command line, from verifying file format to finding out where and when photos were taken and maybe even getting an unusual view of what they look like.

While the best way to view or manipulate image files on Linux is to open them on your desktop for viewing or manipulating with tools like Gimp, there are quite a few ways to get important details on the command line.

Identifying image type by file extension

In general, image files can be identified on the command line by listing their names. Clearly “.jpg” represents a jpeg file, “.png” a portable network graphics file, “.gif” a graphics interchange format file, “.tiff” a tagged image file and so on.

$ ls -l images 
-rw-rw-r--. 1 shs shs  256093 Jul 15  2018 mycats.jpg
-rw-r-----. 1 shs shs  784238 Jul 15  2018 mycats.png
-rw-rw-r--. 1 shs shs    6760 Jul 15  2018 arrow.jpg
-rw-r-----. 1 shs shs    8853 Jul 15  2018 arrow.png

Nearly all of the time you can rely on file extensions accurately reporting the file type, but there’s more you can do with additional commands.

Identifying images with the file command

The file command can identify image file types by extracting data from the files themselves.

$ file Emmy.jpg
Emmy.jpg: JPEG image data, progressive, precision 8, 704x584, components 3

The file below is clearly a jpg file in spite of its name. The output from the file command reports this.

$ file camper.png
camper.png: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, Exif Standard: [TIFF image data, little-endian, direntries=11, manufacturer=samsung, model=SM-G935V, orientation=upper-left, xresolution=164, yresolution=172, resolutionunit=2, software=GIMP 2.8.18, datetime=2018:04:30 07:56:54, GPS-Data], progressive, precision 8, 3465x2717, components 3

For most jpg files these days, the file command will show a lot more details as in the examples above and below. This output includes the file type, resolution details and quite a bit of information about where the image came from and when it was taken.

$ file tulip_poplar.jpg
tulip_poplar.jpg: JPEG image data, Exif standard: [TIFF image data, big-endian, direntries=11, yresolution=146, xresolution=154, model=SM-A526U, height=2448, manufacturer=samsung, software=A526USQS5CVC9, orientation=upper-left, datetime=2022:05:17 19:58:50, width=2448], baseline, precision 8, 2448x2448, components 3

As you can see from the output above, the tulip_poplar.jpg file was taken on May 17, 2022, with a Samsung cell phone,

Viewing image details using an octal dump

You can view some image details using the od -bc (octal dump) command. However, looking beyond the first couple of lines will also display portions of the image itself that won’t be helpful. The first part of the file contains “meta information” (data that describes the content rather than the image content itself). For example, the second line below identifies the file as a png file.

$ od -bc coconuts.png | head -4
0000000 211 120 116 107 015 012 032 012 000 000 000 015 111 110 104 122
        211   P   N   G  r  n 032  n  
sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.