Header Shadow Image

Screenshot: How to get a screenshot in linux using command line tools: xwininfo, convert.

Sometime back I was wondering how I can get a screenshot in Linux.  I've created this workaround for myself to use from the shell not knowing about the GIMP screenshot capture/aquire function at the time:

$ xwininfo|grep "Window id:"|awk '{ print $4 }'| awk '{ print "xwd -id "$1" -out screenshot.pcx" }'|sh;convert -colors 256 -dither screenshot.pcx screenshot.jpg;if [ -f `pwd`"/screenshot.jpg" ]; then echo "File saved to 'screenshot.jpg'"; fi
File saved to 'screenshot.jpg'


The great thing about this is that it gave me the option to possibly capture screenshots from a script, which could have a number of important uses.


The above is a one line command.  Paste it to a command prompt as you see it.  It uses 'convert' and 'xwininfo' utilities one being an image converter the other the X Windows Information utility respectively to determine which windows your cursor us over.  The third part is 'xwd' which is an X Windows image Dump utility.  When command is ran, your cursor should change to a '+' sign indicating that 'xwininfo' is ready to determine the windows you currently are over.  Click your mouse button over the window you need and next thing you'll have a screenshot of the window in the current folder.   'man' pages has more information on each of the commands so give them a try.  Also use '–help' with each one to tweak to your taste.

3 Responses to “Screenshot: How to get a screenshot in linux using command line tools: xwininfo, convert.”

  1. Very interesting. I like it.

    I was looking through it and looking at all of the commands used however and I’m not sure why you need to use xwininfo at all. xwd without the “-id” argument specified seems to give you the same + sign to choose the window and seems to produce the same results. Why can’t this just be shortened to:

    xwd -out hi; convert -colors 256 -dither screenshot.pcx screenshot.jpg;if [ -f `pwd`”/screenshot.jpg” ]; then echo “File saved to ’screenshot.jpg’”; fi

    That seems to work fine for me. Is there a reason why using xwininfo is better?

  2. oops. the “-out hi” should be “-out screenshot.pcx”. I was trying some examples and forgot to change that. So the command I was asking about was:

    xwd -out screenshot.pcx; convert -colors 256 -dither screenshot.pcx screenshot.jpg;if [ -f `pwd`”/screenshot.jpg” ]; then echo “File saved to ’screenshot.jpg’”; fi

  3. Tom Kacperski on April 11th, 2009 at 1:22 am

    Hey imchairmanm,

    Thanks for the followup. Appreciated.

    Yup. You certainly can. Can’t recall now why I threw in xwininfo (I’ve used this command for what seamed like decades). I suspect that either xwd didn’t return proper windows results (ie was getting the ID differently which didn’t work) or xwininfo was more reliable at the time (or simply threw in xwininfo as a reminder to myself on how to get window information – 😀 ). I don’t recall now.

    Anyway, you can definitely use xwd without xwininfo as you pointed out. 🙂


Leave a Reply

You must be logged in to post a comment.

  Copyright © 2003 - 2013 Tom Kacperski (microdevsys.com). All rights reserved.

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License