Score:0

How to use watch to return latest picture file and open that picture?

in flag

I am trying to use the watch command to see the latest file. It is showing the file but how do I switch to the latest one when an even newer file is created?

The files are pictures so I am opening them with an image viewer

here's what I tried

watch -n 0.1 ls /home/titan/mnt

but it doesn't work because I just need one file and basically the lastest file and store that file name and open it with an image viewer.

Raffa avatar
jp flag
Try `ls -tp /home/titan/mnt | grep -v / | head -1 | xargs -I {} eog /home/titan/mnt/{}` ... But why use `watch`?
terdon avatar
cn flag
@Raffa that will fail for even slightly strage file names (spaces in the name, newlines, glob characters) but even if that's not the issue, why `xargs`? All you want with your approach is `eog $(ls -tp /home/titan/mnt | grep -v / | head -1)`.
Raffa avatar
jp flag
@terdon That was never meant to be a solution and you are right ... it will fail at some point ... and yes command substitution can do the same job if you add quotes around it for some white space tolerance and add the path before it so the file can be opened i.e. `eog /home/titan/mnt/"$(ls -tp /home/titan/mnt | grep -v / | head -1)"`.
Score:2
jp flag

It is not clear to me why you want to use the watch command. Also ls is not the best choice here for many reasons.

If all you want is to watch for when a new file is created in /home/titan/mnt and open that file in an image viewer, then install inotify-tools like so:

sudo apt install inotify-tools

and use it in a bash script like so:

#!/bin/bash
path_to_directory="/home/titan/mnt/"

inotifywait -m "$path_to_directory" -e create |
while IFS=' ' read path action file
    do
    # You can use other image viewers than eog if you want
    eog "$path$file" &
    done
answerSeeker avatar
in flag
I tried with inotify wait but it's unfortunately not detecting changes. If you do a cp file.jpg to ~/mnt it won't notice it
Raffa avatar
jp flag
@answerSeeker It should detect it ... Did you install `inotify-tools` first ... How did you run the script? You need to save it to a file copy/paste and then run the file with `bash filename` ... It could be that you need to change the image viewer application ... To debug, add `echo "$path$file"` one line above `eog "$path$file" &` and see what gets printed when you copy a file to `/home/titan/mnt/`
answerSeeker avatar
in flag
Yes it works if I paste an image in there, but if another application inserts an image, it doesn't work.
Raffa avatar
jp flag
@answerSeeker That might be a permission issue ... check that the image inserted by the other application is readable by the user executing the script.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.