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.

HOW DOES IT WORK?

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.

Subscribe
Notify of
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
imchairmanm
imchairmanm
17 years ago

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?

imchairmanm
imchairmanm
17 years ago

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


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

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

 

3
0
Would love your thoughts, please comment.x
()
x
The IT Development and Technology Mini Vault | MicroDevSys.com
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.