Needed shell script to get the latest file based on date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Needed shell script to get the latest file based on date
# 1  
Old 02-12-2013
Needed shell script to get the latest file based on date

hi i need the shell script to get the file with latest date.

for example in my input folder i have files like

file_20130212.txt
file_20130211.txt

now my output folder should have the file with latest date i.e..file_20120212.txt

i want to get the latest file .. i.e is should take the value 20130212 from the file and should convert it to 2013-02-12 and should check with the system date and should return the latest file based on the date format..

thanx in advance....

Last edited by hemanthsaikumar; 02-12-2013 at 02:51 AM.. Reason: i want to add new requirement for the above problem
# 2  
Old 02-12-2013
Since the date is in YYYYMMDD, a numeric sort would do the trick.

Try something like..

Code:
ls file_*.txt | sort -t"_" -nrk2.1,2.8 | head -1

# 3  
Old 02-12-2013
what is that -nrk2.1,2.8 in that above code..?
# 4  
Old 02-12-2013
Quote:
Originally Posted by hemanthsaikumar
what is that -nrk2.1,2.8 in that above code..?
Reverse numeric sort ( -nr) on char 1 to 8 ( 2.1,2.8) of second field (20130212.txt) while using "_" as the field separator ( -t"_")
This User Gave Thanks to clx For This Post:
# 5  
Old 02-12-2013
actually i got a change in my requirement can u solve the above problem..
# 6  
Old 02-13-2013
Since the format is YYYYMMDD, the alphabetic sort in ls will do it:
Code:
ls file_*.txt | tail -1

# 7  
Old 02-13-2013
Quote:
Originally Posted by hemanthsaikumar
actually i got a change in my requirement can u solve the above problem..
I am confused with the new requirement.
Do you want to get the latest file based on last modified/create date/time or the one which is present in the filename.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Run shell script based on date file

Hi Team, I have a to run a script based on a date present in a different file which updates everyday. Kindly help with the solution. My current execution : ksh scriptname.sh 10152019. But here i want to enter this date from a file which gets updated daily. My appraoch : date file location:... (3 Replies)
Discussion started by: midhun3108
3 Replies

2. Shell Programming and Scripting

Script to determine Date,TotalFile,total size of file based on date

I have file listed like below -rw-r--r--+ 1 test test 17M Nov 26 14:43 test1.gz -rw-r--r--+ 1 test test 0 Nov 26 14:44 test2.gz -rw-r--r--+ 1 test test 0 Nov 27 10:41 test3.gz -rw-r--r--+ 1 test test 244K Nov 27 10:41 test4.gz -rw-r--r--+ 1 test test 17M Nov 27 10:41 test5.gz I... (5 Replies)
Discussion started by: krish2014
5 Replies

3. Shell Programming and Scripting

Help with korn shell script to get the latest file versions in a directory

I want to write a korn shell script to get the latest three versions for a file in the directory having lot of files with various versions (files with prefix as same but time stamp as suffix) and compress it and at the same time have to remove the remaining versions of the file (other than latest... (4 Replies)
Discussion started by: maheshbabu
4 Replies

4. Shell Programming and Scripting

Find the latest file based on the date in the filename

Hi, We've a list of files that gets created on a weekly basis and it has got a date and time embedded to it. Below are the examples. I want to find out how to get the latest files get the date and time stamp out of it. Files are PQR123.PLL.M989898.201308012254.gpg... (1 Reply)
Discussion started by: rudoraj
1 Replies

5. Shell Programming and Scripting

Shell script to get the latest file from the file list and move

Hi, Anybody help me to write a Shell Script Get the latest file from the file list based on created and then move to the target directory. Tried with the following script: got error. A=$(ls -1dt $(find "cveit/local_ftp/reflash-parts" -type f -daystart -mtime -$dateoffset) | head... (2 Replies)
Discussion started by: saravan_an
2 Replies

6. Shell Programming and Scripting

How to FTP the latest file, based on date, from a remote server through a shell script?

How to FTP the latest file, based on date, from a remote server through a shell script? I have four files to be FTP'ed from remote server. They are of the following format. build1_runtime_mmddyyyy.txt build2_runtime_mmddyyyy.txt build3_runtime_mmddyyyy.txt buifile_count_mmddyyyy.txt ... (9 Replies)
Discussion started by: imran_affu
9 Replies

7. Shell Programming and Scripting

grep latest file based on date.

hi all, not sure if this has been posted b4 but i try to search but not valid. this is my question: when i do a ls -ltr there will be a list generated as follows: -rw-r--r-- 1 root sys 923260 Jan 10 04:38 FilePolling.41025.083TL021.xml -rw-r--r-- 1 root sys 1761337 Jan 10 04:40... (12 Replies)
Discussion started by: lweegp
12 Replies

8. Shell Programming and Scripting

shell script to find latest date and time of the files

Hi everyone, Please help:) I have a list of 1000 different files which comes daily to the directory.Some of the files are not coming to the directory now. I need to write a shell script to find the latest date and time of the files they came to the directory. The files should be unique.... (1 Reply)
Discussion started by: karthicss
1 Replies

9. Shell Programming and Scripting

shell script to selectively tar directory based on date

hye everybody :) , i'm new to the scripting world.. hope you guys can help me out with this one.. i'm trying to identify any directory under /tmp/saya that is created more than one day from the current date.. e.g, today is March 14, so any directory that has time stamp March 13 backwards, i... (2 Replies)
Discussion started by: fara_aris
2 Replies

10. Shell Programming and Scripting

Shell script to ftp latest file

Hello All, Hope u r doing fine. I'm writing a shell script to ftp the latest file having pericular convention as 'ULTI_15072007043205.txt' on daily basis. Now the date & timing of the file geleration isnt constant, so the file name daily varies as per the date & timing of generation. Can anyone... (7 Replies)
Discussion started by: im_new
7 Replies
Login or Register to Ask a Question