-1

My unsuccessful proposal

find ./ -newerct '1 week ago' -print | grep TODO

No output although should be.

Files are text files like

Lorem
% TODO check this out
Lorem ipsun

How can you find less than 1 week old files matching TODO? Output should be the line after TODO. Perl solution is also welcome, since I am practising it too.

5

Change this:

find ./ -newerct '1 week ago' -print | grep TODO

to this:

find ./ -newerct '1 week ago' -exec grep TODO {} +

or this:

find ./ -newerct '1 week ago' -print | xargs grep TODO

Explanation

Your grep doesn't interpret the output of find as a list of files to search through, but rather as its input. That is, grep tries to match TODO in the names of files rather than their contents.

From the grep(1) man page:

grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name)

To match the line after TODO:

find ./ -newerct '1 week ago' -exec grep -A1 TODO {} + | grep -v TODO

This assumes you have GNU grep.

0

You can use that:

find ./ -type f -newerct '1 week ago' -exec sh -c "grep -H -A1 TODO {} | tail -1" \;

Explanation:

  • -type f: includes only files in the search pattern
  • -exec sh -c "...": executes ... for all found files
  • grep -H -A1 TODO {} | tail -1: -H prints the filename, -A1 prints the line after the match, and tail -1 deletes the first line (the line with TODO) out of the output.
0

Try this

find . -type f -ctime -7 -print | grep -R "TODO" /etc/

-type f -type f to list only files

-ctime -7 is for files with a created time less than 7 days old

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.