I have the following files.
root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver*
-rw-r--r-- 1 root root 0 Jul 5 18:54 /client/folder299/version_1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder300/ifolder1/version_b
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder301/ifolder2/version_c
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder302/ifolder3/version_d
-rw-r--r-- 1 root root 0 Jul 19 13:35 /client/folder300/version_2
-rw-r--r-- 1 root root 0 Jul 19 13:35 /client/folder301/version_3
-rw-r--r-- 1 root root 0 Jul 19 13:36 /client/folder302/version_4
I am trying to get the latest version file for a pattern matching an ID. Example is shown below.
root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* | grep 299
-rw-r--r-- 1 root root 0 Jul 5 18:54 /client/folder299/version_1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a
The latest version is version_a in the above example.
root@VMBOX:/client# ls -lrt /client/*/ver* /client/*/*/ver* | grep 299 | tail -1
-rw-r--r-- 1 root root 0 Jul 5 18:58 /client/ifolder299/ifolder/version_a
I am told that this approach is not good to find a file (Why *not* parse `ls`? )and am looking for an alternative way like https://stackoverflow.com/a/26766782/9316558. Please let me know if something is not clear.
Update:
From below answer by Jasen, I could get the latest file in the path /client
find /client -path "*299*" -printf "%T@ %P\n" | sort -n | tail -1
But, the above command gives the latest file. I am looking for finding the latest version file.
version_3
sorts afterversion_10
. – Kusalananda Jul 19 '18 at 09:30files=( /client/*299*/ver*(Nom) )
to get an array of files sorted in ascending order by modification time (o means sorting, m means modification time, N causes an empty array to be generated if no matching files exist). If you need descending order, use(^Nom)
instead. – user1934428 Jul 19 '18 at 09:37find
on Solaris. – Jeff Schaller Jul 19 '18 at 19:24