Part 2, Simple graphical screen and window capture

Capturing screen shots from the command line


Content series:

This content is part # of # in the series: Capturing screen shots and program interaction on UNIX and Linux systems

Stay tuned for additional content in this series.

This content is part of the series:Capturing screen shots and program interaction on UNIX and Linux systems

Stay tuned for additional content in this series.

Why capture screens, windows, and dialogs?

Though many technical writers hate to admit it, a picture is often worth a thousand words. Most good technical documentation includes images, generically known as screen captures or screen shots, that show windows and dialogs as they appear in the application that is being documented. Including images in documentation makes users more comfortable with an application because such images show menus, dialogs, and general program interaction in context, so that users can see exactly what to do and what an application will look like while they are working with it.

Capturing such images is not just a writing task. Most marketing material for graphical applications includes screen shots of significant portions of an application's interface, or the application with complex work underway, to highlight the appearances of capabilities of an application. Anyone who has ever filed a defect report against a piece of software knows that a screen shot that actually shows the problem establishes more credibility than a written or verbal description. Highlighting a problem via a screen shot also makes it easier for developers to focus on the actual source of a problem.

UNIX and Linux screen capture overview

All modern Linux® and UNIX® systems provide some sort of graphical desktop, which usually includes at least one graphical screen capture utility—most often one that is specific to a particular desktop environment. For example, GNOME provides gnome-screenshot, and KDE provides KSnapshot. However, every Linux and UNIX system that runs the X Window System (in other words, all of them) includes a command-line utility, xwd, that makes it easy to capture an entire screen, a specific window, a window showing pull-down menus, and even specific dialogs.

The next sections of this article explore various ways of using the xwd tool to capture different types of images from a UNIX or Linux system's graphical display.

Introducing the xwd utility

The X Window Dump (xwd) utility has been part of the X Window System since the mid-1980s and the early days of Massachusetts Institute of Technology (MIT) Project Athena. It is still typically installed as a part of most X11 distributions. The xwd utility enables you to capture an entire screen (by capturing the X11 root window) or a specific window identified by using a mouse or a window ID on the command line. Images of a single window can consist of just that window or can also include menus and overlaid dialogs.

The simplest possible xwd command line looks like the following:

    xwd > filename.xwd

When executed, this command displays a set of crosshairs. You use the crosshairs to select the window that you want to capture an image of by right-clicking on that window. The command then writes its output to the filename.xwd file using shell redirections. The xwd command also provides a command-line option, -out, that eliminates the need for shell redirection by enabling you to specify the name of an output file on the command line, as shown below:

    xwd -out filename.xwd

However you specify the output file, the output of the xwd command is a window image in a generic format (known as xwd format), which you can easily convert to more standard graphical formats (such as, portable network graphics (PNG); joint photographic experts group (JPEG); or graphics interchange format (GIF)) using easily-obtained utilities (as discussed in the next section).

Converting xwd images to more popular formats

Several image processing and conversion toolkits are available for almost all Linux and UNIX systems. The most common of these are the ImageMagick and Netpbm toolkits (see Related topics for more information). Of these, the most widely used is probably the ImageMagick toolkit, which is included on vendor distributions of Linux tools for various UNIX systems (such as, IBM AIX Toolbox for Linux Applications CD).

The ImageMagick toolkit contains a number of different utilities for graphics conversion and manipulation. It includes a utility (import) that provides a command-line alternative to the xwd utility. Of these utilities, the most important when working with xwd files is the convert utility, which enables you to convert images from one format to another. You can specify the format that you want to convert an image to by preceding the name of the output file with the abbreviation for an image format or by specifying the target image format as the file extension of your output file. For example, both the following commands produce an output file in PNG format:

    convert filename.xwd PNG:filename.image
    convert filename.xwd filename.png

The ability to specify the output format as a prefix is convenient if you do not want your output file to have a format-specific file extension. Figure 1 shows a screen capture of a simple GNOME terminal window that has been converted into PNG format.

Figure 1. Converted screen capture of a terminal window
Converted screen capture of a terminal window
Converted screen capture of a terminal window

Capturing entire screens

The previous example of using the xwd command required that you select a specific window interactively. In some cases, you may want to capture an entire screen to show the contents and placement of multiple application windows simultaneously. The xwd command provides the -root option to enable you to do this by capturing the X Window System root window, which is the full-display window atop all other X Window System windows and dialogs displayed. The following command captures the entire screen and writes it to the full-screen.xwd file:

    xwd -root -out full-screen.xwd

