Loading...

Lab 93: Opening Image Files from the Command Line

Open image artifacts from the CLI using a lightweight viewer or the system default opener. Validate file presence and choose the right approach for graphical versus headless environments.

cli desktop tools

Scenario

You are working a ticket that includes screenshots and diagrams stored on disk. You need to confirm the files are present, open them from the shell, and avoid guesswork about which application should handle the file type.

Operator context

Your approach depends on environment. On a headless server, GUI viewers are not useful; you either transfer the files off box or use a terminal-capable preview tool when available.

Objective

  • Verify image files exist from the shell.
  • Install a lightweight GUI image viewer ( feh ).
  • Open images directly with the viewer.
  • Optionally install a terminal preview tool ( viu ).
  • Open images using the system default with xdg-open .

Concepts

  • File presence checks: using globs to quickly list candidate files ( *.png , *.jpg ).
  • Graphical versus headless constraints: GUI viewers require a running graphical session and display.
  • Default application delegation: using xdg-open to hand off to the desktop’s configured default.
  • Terminal previews: tools like viu can provide quick inspection when you cannot or do not want to launch a GUI.

Walkthrough

Step 1 : Confirm the image file exists.
Commands
ls *.jpg
# OR
ls *.png
# OR
ls *.jpeg

This is the fastest “is it here?” check. If the glob matches nothing, your shell may print an error or return a non-zero status depending on settings.

# Expected pattern:
diagram.png
screenshot-01.jpg
Step 2 : Install a lightweight GUI viewer.
Command
sudo apt install feh -y
# OR
sudo dnf install feh -y
# OR
sudo pacman -S feh

feh is a common lightweight viewer for X11 environments. If you do not have a graphical session, this step will not be useful.

# Expected pattern:
feh installs successfully (or reports already installed).
Step 3 : Open an image with feh.
Commands
feh image.jpg
# OR
feh image.png

Use the explicit filename you identified in Step 1. This opens a window and renders the image in your GUI session.

# Expected pattern:
feh opens a window and displays the image.
Step 4 : Install a terminal-based preview tool (optional).
Commands
sudo apt install viu -y
# OR
sudo pacman -S viu
# OR
cargo install viu

viu renders images as ANSI and Unicode graphics in the terminal. Output quality depends on your terminal and font support.

# Expected pattern:
viu installs successfully (or cargo compiles it).
Step 5 : Preview an image inline in the terminal.
Commands
viu image.jpg
# OR
viu image.png

If you are in a TTY or a limited terminal, you may get a poor result (or no useful output). In a modern terminal emulator, it typically renders well enough for a quick preview.

# Expected pattern:
a terminal-rendered preview of the image (quality varies by terminal).
Step 6 : Open the image using the system default app.
Commands
xdg-open image.jpg
# OR
xdg-open image.png

xdg-open delegates to whatever your desktop environment has set as the default image viewer.

# Expected pattern:
the image opens in your default GUI application.

Common breakpoints

No graphical session available

If you are on a headless host, GUI viewers like feh will fail. Transfer the file off box or use a terminal preview tool when available.

xdg-open fails or does nothing

xdg-open relies on desktop integration and MIME associations. Verify you are in a desktop session and that a default image viewer is configured for the file type.

Terminal preview output is unreadable

Tools like viu depend on terminal emulator capabilities and font rendering. If output is unusable, use a GUI viewer or open the file on another system.

Globs do not match any files

Confirm you are in the expected directory and verify filenames. If the extension differs, list with a broader pattern or use ls on the directory to locate the asset.

Cleanup checklist

This lab is read-only aside from installing packages. If you installed optional tooling for testing, remove it when you no longer need it.

Commands
# Debian/Ubuntu
sudo apt remove -y feh viu

# RHEL/Fedora
sudo dnf remove -y feh

# Arch
sudo pacman -Rns feh viu

Reference

  • ls *.png : Lists files matching an image extension glob.
    • *.png : Shell glob that matches files ending in .png .
  • feh <file> : Opens an image in a lightweight GUI viewer.
  • viu <file> : Renders an image as ANSI and Unicode graphics in the terminal.
  • xdg-open <file> : Opens a file with the system default application.