I need to find the folder names along with the file names which has my string in their contents. I am in this directory "/data/queue/data" and I have lot of folders in the same directory and inside those each folders, I have various files.

Below are the folders in this directory "/data/queue/data" and each of those folders has various files in it.

david@machineA:/data/queue/data$ ls -lrth
drwxr-xr-x 2 david david  12K Apr 11 18:58 1428800400
drwxr-xr-x 2 david david  12K Apr 11 19:58 1428804000
drwxr-xr-x 2 david david  12K Apr 11 20:58 1428807600

I want to run a command from this directory only - "/data/queue/data" but it should only find the files inside folder whose timestamp is from April 13th 3 AM to April 13th 9 AM.

Below is the command I have which will recursively find the files inside all the folders but how should I make sure that it should only find files from April 13th 3 AM to April 13th 9 AM folders?

david@machineA:/data/queue/data$ grep -rl "pcV6URY" /data/queue/data

What is the best way to do this?


Try this:

touch -d"April 13 3 AM" file1
touch -d"April 13 9 AM" file2
find . -newer file1 ! -newer file2 -exec grep -l "pcV6URY" {} +
rm file1 file2

How it works

find can work directly with times but touch handles human-style dates better:

  • touch -d"April 13 3 AM" file1; touch -d"April 13 9 AM" file2

    This creates two files to mark the beginning and end of the time range.

  • find . -newer file1 ! -newer file2 -exec grep -l "pcV6URY" {} +

    This finds files newer than file1 but not newer than file2. For all such files, it runs grep on them to see if they contain your string.

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.