When used with the -root option, the xwd command is non-interactive because you are capturing everything. If you need to capture a specific window in a specific state, such as displaying a menu or some other interactive action, you can do this by delaying the time at which the root window is captured (giving you time to set up the screen in the way that you want). To do so, use the sleep command found on all UNIX and Linux systems, which simply waits for a specified number of seconds, as in the following example:

    sleep 15 ; xwd -root -out full-screen.xwd

This command sequence gives you 15 seconds to display whatever menus or dialogs you want to capture before executing the xwd command.

Capturing specific windows

When executed without arguments, the xwd command requires that you interactively select the window that you want to capture which can be a problem if you want specific menus to be displayed. In most cases, the action of selecting a window is a mouse event that cannot proceed when any other pending mouse events, such as selecting a menu item, are in progress. Trying to capture a window with a pending mouse event results in an error message, as in the following example:

    $ sleep 10 ; xwd -out window2.xwd
    xwd: error: Can't grab the mouse.

You can work around this problem by taking advantage of other command-line options for the xwd command. The first one to try is the -id option, which lets you specify the X Window System ID of the window that you want to capture and therefore does not require that you select a window. The easiest way to determine the X Window System ID of a window is to use the xwininfo command, which provides that information plus a huge amount of information about the location and appearance of a specific window. When you execute the xwininfo command, a set of crosshairs is displayed. Use the crosshairs to click on the window for which you want information. A sample xwininfo command and its output are shown below in Listing 1.

Listing 1. Abbreviated output from the xwininfo command
    $ xwininfo

    xwininfo: Please select the window about which you
              would like information by clicking the
	      mouse in that window.

    xwininfo: Window id: 0x2c435b4 (has no name)

    Absolute upper-left X:  1811
    Absolute upper-left Y:  99
    Relative upper-left X:  1811
    Relative upper-left Y:  99
    Corners:  +1811+99  -423+99  -423-116  +1811-116
    -geometry 1126x835+1811+99

Using the window ID from this information, you can combine imposing a delay via the sleep command and the xwd command's -id option to capture a window and any menus or other transient graphics by using a command such as the following:

    sleep 15 ; xwd -id 0x2c435b4 -out window-with-menu.xwd

Figure 2 shows a screen capture of the same window that was shown in Figure 1 but with a menu displayed.

Figure 2. Screen capture of a terminal window with menu
Screen capture of a terminal window with menu
Screen capture of a terminal window with menu

One side effect of identifying a window by ID is that a screen capture based on the window ID may not capture other windows, such as dialogs, that are overlaid on the specified window. All windows, even dialogs, have their own IDs, and you can only specify one ID at a time. Whether or not you capture overlying windows is dependent on the graphical environment that you are using. To work around potential problems capturing overlying windows, you can use the xwd command's -screen option, which specifies that the contents of a window are to be captured relative to the root window, as in the following example:

    sleep 15 ; xwd -id 0x2c435b4 -screen -out window-with-menu.xwd

The -screen option captures a window as it appears atop the root window and therefore includes any overlaid dialogs, in-progress menu actions, and so on.


The xwd command is available for every Linux and UNIX distribution and, as a command-line application, provides an easy, graphical user interface (GUI)-independent screen and window capture tool. Combining xwd with freely available graphics toolkits such as ImageMagick and Netpbm makes it easy to capture screen images on your platform of choice and deliver them in any graphics formats that you need for documentation, marketing, bug reporting, and other purposes where a picture may indeed save you a thousand words of description.

The command-line screen and window capture tool discussed in this article are both easy to use and available on all Linux and UNIX systems, but different Linux and UNIX systems also offer a variety of graphical tools to further simplify capturing screen and window images. The next article in this series discusses the graphical screen capture tools that are available on different Linux and UNIX systems.

Downloadable resources

Related topics

  • Browse the technology bookstore for books on this and other technical topics.
  • The ImageMagick Toolkit provides a popular set of command-line tools for graphics capture, conversion, and manipulation.
  • The Netpbm toolkit provides another popular set of command-line tools for graphics capture, conversion, and manipulation.


Sign in or register to add and subscribe to comments.

Zone=AIX and UNIX, Linux
ArticleTitle=Capturing screen shots and program interaction on UNIX and Linux systems: Part 2, Simple graphical screen and window capture