Get display resolution from the command line for Linux Desktop

71,549

Solution 1

Use the command xrandr. Without any argument it displays the available resolutions and the current one (with an asterisk), for instance:

$ xrandr | fgrep '*'

Solution 2

Alternative solution: xdpyinfo | grep dimensions. xdpyinfo is older than xrandr, so might be more portable if you happen to use a very old distribution or some different X server.

Solution 3

You can get the horizontal and vertical resolutions using the following command:

xdpyinfo | grep dimensions | awk '{print $2}' | awk -Fx '{print $1, $2}'

or, in more compact form (as suggested by Peter.O in this comment):

xdpyinfo | awk -F'[ x]+' '/dimensions:/{print $3, $4}' 

For exmaple, on a 1600x900 display this will produce the following output:

1600 900

You can then place the values into separate variables using the command:

read RES_X RES_Y <<<$(xdpyinfo | awk -F'[ x]+' '/dimensions:/{print $3, $4}')

Display the values of the above variables using the command:

echo $RES_X, $RES_Y

On a 1600x900 display, the output is:

1600, 900

Solution 4

I should have looked a little harder before posting. xrandr will echo the current display settings, if not given any other arguments.

By default, this will dump all possible display settings, this can be filtered as follows:

xrandr | egrep '^[^ ]|[0-9]\*\+'

Solution 5

Clean xrandr output for imagemagick use

xrandr |awk '/\*/ {print $1}'

The /\*/ searches for the line containing an asterisk *.

Share:
71,549

Related videos on Youtube

Barton Chittenden
Author by

Barton Chittenden

Juggler, Bash jockey, Perl scripter, Linuxhead, code janitor.

Updated on September 18, 2022

Comments

  • Barton Chittenden
    Barton Chittenden over 1 year

    I'm looking for a method of reporting display resolution. I want to set up scripts to launch rdesktop, and I want to launch it on several machines with different resolutions, so I want a way to dynamically determine it.

  • Ehtesh Choudhury
    Ehtesh Choudhury about 11 years
    Could you append some sample output to the answer? And I'm guessing xrandr |g grep \* does the trick?
  • Peter.O
    Peter.O about 9 years
    xdpyinfo | awk -F'[ x]+' '/dimensions:/{print $3, $4}'
  • Stéphane Gourichon
    Stéphane Gourichon about 9 years
    Indeed, but some (all ?) multi-monitor setups appear as one screen in xdpyinfo while xrandr correctly enumerates screens and their resolution. This may or may not be a problem depending on the context.
  • Stéphane Gourichon
    Stéphane Gourichon about 9 years
    Indeed, but some (all ?) multi-monitor setups appear as one screen in xdpyinfo while xrandr correctly enumerates screens and their resolution. This may or may not be a problem depending on the context.
  • Ruslan
    Ruslan over 5 years
    Note: while xdpyinfo seems to report correct dimensions in pixels, it reports wrong resolution (DPI). E.g. on my monitor with dimesions 3840x2160 (native and actual) and size 708mm x 399mm as reported by xrandr, xdpyinfo says that resolution is 96x96 dots per inch.
  • Pi Delport
    Pi Delport over 3 years
    To search with awk directly, instead of grep: xrandr | awk '/\*/ {print $1}'