[SOLVED] Get latest file from directory

Hi,

In a rule i need (from a directory of *.avi and *.jpg files) to get the name of the latest jpg file.

Is anyone who can give me some hints?

-ben

Google is your friend:
For linux:
To get the Most recent file in another directory , for example in /opt/spark/ :

ls /opt/spark/ -Art | tail -n 1
To filer the jpg add a grep:
ls /opt/spark/ -Art | grep .jpg | tail -n 1

So in a rule:

var String fileName = executeCommandLine("ls /opt/spark/ -Art | grep .jpg | tail -n 1", 500) //Give 500ms to return a query

Hi,

Thanks for the info, i tried to make a rule in java but this looks much easier …
I used your code but it doesn’t work, oh is complaining that directory does not exists etc while it does not.
rule:

    var String PathToImage = "/backup/camera/frontdoor/" + String::format("%1$tY-%1$tm-%1$te", new java.util.Date) + "/"
    //var String CurrentDate = String::format("%1$ty-%1$tm-%1$te", new java.util.Date)
    logInfo ("path to file", PathToImage)
    var String cmd = "ls -Art " + PathToImage + " | grep .jpg | tail -n 1"
    logInfo ("cmd", cmd)
    var String fileName = executeCommandLine(cmd, 1500) //Give 500ms to return a query
    logInfo ("file ", fileName)

oh log:

2019-01-23 14:08:55.327 [INFO ] [.smarthome.model.script.path to file] - /backup/camera/frontdoor/2019-01-23/
2019-01-23 14:08:55.339 [INFO ] [g.eclipse.smarthome.model.script.cmd] - ls -Art /backup/camera/frontdoor/2019-01-23/ | grep .jpg | tail -n 1
2019-01-23 14:08:55.399 [INFO ] [eclipse.smarthome.model.script.file ] - ls: cannot access |: No such file or directory
ls: cannot access grep: No such file or directory
ls: cannot access .jpg: No such file or directory
ls: cannot access |: No such file or directory
ls: cannot access tail: No such file or directory
ls: cannot access 1: No such file or directory
/backup/camera/frontdoor/2019-01-23/:
total 6532
-rwxrwxrwx 1 0 0 1601766 Jan 23 07:14 07-14-11.avi
-rwxrwxrwx 1 0 0  310952 Jan 23 07:14 07-14-19.jpg
-rwxrwxrwx 1 0 0   15364 Jan 23 07:14 07-14-11.avi.thumb
-rwxrwxrwx 1 0 0 1282312 Jan 23 07:15 07-15-10.avi
-rwxrwxrwx 1 0 0  290069 Jan 23 07:15 07-15-11.jpg
-rwxrwxrwx 1 0 0   14493 Jan 23 07:15 07-15-10.avi.thumb
-rwxrwxrwx 1 0 0 2800402 Jan 23 13:13 13-13-07.avi
-rwxrwxrwx 1 0 0  311981 Jan 23 13:13 13-13-10.jpg
-rwxrwxrwx 1 0 0   43623 Jan 23 13:13 13-13-07.avi.thumb

If I run the concatened cmd “ls -Art /backup/camera/frontdoor/2019-01-23/ | grep .jpg | tail -n 1” in a shell, it returns the correct file, so the command is working!

-ben

    var String cmd = "ls " + PathToImage + "-Art | grep .jpg | tail -n 1"

Changed the code but same result :

2019-01-23 15:05:15.885 [INFO ] [.smarthome.model.script.path to file] - /backup/camera/frontdoor/2019-01-23/
2019-01-23 15:05:15.893 [INFO ] [g.eclipse.smarthome.model.script.cmd] - ls /backup/camera/frontdoor/2019-01-23/ -Art | grep .jpg | tail -n 1
2019-01-23 15:05:15.952 [INFO ] [eclipse.smarthome.model.script.file ] - /backup/camera/frontdoor/2019-01-23/:
total 6532
-rwxrwxrwx 1 0 0 1601766 Jan 23 07:14 07-14-11.avi
-rwxrwxrwx 1 0 0  310952 Jan 23 07:14 07-14-19.jpg
-rwxrwxrwx 1 0 0   15364 Jan 23 07:14 07-14-11.avi.thumb
-rwxrwxrwx 1 0 0 1282312 Jan 23 07:15 07-15-10.avi
-rwxrwxrwx 1 0 0  290069 Jan 23 07:15 07-15-11.jpg
-rwxrwxrwx 1 0 0   14493 Jan 23 07:15 07-15-10.avi.thumb
-rwxrwxrwx 1 0 0 2800402 Jan 23 13:13 13-13-07.avi
-rwxrwxrwx 1 0 0  311981 Jan 23 13:13 13-13-10.jpg
-rwxrwxrwx 1 0 0   43623 Jan 23 13:13 13-13-07.avi.thumb
ls: cannot access |: No such file or directory
ls: cannot access grep: No such file or directory
ls: cannot access .jpg: No such file or directory
ls: cannot access |: No such file or directory
ls: cannot access tail: No such file or directory
ls: cannot access 1: No such file or directory

-ben

remove the / at the end:

    var String PathToImage = "/backup/camera/frontdoor/" + String::format("%1$tY-%1$tm-%1$te", new java.util.Date)

Same error … :frowning:
result from cli:

[16:32:31] openhabian@openhabianpi:~$ ls /backup/camera/frontdoor/2019-01-23 -Art | grep .jpg | tail -n 1
16-12-01.jpg

Fromthe docs: https://www.openhab.org/docs/configuration/actions.html#exec-actions

Note: The commandLine variable often has to use a special format where @@ needs to be used in place of spaces. For example the bash command touch somefile will have to be written as touch@@somefile

So change your code to:

    var String PathToImage = "/backup/camera/frontdoor/" + String::format("%1$tY-%1$tm-%1$te", new java.util.Date)
    //var String CurrentDate = String::format("%1$ty-%1$tm-%1$te", new java.util.Date)
    logInfo ("path to file", PathToImage)
    var String cmd = "ls@@" + PathToImage + "-Art@@|@@grep@@.jpg@@|@@tail@@-n@@1"
    logInfo ("cmd", cmd)
    var String fileName = executeCommandLine(cmd, 1500) //Give 500ms to return a query
    logInfo ("file ", fileName)

Hi,

Also adding the @ did not work, I changed my program and called a bash script from OH.
This now works fine, thanks for all your suggestions.

PS. Somehow it looked like the exec did not liked the ‘|’ command.