How do I find out my screen resolution from a shell script?
9 Answers
xdpyinfo | grep dimensions | sed -r 's/^[^0-9]*([0-9]+x[0-9]+).*$/\1/'
Command xdpyinfo displays various information about your X server. It writes a lot of things to the standard output but we only need the line starting with the word dimensions, thus we use grep. Finally we use sed to clean the result.
xdpyinfo | grep dimensions will give you the total resolution, if you have multiple monitors it will be the sum of all of them. xrandr --current will give you the resolution for each monitor.
I use this snippet to find the maximum possible resolution for rDesktop without going to full screen:
Xaxis=$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f1)
Yaxis=$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f2)
Output:
Xaxis = 1280
Yaxis = 1024
Minus windows decoration (more or less):
MaxRes=$(($Xaxis-5))"x"$(($Yaxis-25))
Output:
MaxRes = 1275x999
Which is the max resolution for rDesktop without going full screen.
End command:
rdesktop -u $User -P -z -5 -g $MaxRes $Host &
It works fine so far but I haven't tested thoroughly though.
Another example is for screencast with avconv:
avconv -f x11grab -r 15 -s `xrandr --current | grep '*' | uniq | awk '{print $1}'` -i :0.0 -c:v libx264 ./output.mp4
- 140
A very simple method is to read out the modes file in the sys-directory:
cat /sys/class/graphics/*/modes
or respectively
cat /sys/class/graphics/*/virtual_size
- 558
- 9
- 9
Reading the Monitor Screen Data
The vesa standard provides a method of how to read the monitor screen resolution.
Extended Display Identification Data (EDID): This standard defines data formats to carry configuration information, allowing optimum use of displays.
A monitor typically supports multiple resolutions and refreshrates. Of course someone will prefer the maximum (physical) one.
To read this monitor data, try one of these solutions:
edid-decode
If not installed, type
sudo apt install edid-decodeThen read the
edidfileedid-decode /sys/class/drm/card0-eDP-1/edidread-edid
Install with
sudo apt install read-edidThen read via i2c the screen monitor data and parse it
sudo get-edid | parse-edidHexdump the edid data
In case edid-tools are not installed, you can dump the
edidhex-file, e.g.:hd /sys/class/drm/card0-eDP-1/edidTo encrypt this hex file take a look at wiki or download the edid specifications.
- 558
- 9
- 9
#############################################
## I use this with a Video Recording Program.
# window size --root option - information on the screen's root window
echo $(xwininfo -root | grep 'geometry' | awk '{print $2;}')
# output(s): 1024x768+0+0
# height x width + x + y positions.
######################
## Reference Manual ##
man xwininfo
- 121
Two possible alternatives produced combining the answers of @user31752 and @eliezer-e-vargas
A simpler regex:
$ xrandr --current | sed -n 's/.* connected \([0-9]*\)x\([0-9]*\)+.*/\1x\2/p'
1440x900
or using cut:
$ xrandr --current | grep ' connected ' | cut -d ' ' -f 3 | cut -d '+' -f 1
1440x900
The use of grep '*' | uniq from @eliezer-e-vargas get a different line (ex. " 1440x900 59.90*+ 59.89" ) of xrandr output, while the grep ' connected ' get a simple one (ex. "LVDS1 connected 1440x900+0+0 .....").
The use of regex by @user31752 is nice, so the line that I'm using needs a simpler regex, or can be substituted whit the simpler cut command.
Example xrandr output
$ xrandr --current
Screen 0: minimum 320 x 200, current 1440 x 900, maximum 8192 x 8192
LVDS1 connected 1440x900+0+0 (normal left inverted right x axis y axis) 331mm x 207mm
1440x900 59.90*+ 59.89
1360x768 59.80 59.96
1152x864 60.00
1024x768 60.00
800x600 60.32 56.25
640x480 59.94
VGA1 disconnected (normal left inverted right x axis y axis)
HDMI1 disconnected (normal left inverted right x axis y axis)
DP1 disconnected (normal left inverted right x axis y axis)
- 481
xdpyinfo will do it, with some parsing. It gives a lot of info which you'll then have to dig the screen number, and dimensions from
- 32,350
As in the accepted answer but less complicated:
xdpyinfo | grep dimensions
Example of output:
dimensions: 1366x768 pixels (361x203 millimeters)
- 377