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.

8
  • Will the latest version file always be the one that sorts last, or should file timestamps be used, or some other scheme? Commented Jul 19, 2018 at 8:55
  • @Kusalananda Yes, the latest version file will always be the one that sorts last. I mean the one that is created recently. Commented Jul 19, 2018 at 9:29
  • Sorry I should have been more specific. Sorts last, by name? ... taking into account that version_3 sorts after version_10. Commented Jul 19, 2018 at 9: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. Commented Jul 19, 2018 at 9:37
  • 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. Commented Jul 19, 2018 at 19:24

1 Answer 1

2

you can combine find and sort

find -path "some pattern" -printf "%T@ %P\n" | sort -n | tail -1 
9
  • Sorry. Should I be running the command like this? find -name "299" -printf "%T@ %P\n" | sort -n | tail -1 Commented Jul 19, 2018 at 10:49
  • possibly -path "*299*" it understands the normal shell wildcards Commented Jul 19, 2018 at 10:50
  • it turns out -name was wrong and you need -path th match on directory names. answer edited. Commented Jul 19, 2018 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. Commented Jul 19, 2018 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? Commented Jul 19, 2018 at 11:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.