Optimizing grep -rnw . -e "string_to_search"?

balenshah

Member
Joined
Aug 23, 2022
Messages
74
Reaction score
4
Credits
636
This greps for all files that contains the string_to_search in current directory. Is there a way to limit it to latest files? Or any way to optimize it? Like changing the command. Say I need to search for files from July only how'd I do it? Assume files are named programs.logs_2023-07-*
 


If you have a file dated 01-July-2023 could use "find" like this:

Code:
$ find -newer 'myfile.txt' -name '*.txt'

Replace "myfile.txt" with the file that has the desired date as the earliest.

Sadly "find" doesn't allow putting in a date directly. Must find a "reference" file with a desired date. But probably you could create a file and use "touch" utility, assuming it could set a file to a yesterday.
 
If you have a file dated 01-July-2023 could use "find" like this:

Code:
$ find -newer 'myfile.txt' -name '*.txt'

Replace "myfile.txt" with the file that has the desired date as the earliest.
I use this in case filename isn't known at all. I am searching filenames with this command. Is there a way to only include filenames in the output of that above command that I shared? It lists out all the contents of the file and it gets piled up badly.
 
I was going to edit my previous post saying you could create a temporary text file, say it's called "myfile.txt" then use "touch" like this:

Code:
touch myfile.txt --date=2023-07-01

Then use the "find" command that I proposed.

"find" is supposed to list filenames only, what are you doing that it is displaying file contents also?

Using "grep" sometimes is counterintuitive, and you might have to pipe it into another command to get the results you want. That's why I proposed something else. :)
 
can you explain that command above?I don't think it achieves what I want.
 
So are you saying that you want to search for some strings/patterns inside files that are newer than July 1st 2023?
If so, try this:
Bash:
find ./ -maxdepth 1 -type f -newermt 2023-07-01 -exec grep -iHn --color searchstring {} +
Where searchstring is the actual string you want to search for in the file.
If you want to search for something else - then replace searchstring with a string or regular expression that describes what you're looking for in the file.

The above command will use find to find ALL files in the current directory (without recursing into any sub-directories) that are newer than July 1st 2023.
Each file that is found is then ran through grep, to search for the string "searchstring".

Because we've used grep's -i flag, the search will be case-insensitive - if you want the search to be case sensitive - remove the -i flag.
The -H flag ensures that the filename is listed next to each result from grep, so you know which file the pattern was found in. The -n flag shows the line-number for the lines that matched the pattern. And the -color will highlight the matches in colour, so you can see which parts of the line matched the pattern. Again, if any of these flags are NOT required, feel free to remove them.

In find's -exec section - the {} is a placeholder for the path/filename of files that were found by find. And the + at the end tells find to append any other found files at the end of the command line.
This will cause the grep command in the -exec section to be ran once, with a list of ALL of the files found.

Alternatively, the -exec section could look like this:
Code:
-exec grep -iHn --color searchstring {} \;
If you end the -exec with \;, then the grep command will be ran each time a file is found. But if you have 100 files - grep will be ran 100 times.
I hope this is useful!
 

Members online


Latest posts

Top