I want to start a scan from a terminal and view the image automatically upon completion. How can I do this without knowing the saved filename? I am currently running Ubuntu 16.04 LTS locally on my notebook, which is connected via USB to my scanner.

I wrote the following terminal command; however, the problem is that if more than one minute lapses during the scanning process then Eye of Gnome cannot find the image file, and I receive an error message:

scanimage --format=tiff > ~/Pictures/$(date +%Y%m%d_%H%M).tiff && sleep 5 ; eog --fullscreen ~/Pictures/$(date +%Y%m%d_%H%M).tiff

For example, an image file will be saved as 20170128_2314.tiff, but Eye of Gnome or EOG will search for 20170128_2315.tiff resulting in an error. And depending on the scanner resolution setting, it may take 1-5 minutes to scan from start to finish.

The image files are currently being saved to my home directory ~/Pictures folder.

Any help or input would be greatly appreciated.


Open the latest file inside a directory with its default application

The script below will open the latest file inside your ~/Pictures directory. Independently to how the file is named, or when the script is run.

Of course you can set any other directory to use, or any application to open the file.

The script

#!/usr/bin/env python3
import subprocess
import os
from operator import itemgetter

dr = os.path.join(os.environ["HOME"], "Pictures")
files = [os.path.join(dr, f) for f in os.listdir(dr)]
latest = sorted([[f, os.path.getmtime(f)] for f in files], key=itemgetter(1))[-1][0]
subprocess.Popen(["xdg-open", latest])

To use

  1. Copy the script into an empty file, save it as open_latest.py
  2. Run it:

    python3 /path/to/open_latest.py

    and the latest file will be opened by its default application.


  • To use another directory, change the line

    dr = os.path.join(os.environ["HOME"], "Afbeeldingen")

    into e.g.

    dr = "/path/to/dir"

    but don't use paths like $HOME, which do not work in python

  • To use a specific application instead of the default one, change the line:

    subprocess.Popen(["xdg-open", latest])

    into e.g.

    subprocess.Popen(["eog", latest])


The script:

  • defines the Pictures directory inside your $HOME directory:

    dr = os.path.join(os.environ["HOME"], "Pictures")
  • lists the files:

    files = [os.path.join(dr, f) for f in os.listdir(dr)]
  • sorts the files by modification date (os.path.getmtime(f)), get the latest:

    latest = sorted([[f, os.path.getmtime(f)] for f in files], key=itemgetter(1))[-1][0]
  • open the file with its default application:

    subprocess.Popen(["eog", latest])
  • This method worked great too! Thank you very much for the detailed explanation and your help. :) – RPeezey Feb 2 '17 at 2:47

Why not save the filename to a variable before sending it to scanimage:

filename=~/Pictures/$(date +%Y%m%d_%H%M).tiff
scanimage --format=tiff > "$filename" && sleep 5
eog --fullscreen "$filename"

(I split it into multiple lines for readability, but you can combine the commands with ; as you were doing before.)

  • Thank you! This solution worked, but I get the following error in the terminal: (eog:2964): EOG-WARNING **: Failed to open file '/home/owner/.cache/thumbnails/normal/e0a8f3a3f62dbc085858e84574303ac2.png': No such file or directory – RPeezey Feb 2 '17 at 0:06
  • I re-ran the script and the error message did not appear after the next scan. The EOG error mysteriously appears and disappears from time to time in the terminal, but does not effect the functionality. – RPeezey Feb 2 '17 at 5:54
  • @RPeezey it's a warning, not an error. Run any GUI program on the command line you'll see dozens of errors and warnings. I never bother about them. – muru Feb 2 '17 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.