Querying framebuffer resolution in Linux

Problem:

You want to query the current resolution of a screen connected as framebuffer device (e.g. /dev/fb0).

Solution

The /sys virtual filesystem contains all information about a framebuffer device in /sys/class/graphics/<devicename>.

In order to get the size, use

cat /sys/class/graphics/fb0/virtual_size

replacing fb0 with the name of the framebuffer device to query. This will print a line like

1280,1024

In order to get the width/height as number, you can use these commands:

cat /sys/class/graphics/fb0/virtual_size | cut -d, -f1 # Get width
cat /sys/class/graphics/fb0/virtual_size | cut -d, -f2 # Get height