Grep last file in a directory by time


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep last file in a directory by time
# 1  
Old 08-09-2012
Grep last file in a directory by time

Greetings,

I wasn't able to find this exact issue through a forum search, so I hope it is not a repost.

I have a directory of files that all end in ".log", and I would like to grep out something from the most recently created file in the directory without knowing the exact file name (aka I want to just grab the last file, not look at it's name).

So far, I have tried this:

Code:
ls -ltr | tail -1 | grep "Something" *.log

If I do just a ls -ltr | grep -1, it shows the last file like I want, but the grep ends up grepping on all the files in the directory (and there are a crap ton of files), so I guess I am not understanding exactly how the pipe thing works.

This gives me what I want:

Code:
grep "Something" *.log | tail -1

But it is searching every file not just the last one, so it takes forever.

Thanks in advance!
Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 08-09-2012 at 05:33 PM..
# 2  
Old 08-09-2012
You don't need to use -l unless you actually need the long-style listing.

You want to feed a filename into grep, which doesn't take filenames on standard input. The xargs tool can be used to turn a stream into arguments though.

Here is how xargs works in short:

Code:
# These two commands are equivalent.
cat a b c
echo a b c | xargs cat

So you can feed the file from head into grep like this:
Code:
ls -tr | head -n 1 | xargs grep "search string"

# 3  
Old 08-09-2012
That worked! Thanks for the help
This User Gave Thanks to Tennesseej For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a log file starting from a specific time to the end of file

I have a log file which have a date and time at the start of every line. I need to search the log file starting from a specific time to the end of file. For example: Starting point: July 29 2018 21:00:00 End point : end of file My concern is what if the pattern of `July 29 2018 21:00:00`... (3 Replies)
Discussion started by: erin00
3 Replies

2. Shell Programming and Scripting

Grep in a log file within a time range (hour)

Hi, im trying to write a grep script that returns me the last inputs added in the last hour in the log file. Literally i have nothing yet but: grep 'Line im looking for' LOGFILE.log | tail -1 this only gives me the last input, but no necessarily from the last hour. Help Please. (4 Replies)
Discussion started by: blacksteel1988
4 Replies

3. UNIX for Dummies Questions & Answers

Does 'grep' update a file's access date/time?

I've got a job that finds and removes trace files based upon an access time of more than seven days (I've also tried a modify date). find TABC* -atime +7 -exec rm + find TABC* -mtime +7 -exec rm + Whether I use -atime or -mtime, the process seems to work sporadically. Sometimes it removes... (6 Replies)
Discussion started by: Scottie1954
6 Replies

4. Shell Programming and Scripting

How to extract latest file by looking at date time stamp from a directory?

hi, i have a Archive directory in which files are archived or stored with date and time stamp to prevent over writing. example: there are 5 files s1.txt s2.txt s3.txt s4.txt s5.txt while moving these files to archive directory, date and time stamp is added. of format `date... (9 Replies)
Discussion started by: Little
9 Replies

5. UNIX for Dummies Questions & Answers

Why I don't see the time in some file/directory?

Hi all, When doing an ls -l, why is it that I have the date and time on some of the files and directory, while others don't? :wall: Is there a way to show the date and time using a different command? OS is Linux and Solaris. Any response much appreciated. Thanks in advance. (1 Reply)
Discussion started by: newbie_01
1 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. Shell Programming and Scripting

grep to show date/time of file the string was found in.

I've seen several examples of grep showing the filename the string was found in, but what I really need is grep to show the file details in long format (like ls -l would). scenario is: grep mobile_number todays_files This will show me the string I'm after & which files they turn up in, but... (2 Replies)
Discussion started by: woodstock
2 Replies

8. UNIX for Dummies Questions & Answers

Copying one file at a time from one directory to another directory.

Hi All i want to write a script which could copy one file at a time from one directory to another directory. Scenerio: Let's say i have 100 file in a dirctory,so i want to copy one file at a time to another directory with a sleep statement in between that of 30 secs. please help me... (1 Reply)
Discussion started by: Nikhilindurkar
1 Replies

9. Shell Programming and Scripting

how to grep the oldest file in a directory

Hi, Please help me out I want to grep the oldest file in a directory, could I use "ls" command? and how? thanx in advance (1 Reply)
Discussion started by: ericaworld
1 Replies

10. Programming

problem deleting date-time stamped file in a directory

I have a number of files of the format filename.xfr_mmddyy_%H%M%S which i get in a specified directory daily. Now i want to search in the specified directory & delete the files which are more than 2 days old .So I use a command find $DIR/backup/* -ctime +2 -exec rm -f {} \; But after executing... (1 Reply)
Discussion started by: dharmesht
1 Replies
Login or Register to Ask a Question