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:
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:
But it is searching every file not just the last one, so it takes forever.
Thanks in advance!
Moderator's Comments:
Code tags for code, please.
Last edited by Corona688; 08-09-2012 at 05:33 PM..
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:
So you can feed the file from head into grep like this:
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)
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)
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)
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)
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)
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)
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)
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)
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)