How do I check if all the NoData Values of my rasters (.tif) are the same in a directory in a python script/command line/bash using ubuntu?

I am using Ubuntu 16.4 LTS and using the Terminal Command Line to look at and edit rasters.

These rasters will be processed and the area with values will eventually be polgonized, the NoData values will need to be the same therefore I need to check them.

Also my NoData Values for each individual tif files are not showing up as a raster band, they are embedded in the file itself;


  • Are you doing this in python? C#? C++? or do you just want to get a report that contains the NoData value to skim? What operating system do you have? Windows? Linux? Mac? – Michael Stimson Mar 12 '18 at 22:17
  • Apologies, ubuntu command line terminal. Want a summary of all nodata values – Rose Mar 12 '18 at 23:03
  • 1
    Can you script in python? The object you're after is a raster band, from Dataset.GetRasterBand(1).GetNoDataValue()... getting this value from the command line would be painful without python, you'd need to call gdalinfo for each raster, pipe the text to a file and review the file manually. – Michael Stimson Mar 12 '18 at 23:11
  • Thank you for your last message, I'm just started to learn py from no coding experience will keep that in mind. I checked if the no data values were showing up as a raster band in QGIS, and it is not. Will add picture. Is it possible you could point me in the right direction how to write this out in py? – Rose Mar 12 '18 at 23:22

If you're on Linux or Mac, Python is not needed.
The following bash script will do it:

for f in *.tif; do
    gdalinfo "$f" | grep -o 'NoData Value\=[-0-9]*' || echo "NoData Value=None";
done | uniq

This outputs one line for each unique NoData Value found in *.tif.

  • That's really good to know and probably works just as stated on a Linux system (+1 for that) but really doesn't help other users on Mac and plain old Windows... For those of us that don't have Linux (or Unix) python for this task is not overkill! One of the really good things about python is that it is generally platform independent; I can test a script on Windows and am reasonably confident that it will work the same on a different OS. – Michael Stimson Mar 13 '18 at 11:45
  • 1
    This script should also work on Mac. For Windows you might be right. But OP says he uses Ubuntu. I removed the "overkill" sentence. Sorry, I'm living in a very Linux-centric world ;-) – pLumo Mar 13 '18 at 12:01
  • It's easy to fall into the everyone is just like me trap, I've done the same many times myself; I know literally 0 about bash, so I'd go for a python solution - horses for courses I guess. You're right, as the OP specifically states Linux your answer for this question is quite suitable and is probably better than mine so I upvoted it. – Michael Stimson Mar 13 '18 at 12:34
  • r@Thin:~/Desktop/GreatExtentDEM/0900Rich$ for f in *.tif; do > gdalinfo "$f" | grep -o 'NoData Value\=[-0-9]*' || echo "NoData Value=None"; > done | uniq NoData Value=-100 NoData Value=-32767 NoData Value=-100 rose@Thinkpad:~/Desktop/GreatExtentDEM/0900Rich$ This is my output for running this for loop in a directory. Do you happen to know why @RoVo it is showing the same no data value in the third output? Is there any way I could check if there are spaces that I cannot see? I tried running a simple for loop with just gdalinfo and grep -o 'NoData:' but it just came up with NoData: – Rose Mar 29 '18 at 0:21
  • Thats weird. Try sort -u instead of uniq – pLumo Mar 29 '18 at 5:43

There's only a few basic things to do:

  1. Iterate: I'm using os.walk which will list all the files in the nominated folder and all subfolders, checking the extension of any files located to ensure that the script only tries to open images. You could use os.listdir() if you don't want to find all the files down the folder tree.
  2. Open the dataset: using gdal.Open or gdal.OpenShared and get the first band (index is 1 as bands are 1 based).
  3. Get the NoData value as an object and print it.

Before trying to run this please ensure that GDAL is installed with python bindings or you will get an error on line 2.

import os, sys
from osgeo import gdal # import the GDAL object, please ensure this is installed in your python

AllowedImageExtensions = ['.TIF','.IMG'] # Add new image extensions here in UPPER CASE

BaseFolder = sys.argv[1] # the first argument, if you like replace with a defined path.

for (curPath,Folders,Files) in os.walk(BaseFolder):
    for thisFile in Files:
        bName, bExt = os.path.splitext(thisFile) # break the file name into 'name', '.ext'
        if bExt.upper() in AllowedImageExtensions:
            # file has the right extension to be a raster
            ds  = gdal.Open(os.path.join(curPath,thisFile)) # open the dataset with the full path
            bnd = ds.GetRasterBand(1) # get the first band
            NoDat = bnd.GetNoDataValue()
            print( "{} :: {}".format( os.path.join(curPath,thisFile),NoDat))

If your QGIS has a python window you should be able to run from there but replace the line BaseFolder = sys.argv[1] with BaseFolder = '/your/path'.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.