KaverappaKU
KaverappaKU

Reputation: 65

I want to grep a pattern inside a file n list that file based on current date

ls -l | grep "Feb 22" | grep -l "good" *

This is the command i am using . i have 4 files among which one file contains the world good . I want to list that file . And that file creation is the current date . based on both the criteria i want to list that file

Upvotes: 0

Views: 1032

Answers (2)

Abhijit Pritam Dutta
Abhijit Pritam Dutta

Reputation: 5591

Hi Try with below command. How it works? Here find command with parameter -mtime -1 will search for files with current date in current directory as well as its sub directories. Each file found will be pass to grep command one at a time. grep command will check for the string in that file (means each file passes to it)

find . -mtime -1 -type f | xargs grep -i "good"

In the above command it will list all the file with current date. To list a files of specific kind you below command. Here I am listing only txt files.

find . -name "*.txt" -mtime -1 -type f | xargs grep -i "good"

find . is for running it from current directory (dot means current directory). To run it from a specific directory path modify like below:-

find /yourpath/ -name "*.txt" -mtime -1 -type f | xargs grep -i "good"

Also grep -i means "ignore case". For a specific case just use grep "good"

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185560

Try this :

find . -type f -newermt 2018-02-21 ! -newermt 2018-02-22 -exec grep -l good {} \;

or

find . -type f -newermt 2018-02-21 ! -newermt 2018-02-22 | xargs grep -l good

And please, don't parse ls output

Upvotes: 1

Related Questions