Counting number of layers returned by g.list in GRASS GIS

Author: Paulo van Breugel
Updated on: 2015-09-24

I am running a script that is producing a very large number of layers. As it takes a while, I wanted to find out how many layers were already created using the command line. Trivial thing perhaps, but I wasn’t really sure how to do it. After a bit of search, one way I found uses the versatile awk command line tool.

In the code below the g.list function is used to list all layers in the mapset VECEA. This is passed to (piped) as an array to awk, which splits the array and counts the elements.

echo `g.list type=raster pattern=* mapset=VECE sep=,` | awk '{n=split($0, array, ",")} END{print n}'

And the following will give the number of layers which name start with maxent in the mapset VECEA.

echo `g.list type=raster pattern=maxent* mapset=VECEA sep=,` | awk '{n=split($0, array, ",")} END{print n}'

There are other, possibly better, ways, but this is quick and easy enough for my purposes.


Software: @grassgis @awk
Tools: @g_list

If you have questions

If you have questions or comments about the text, let me know. You can use this contact form. Please make sure to include the page title ("Counting number of layers returned by g.list in GRASS GIS") or page name ("glist-count-layers").