Visit Our UNIX and Linux User Community


find the most recent file containing a certain string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find the most recent file containing a certain string
# 1  
Old 09-17-2009
find the most recent file containing a certain string

I want to find the most recent file containing ' NORESETLOGS"
I'm already here but, how to sort this now in a correct way ?
By the way, my version of find does not know about 'fprint'

find . -type f -exec grep -i " NORESETLOGS" {} \; -exec ls -l {} \; | grep -vi " RESETLOGS"
# 2  
Old 09-17-2009
1) Are all the files in the same directory or are there multiple subdirectories?
2) What is the frequency of creation of the files and how many files are there to search?
3) Are all the files normal unix text files?
# 3  
Old 09-17-2009
wont it work ?

Code:
find . -type f | xargs grep -l "NORESETLOGS" 2>/dev/null | xargs ls -rt | tail -1

# 4  
Old 09-17-2009
anchal_khare method does not work because "ls -rt" with a command line containing multiple files in multiple directories does not sort the files by time. It could sometimes appear to work because of the order find processes the files.

If the files are spread in multiple directories it seems complex. We can however compare file timestamps with the "-nt" or "-ot" shell operators and easily find the most recent file in a list of files.
# 5  
Old 09-18-2009
1) the files are all in the same directory and their are no subdirs
2) the frequency and the number of files changes from 60 per year to 20 per week depending on some external factors
3) this are all normal unix text files

---------- Post updated at 09:38 AM ---------- Previous update was at 08:59 AM ----------

the solution of anchal_khare does not work, it's well possiblze thatt here are no files containing this string, in this case the solution gives the last file in the dir anyway, and it should give nothing

---------- Post updated at 09:45 AM ---------- Previous update was at 09:38 AM ----------

I think I found a simple solution
In the first try I wanted to find all the files with the string and sort them later on, I just reversed this approach. I sort all the files first and search the string afterwards

Code:
ls -1rt *.trc | xargs grep -l " NORESETLOGS" | tail -1

# 6  
Old 09-18-2009
Quote:
Originally Posted by methyl
anchal_khare method does not work because "ls -rt" with a command line containing multiple files in multiple directories does not sort the files by time. It could sometimes appear to work because of the order find processes the files.

If the files are spread in multiple directories it seems complex. We can however compare file timestamps with the "-nt" or "-ot" shell operators and easily find the most recent file in a list of files.

Thanks for the info methyl. I hadn't thought of it.

Previous Thread | Next Thread
Test Your Knowledge in Computers #893
Difficulty: Easy
sed uses regular expressions, such as .* (zero or more of any character).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help script find file most recent

Hi, I need to find the most recent files by their name from an X repertoire. The problem is that the name of the files is of type: POWERL10_20151203000.xml POWERL10_20151203001.xml POWERL10_20151202000.xml FIXED VALUE_DATENNN.xml NNN = Sequential number I would need to recover the... (4 Replies)
Discussion started by: verita
4 Replies

2. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

3. UNIX for Dummies Questions & Answers

Find most recent file and copy to another directory.

Very new to shell scripting. Not sure if my title is correct but I will try and explain. Directory has 100+ files with this format, " ABCD_ABC_Abc_AB0126.abc ". When a new file gets created, the 16-19 characters in the file name gets incremented by 1. Ex...todays most recent file is... (14 Replies)
Discussion started by: askvip
14 Replies

4. Shell Programming and Scripting

Find string in file and append new string after

Hi All, I'm trying to insert a string into a file at a specific location. I'd like to add a string after the parent::__construct(); in my file. <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { function... (6 Replies)
Discussion started by: jjkilpatrick
6 Replies

5. Shell Programming and Scripting

Find multiple string in one file using find command

Hi, I want find multiple string in one file using find coomand. And keeping it in one variable.grep is not working. (5 Replies)
Discussion started by: vivek1489
5 Replies

6. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

7. Shell Programming and Scripting

How to find the recent file in many sub-directories?

Hi guys, Under my root directory there are many sub-directories which contains log file for every day of running. How can I find , in one command only, the recent log file in each sub-directory? For example, If I run the following: find . -name "exp_prod_*_*_yes_*_.log" -exec ls -ltr {} \;... (12 Replies)
Discussion started by: nir_s
12 Replies

8. UNIX for Dummies Questions & Answers

command to find most recent file

Hi, Here is my problem: I try to write a script to find data in a file named "data" for exemple. Let's say I am in the directory /x/y/z, there are several sub-directories in the "z" directory (each sub-directory has a file "data") and I am searching for the word "help". So I use this... (9 Replies)
Discussion started by: StephB
9 Replies

9. Shell Programming and Scripting

how to get the most recent file that contains a specific string

Hi All, I wish to get the most recent file from a dir which contains a specific string. for example, in a dir sample/ , i have 3 files file1.txt -- contains 'good' file2.txt -- contains 'good' file3.txt-- contans 'hello' I want to search for the recent file (that is file2.txt) which... (3 Replies)
Discussion started by: little_wonder
3 Replies

10. Shell Programming and Scripting

Find most recent files in dirs and tar them up?

Hey all.. This should be simple but stoopid here can't get head around it! I have many directories, say 100 each with many files inside. I need a script to traverse through the dirs, find most recent file in each dir and add it to a tar file. I can find the files with something like for... (1 Reply)
Discussion started by: bobdung
1 Replies

Featured Tech Videos