Help script find file most recent

Hi,

I need to find the most recent files by their name from an X repertoire.
The problem is that the name of the files is of type:

POWERL10_20151203000.xml
POWERL10_20151203001.xml
POWERL10_20151202000.xml

FIXED VALUE_DATENNN.xml

NNN = Sequential number

I would need to recover the first two files in the example.

He tried to use find:

find / home / Rec12 -name $ 1 -type f | queue -1 >> test.txt

I don't get resultat.

Could anyone help me, please? Thank you

ls -1t $(find /somedir -type f -name "$1") | head -2
1 Like

Thanks!

But if I write

head -2

, only I will have the last two values. In the repertoire there are thousands of files every day.
I wanted to give an example of the files.

Then, specify your request correctly and detailedly, and post representative samples.

You don't mention what OS you are using this solution uses GNU find so that we can match files with version info within their name:

look=${1:-[A-Z]*}
find /home/rec12 -type f -regextype posix-extended -regex ".*/${look}_[0-9]{9,}$" -print | awk -F / '
{ fn=$NF
  split(fn, a, "_")
  fval=a[2]
  fname[a[1]]

  if (fv[a[1], 1] < fval) {
      fv[a[1], 2] = fv[a[1], 1]
      fp[a[1], 2] = fp[a[1], 1]
      fv[a[1], 1] = fval
      fp[a[1], 1] = $0
  } else if (fv[a[1], 2] < fval) {
      fv[a[1], 2] = fval
      fp[a[1], 2] = $0
  }
}
END {
    for (fd in fname) {
       print fp[fd,1] 
       if(fd SUBSEP 2 in fp) print fp[fd,2]
    }
}'

It picks the most recent (via date and revision #) version of the passed file regardless of the folder it is contained in.

Example:

$ mkdir -p {a,b,c}/{d,e,f}
$ touch /home/rec12/{a,b,c}/{d,e,f}/{TRIAL,DUMMY}_20180329{0,1,2}
$ touch /home/rec12/c/d/TRIAL_201803299
$ touch /home/rec12/a/e/TRIAL_201803297

$ ./verita TRIAL
/home/rec12/c/d/TRIAL_201803299
/home/rec12/a/e/TRIAL_201803297