Help script find file most recent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help script find file most recent
# 1  
Old 03-28-2018
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
# 2  
Old 03-28-2018
Code:
ls -1t $(find /somedir -type f -name "$1") | head -2

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 03-28-2018
Quote:
Originally Posted by rdrtx1
Code:
ls -1t $(find /somedir -type f -name "$1") | head -2

Thanks!

But if I write
Code:
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.
# 4  
Old 03-28-2018
Then, specify your request correctly and detailedly, and post representative samples.
# 5  
Old 03-28-2018
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:

Code:
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:
Code:
$ 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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find most recent file and copy to another directory.

Very new to shell scripting. Not sure if my title is correct but I will try and explain. Directory has 100+ files with this format, " ABCD_ABC_Abc_AB0126.abc ". When a new file gets created, the 16-19 characters in the file name gets incremented by 1. Ex...todays most recent file is... (14 Replies)
Discussion started by: askvip
14 Replies

2. Solaris

Recent file available

Dear, Require a script to check : If the file under /opt/OV/log directory has recent 5 hours(or user defined value) file generated or not If generated then cmd to call : opcmsg a=a o=o msg_grp=OpC msg_text="Log file didn't generated on $time" s=critical (1 Reply)
Discussion started by: mjoshi87
1 Replies

3. Shell Programming and Scripting

How to find the recent file in many sub-directories?

Hi guys, Under my root directory there are many sub-directories which contains log file for every day of running. How can I find , in one command only, the recent log file in each sub-directory? For example, If I run the following: find . -name "exp_prod_*_*_yes_*_.log" -exec ls -ltr {} \;... (12 Replies)
Discussion started by: nir_s
12 Replies

4. UNIX for Dummies Questions & Answers

command to find most recent file

Hi, Here is my problem: I try to write a script to find data in a file named "data" for exemple. Let's say I am in the directory /x/y/z, there are several sub-directories in the "z" directory (each sub-directory has a file "data") and I am searching for the word "help". So I use this... (9 Replies)
Discussion started by: StephB
9 Replies

5. Shell Programming and Scripting

find the most recent file containing a certain string

I want to find the most recent file containing ' NORESETLOGS" I'm already here but, how to sort this now in a correct way ? By the way, my version of find does not know about 'fprint' find . -type f -exec grep -i " NORESETLOGS" {} \; -exec ls -l {} \; | grep -vi " RESETLOGS" (5 Replies)
Discussion started by: plelie2
5 Replies

6. Shell Programming and Scripting

Use cURL in shell script and get most recent file from remote server using SFTP

I have been trying to implement the following shell script -- sftp to remote server get list of files in the directory sftp get the most recent listed file exit This script will be scheduled to be executed everyday using CRON or CONTROL-M and the need is to have absolutely no human... (7 Replies)
Discussion started by: toobrown1
7 Replies

7. Shell Programming and Scripting

Find most recent files in dirs and tar them up?

Hey all.. This should be simple but stoopid here can't get head around it! I have many directories, say 100 each with many files inside. I need a script to traverse through the dirs, find most recent file in each dir and add it to a tar file. I can find the files with something like for... (1 Reply)
Discussion started by: bobdung
1 Replies

8. Shell Programming and Scripting

Getting the most recent file

Hi people, Please some help over here. I have logs in a directory, in which I need to get the most recent file in order to put it within other command. The format of the files are loadfiles20090308094339_41 loadfiles20090308094418_42 loadfiles20090308095457_43... (4 Replies)
Discussion started by: cgkmal
4 Replies

9. Shell Programming and Scripting

getting the most recent file

Hi, I have files coming in every day with that days timestamp like: nameyyyymmddhhmmss.ext. I need the most recent one and so i am using cat `ls -t name*|head -1 ` > temp i am sorting the files in the decending order and am copying the most recent one into a temp file. It works at times... (3 Replies)
Discussion started by: anujairaj
3 Replies

10. Shell Programming and Scripting

Most Recent File script

OK, I know next to nothing about scripting in unix, and at the moment I don't have access to a unix environment... We have an application that generates a text report file which is later printed. The format is this: bbtptcYYMMDDSSCC.txt (year/month/day/second/check digit) I want a... (1 Reply)
Discussion started by: josborn777
1 Replies
Login or Register to Ask a Question