Grep inside all files created within date range
This is a little different from Banthar's solution, but it will work with versions of find
that don't support -newermt
and it shows how to use the xargs
command, which is a very useful tool.
You can use the find
command to locate files "of a certain age". This will find all files modified between 5 and 10 days ago:
find /directory -type f -mtime -10 -mtime +5
To then search those files for a string:
find /directory -type f -mtime -10 -mtime +5 -print0 |
xargs -0 grep -l expression
You can also use the -exec
switch, but I find xargs
more readable (and it will often perform better, too, but possibly not in this case).
(Note that the -0
flag is there to let this command operate on files with embedded spaces, such as this is my filename
.)
Update for question in comments
When you provide multiple expressions to find
, they are ANDed together. E.g., if you ask for:
find . -name foo -size +10k
...find
will only return files that are both (a) named foo
and (b) larger than 10 kbytes. Similarly, if you specify:
find . -mtime -10 -mtime +5
...find
will only return files that are (a) newer than 10 days ago and (b) older than 5 days ago.
For example, on my system it is currently:
$ date
Fri Aug 19 12:55:21 EDT 2016
I have the following files:
$ ls -l
total 0
-rw-rw-r--. 1 lars lars 0 Aug 15 00:00 file1
-rw-rw-r--. 1 lars lars 0 Aug 10 00:00 file2
-rw-rw-r--. 1 lars lars 0 Aug 5 00:00 file3
If I ask for "files modified more than 5 days ago (-mtime +5
) I get:
$ find . -mtime +5
./file3
./file2
But if I ask for "files modified more than 5 days ago but less than 10 days ago" (-mtime +5 -mtime -10
), I get:
$ find . -mtime +5 -mtime -10
./file2
How to search contents of Linux files modified between certain dates
This is just quick & dirty and without sorting by date, but with 3 lines of context before/after each match and coloured matches:
find ~/mnt/c/code -type f -mtime -100 -mtime +5 | grep -v 'someUnwantedPath' | xargs -I '{}' sh -c "ls -l '{}' && grep --color -C 3 -h 'mySearchTerm' '{}'"
Broken down into pieces with some explanation:
# Find regular files between 100 and 5 days old (modification time)
find ~/mnt/c/code -type f -mtime -100 -mtime +5 |
# Remove unwanted files from list
grep -v 'someUnwantedPath' |
# List each file, then find search term in each file,
# highlighting matches and
# showing 3 lines of context above and below each match
xargs -I '{}' sh -c "ls -l '{}' && grep --color -C 3 -h 'mySearchTerm' '{}'"
I think you can take it from here. Of course this can be made more beautiful and fulfill all your requirements, but I just had a couple of minutes and leave it to the UNIX gurus to beat me and make this whole thing 200% better.
Update: version 2 without xargs
and with only one grep
command:
find ~/mnt/c/code -type f -mtime -30 -mtime +25 ! -path '*someUnwantedPath*' -exec stat -c "%y %s %n" {} \; -exec grep --color -C 3 -h 'mySearchTerm' {} \;
! -path '*someUnwantedPath*'
filters out unwanted paths, and the two -exec
subcommands list candidate files and then show the grep
results (which could also be empty), just like before. Please note that I changed from using ls -l
to stat -c "%y %s %n"
in order to list file date, size and name (just modify as you wish).
Again, with additional line breaks for readability:
find ~/mnt/c/code
-type f
-mtime -30 -mtime +25
! -path '*someUnwantedPath*'
-exec stat -c "%y %s %n" {} \;
-exec grep --color -C 3 -h 'mySearchTerm' {} \;
linux shell script -search in directory inside files its date is today and 3 day before
find -type f -mtime -3 -exec grep "wordToSearch" \{\} \;
How to grep lines of a file by range of value
you can use the following for this.
grep '2015-01-12 12:3[0-2]'
or
grep '2015-01-12 12:3[0,1,2]'
I hope it helps.
How to use 'find' to search for files created on a specific date?
As pointed out by Max, you can't, but checking files modified or accessed is not all that hard. I wrote a tutorial about this, as late as today. The essence of which is to use -newerXY
and ! -newerXY
:
Example: To find all files modified on the 7th of June, 2007:
$ find . -type f -newermt 2007-06-07 ! -newermt 2007-06-08
To find all files accessed on the 29th of september, 2008:
$ find . -type f -newerat 2008-09-29 ! -newerat 2008-09-30
Or, files which had their permission changed on the same day:
$ find . -type f -newerct 2008-09-29 ! -newerct 2008-09-30
If you don't change permissions on the file, 'c' would normally correspond to the creation date, though.
Related Topics
Grep Without Showing Path/File:Line
Sorting on the Last Field of a Line
Passing External Shell Script Variable via Ssh
Why Redirect Stdin Inside a While Read Loop in Bash
Rsync Copy Over Only Certain Types of Files Using Include Option
Shell - Write Variable Contents to a File
Is There an Invalid Pthread_T Id
Running Shell Script in Parallel
How to Get the Source Code for the Linux Utility Tail
How to Set Memory Limit for Oom Killer for Chrome
Nuget on Linux: Error Getting Response Stream
Do I Need -D_Reentrant with -Pthreads
Why Linux/Gnu Linker Chose Address 0X400000
Bash Script to Remove 'X' Amount of Characters the End of Multiple Filenames in a Directory