Selecting the file of latest Date


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Selecting the file of latest Date
# 1  
Old 05-19-2012
Error 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

cd /var/logs

at this location I see ten different files of different date with same name say abc.log , I want to see the abc.log file of the latest date before opening it in Vi , please advise me the command that will filter out the latest abc.log file of latest date, so That I can open the latest file through Vi editor
# 2  
Old 05-19-2012
You can't have ten files with the same name in a directory. Is the date embedded in the filename? If so, best is in the format YYYYMMDDHHmm, then the files will display already sorted.

Code:
vi $(tail -1 <(ls))

Otherwise
Code:
vi $(head -1 <(ls -t))

This User Gave Thanks to Scott For This Post:
# 3  
Old 05-19-2012
Quote:
Originally Posted by Scott
You can't have ten files with the same name in a directory. Is the date embedded in the filename? If so, best is in the format YYYYMMDDHHmm, then the files will display already sorted.

Code:
vi $(tail -1 <(ls))

Otherwise
Code:
vi $(head -1 <(ls -t))

Yeah scott , files are created with date is also embedded with them let say,
HTML Code:
abc20120515abc.log
abc20120516abc.log
abc20120517abc.log
so please advise I want to open the latest one through Vi editor abc20120517abc.log file
# 4  
Old 05-19-2012
Code:
vi $(tail -1 <(ls abc*.log))

Or if <(..) doesn't work
Code:
vi $(ls abc*.log | tail -1)

Otherwise
Code:
set -- abc*.log
vi $(eval echo \${$#})

# 5  
Old 05-19-2012
you will get the latest file name : ls -lrt | tail -1then vi it.

Last edited by Scott; 05-19-2012 at 12:35 PM.. Reason: Code tags
# 6  
Old 05-19-2012
Code:
vi $(ls -t abc*.log | head -1)


Last edited by Scott; 05-19-2012 at 12:35 PM.. Reason: Code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting latest entry in the log file

Hi there I am trying to write a script where I will need to look for a specific word in the log file and I am aware this can be done by grep for example. As there will be multiple entries for this I want to grep the last one to enter the log... how would I go about this - would I have to use... (5 Replies)
Discussion started by: simpsa27
5 Replies

2. Shell Programming and Scripting

Read latest file name with a date and time and then download from FTP

Hi All, Please help. I have requirement to read the file / folder based on the latest date and download the file and folder. There will be files and folders in the location like 20140630-144422 20140630-144422.csv 20140707-182653 20140707-182653.csv 20140710-183153... (7 Replies)
Discussion started by: Praveen Pandit
7 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. 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

7. UNIX for Dummies Questions & Answers

Removing duplicate rows & selecting only latest date

Gurus, From a file I need to remove duplicate rows based on the first column data but also we need to consider a date column where we need to keep the latest date (13th column). Ex: Input File: Output File: I know how to take out the duplicates but I couldn't figure out... (5 Replies)
Discussion started by: shash
5 Replies

8. Shell Programming and Scripting

RSYNC syntax for pushing file with latest system date

OK, I am a little new to AIX 5.3 and also to scripting. I have a shell script that I wrote and am having difficulty pushing specific files by the system date. Here is my script: #!/usr/bin/sh RSYNC=/usr/local/bin/rsync SSH=/usr/local/bin/ssh KEY=<path> somekey.key RUSER=mike... (4 Replies)
Discussion started by: tfort73
4 Replies

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

10. UNIX for Dummies Questions & Answers

get the latest file by reading the date in the filename.

Hi, I grep for a pattern in a list of files. "grep -i -l $pattern *.datx*" it may give me n number of files. say for eg, it gives me 2 files. lock_eicu_20071228_00000000.dat_20071228_05343100 lock_eicu_20080501_00000000.dat_20080501_05343900 out of these 2 files I need to get the... (7 Replies)
Discussion started by: prsshini
7 Replies
Login or Register to Ask a Question