Take screenshot from command line on an X server

import is part of ImageMagick package.

#import the "root" window - that is, the entire screen.
import -window root filename.png
 
import -window root screenshot-$(date +%Y-%m-%d_%k.%M.%S).png

Note: Import will not work on MS Windows.