1

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.

Raj
  • 203
  • Will the latest version file always be the one that sorts last, or should file timestamps be used, or some other scheme? – Kusalananda Jul 19 '18 at 08:55
  • @Kusalananda Yes, the latest version file will always be the one that sorts last. I mean the one that is created recently. – Raj Jul 19 '18 at 09:29
  • Sorry I should have been more specific. Sorts last, by name? ... taking into account that version_3 sorts after version_10. – Kusalananda Jul 19 '18 at 09:30
  • @Raj: Does it have to be bash? If you would write your script in Zsh, you could do for instance a files=( /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:37
  • @Kusalananda The order in which I need the version file is sorting by time. For example, if version_a is created recently, I would need it. – Raj Jul 19 '18 at 09:40
  • @user1934428 I would like to use default bash for this operation as I am not authorized to use/install any external module. – Raj Jul 19 '18 at 09:41
  • 1
    Please re-tag your Question to Solaris, as it's apparent from comments that you're only connecting from Linux, then running find on Solaris. – Jeff Schaller Jul 19 '18 at 19:24
  • Does this solve your issue?: https://unix.stackexchange.com/questions/456957/how-may-i-find-the-most-recently-modified-files – Kusalananda Jul 24 '18 at 10:41

1 Answers1

2

you can combine find and sort

find -path "some pattern" -printf "%T@ %P\n" | sort -n | tail -1
Jasen
  • 3,761
  • Sorry. Should I be running the command like this? find -name "299" -printf "%T@ %P\n" | sort -n | tail -1 – Raj Jul 19 '18 at 10:49
  • possibly -path "*299*" it understands the normal shell wildcards – Jasen Jul 19 '18 at 10:50
  • it turns out -name was wrong and you need -path th match on directory names. answer edited. – Jasen Jul 19 '18 at 10:54
  • yeah, you can add a start location before -path if you want search other than the current directory, see the man page for find - there's many other options. – Jasen Jul 19 '18 at 11:16
  • Thanks for the update. The command seems to work if executed from inside the folder where I need this value from.
    `root@RV-VMBOX:/client# find -path "*299*" -printf "%T@ %P\n" | sort -n | tail -1`
    `1530797283.5560000000 ifolder299/ifolder/version_a`
    
    

    In my case, I need to execute this command remotely as a user of the system. So, I understand that this will search in the home directory of the above user, where this folder doesn't exist. Is there a way in which we can extend the -path's argument to be more precise with the path?

    – Raj Jul 19 '18 at 11:18
  • Thank you. This is working on linux systems. But, I need to execute this command remotely on a solaris OS unix system, where I am observing errors. I will try to look into man page. Thanks again.

    On Linux : root@RV-VMBOX:/# find /client -path "*299*" -printf "%T@ %P\n" | sort -n | tail -1 1530797283.5560000000 ifolder299/ifolder/version_a

    On Unix: root@solarisserver:~# find /client -path "*299*" -printf "%T@ %P\n" | sort -n | tail -1 find: bad option -path find: [-H | -L] path-list predicate-list

    – Raj Jul 19 '18 at 11:49
  • @Raj POSIX 6 find does not have a -path option. It was added in POSIX 7. Before POSIX 7, the -path option was a vendor-specific, non-portable extension. Which version of Solaris? Depending on the version, you might have GNU find as /usr/bin/gfind or /usr/sfw/bin/find`. – Andrew Henle Jul 21 '18 at 16:00
  • @AndrewHenle Thanks for the response. Solaris version - Solaris 11.3. I could learn that the gfind mimics the find behavior. – Raj Jul 23 '18 at 05:53
  • @Jasen I found that, if there is another file created after version file is updated, the above find command shows the other file. Could you please help me in narrowing it to find latest version file. – Raj Jul 23 '18 at 05:55