Ls followed by grep

Hi,
On AIX 7.1
I run the following:

ls -lrt | grep "Dec 18 14"

-rw-r--r--    1 user1 dba            1015 Dec 18 14:00 l21088100.req

-rw-r--r--    1 user1 dba            2491 Dec 18 14:01 l21088103.req

-rw-r--r--    1 user1 dba            1294 Dec 18 14:01 l21088102.req

-rw-r--r--    1 user1 dba            2471 Dec 18 14:01 l21088104.req

-rw-r--r--    1 user1 dba            2461 Dec 18 14:01 l21088105.req

-rw-r--r--    1 user1 dba            2467 Dec 18 14:02 l21088106.req

-rw-r--r--    1 user1 dba            5847 Dec 18 14:03 l21088107.req

-rw-r--r--    1 user1 dba           11400 Dec 18 14:03 l21088108.req

-rw-r--r--    1 user1 dba            1763 Dec 18 14:03 l21088101.req

-rw-r--r--    1 user1 dba            3033 Dec 18 14:05 l21088109.req

-rw-r--r--    1 user1 dba             401 Dec 18 14:07 FNDOPP297976.txt

-rw-r--r--    1 user1 dba             401 Dec 18 14:07 FNDOPP297975.txt

-rw-r--r--    1 user1 dba             401 Dec 18 14:07 FNDOPP297974.txt

-rw-r--r--    1 user1 dba             401 Dec 18 14:07 FNDOPP297973.txt

-rw-r--r--    1 user1 dba            1786 Dec 18 14:07 l21088111.req

-rw-r--r--    1 user1 dba            1015 Dec 18 14:07 l21088110.req

-rw-r--r--    1 user1 dba           11650 Dec 18 14:07 FNDOPP297977.txt

-rw-r--r--    1 user1 dba            1032 Dec 18 14:08 l21088112.req

-rw-r--r--    1 user1 dba            1015 Dec 18 14:10 l21088113.req

-rw-r--r--    1 user1 dba            2489 Dec 18 14:10 l21088116.req

-rw-r--r--    1 user1 dba            1294 Dec 18 14:10 l21088115.req

-rw-r--r--    1 user1 dba            2471 Dec 18 14:10 l21088117.req

-rw-r--r--    1 user1 dba            2461 Dec 18 14:11 l21088118.req

-rw-r--r--    1 user1 dba            2466 Dec 18 14:11 l21088119.req

-rw-r--r--    1 user1 dba            5754 Dec 18 14:11 l21088120.req

-rw-r--r--    1 user1 dba           12837 Dec 18 14:12 l21088121.req

How can I complete my ls commande to look for a word in all of these files?

like:

ls -l -exec grep myword {} \;

Thanks.

look into xargs

Quick and dirty - do not use with file names with space or other special characters!

ls -lrt | grep " Dec 18 14:" | awk '{ print $NF }'

This gives the file names. You can use xargs to convert stdin to command arguments

ls -lrt | grep " Dec 18 14:" | awk '{ print $NF }' | xargs grep myword

In the last grep use -l or -h option to only print file names or contents.

1 Like

You might be better to crete two temporary reference files with the correct timestamp.

What criteria do you have for the search date(s)? If it is just today's files, then you could do something like:

my_ref_file="$(mktemp)"
touch -mt $(date '+%Y%m%d0000' "${my_ref_file}"                                    # Sets the file timestamp to the start of today
find . -type f -newer "${my_ref_file}" -exec grep "myword" {} /dev/null \;         # Search for files newer than the reference file modification time (set above)
rm "${my_ref_file}"                                                                # Just to be tidy

This will create you a marker file based on the current date, setting it to midnight (the 0000 at the end of the date command) Using that, you can find any files that are newer and search for them. For each file found, it then runs the grep with {} representing the file just found. The reason to add /dev/null to the end is so that grep sees two files and therefore prints the file names where it finds your string.

Does this help? You may need to add in a -maxdepth flag to suit your needs if there are subdirectories.

If you have a date range to collect files from or a particular day to search, you can create two reference files to mark the start and end of the range. You would then add a ! -newer "${second_ref_file}" to stop it finding anything that is too new.

Of course, this could be modified to set specific times by replacing the 0000 with a required time coding when touching a reference file if that is required, e.g. you just want a particular hour.

I hope that this is useful,
Robin