grep latest file based on date.


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Bug 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 FilePolling.41073.114TL020.xml
-rw-r--r-- 1 root sys 76 Jan 11 04:32 FilePolling.41423.024TL022.xml
-rw-r--r-- 1 root sys 82 Jan 11 04:40 FilePolling.41681.110TL022.xml
-rw-r--r-- 1 root sys 1136429 Jan 12 04:36 FilePolling.42235.068TL020.xml
-rw-r--r-- 1 root sys 82 Jan 12 04:40 FilePolling.42301.110TL022.xml
-rw-r--r-- 1 root sys 791807 Jan 13 04:31 FilePolling.42629.018TL024.xml

my question here is how do i grep the latest file (in this case the file will be the 13 Jan)? Smilie

can it be done?

thanks.
# 2  
Simplest could be

ls -latr

If you looking for an xml file then, ls -latr | grep '.xml$'
# 3  
grep "something" `ls -tr | tail -1`

Cheers
ZB
# 4  
hi there,

thanks for the solution but dont think it's working.

what i meant is i want the latest file to be shown not the whole list of them ie:

-rw-r--r-- 1 root sys 791807 Jan 13 04:31 FilePolling.42629.018TL024.xml
# 5  
Now I see what you want.

ls -latr | tail -1
-or-
ls -latr | sed -n '$p'

Cheers
ZB
# 6  
Quote:
Originally Posted by zazzybob
Now I see what you want.

ls -latr | tail -1
-or-
ls -latr | sed -n '$p'

Cheers
ZB
I think the option a can be done away with. It would be ls -ltr | tail -1
# 7  
hey guys,

its working...many thanks.

wee
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #338
Difficulty: Easy
telnet is considered safer than ssh for most remote login applications.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Picking the latest file based on a timestamp for a Dynamic file name

Hi , I did the initial search but could not find what I was expecting for. 15606Always_9999999997_20160418.xml 15606Always_9999999998_20160418.xml 15606Always_9999999999_20160418.xml 9819Always_99999999900_20160418.xml 9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Discussion started by: chillblue
4 Replies

2. Shell Programming and Scripting

I have this list of files . Now I will have to pick the latest file based on some condition

3679 Jul 21 23:59 belk_rpo_error_**po9324892**_07212014.log 0 Jul 22 23:59 belk_rpo_error_**po9324892**_07222014.log 3679 Jul 23 23:59 belk_rpo_error_**po9324892**_07232014.log 22 Jul 22 06:30 belk_rpo_error_**po9324267**_07012014.log 0 Jul 20 05:50... (5 Replies)
Discussion started by: LoneRanger
5 Replies

3. UNIX for Dummies Questions & Answers

How to pick the latest file with date as one among the file name.( not exactly present date.)?

i have files like 1)20131112_abc_01.csv and 2)20131113_abc_01.csv and 3)20131113_abc_02.csv when i try to fetch the file in the next day. it shud pick the third file.. plz help me.. and i use `date +"%Y%m%d"` command to fetch..it fetches the current date... (2 Replies)
Discussion started by: applepie
2 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

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... (6 Replies)
Discussion started by: hemanthsaikumar
6 Replies

6. UNIX for Dummies Questions & Answers

Selecting the file of latest Date

Hi Folks, I have one query that there is a folder in which daily several logs files are getting created , I reached to that location through putty but what I observer that 10 files of different date are been created with same name , what I need to see is the latest file ...let say the location is ... (5 Replies)
Discussion started by: KAREENA18
5 Replies

7. UNIX for Dummies Questions & Answers

Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (12 Replies)
Discussion started by: robertbrown624
12 Replies

8. Shell Programming and Scripting

Grep the Content of a LOG File which has latest Date and Time

Hi All, Need a small help. I have a log file which keeps updating for every Minute with multiple number of lines. I just want to grep few properties which has latest Date and Time to it. How do i do it? I wanted to grep a property by name "Reloading cache with a maximum of" from the... (4 Replies)
Discussion started by: nvindraneel
4 Replies

9. 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

10. Shell Programming and Scripting

grep based on date

Hello! I have a circular log file which contains data that looks like this: 01/23/09 08:24:19:04 treadle_data = L3^M 01/23/09 08:24:19:09 STRIP 3 LOW 01/23/09 08:24:19:09 treadle_data = L4^M 01/23/09 08:24:19:09 STRIP 4 LOW 01/23/09 08:24:19:09 treadle_data = FF^M 01/23/09 08:24:19:09... (8 Replies)
Discussion started by: sdilucca
8 Replies

Featured Tech Videos