Searching for a string inside multiple files and output the name of the files containing that string

find . -name “*” | grep -i ecomm | xargs ls -las | grep “Aug 27” | awk ‘{print $10}’ | xargs grep -i string_to_search_for | grep -i “\.log” | awk ‘{print $1}’

where:

“ecomm” is part of the name of the file in which we should search for the string
“Aug 27” is the date the files were last modified
“$10” is the last column resulting from the “ls -las” command, this is the column containing the filename (the string is not searched for yet)
“string_to_search_for” the name is obvious 🙂
“\.log” all the files end with “.log”
“$1” is the filename containing the string

There are still two problems left (I don’t have time right now to solve them):
  1. All the filenames end with the “:” character, it needs to be omitted
  2. Filenames are redundant, i.e. each file is repeated many times

Update: Problem number 2 solved using the “sort” and “uniq” commands which sort the results and then omit the repeated results, here is an example:

find . -name “*” | grep -i ecomm | xargs ls -las | grep “Aug 31” | awk ‘{print $10}’ | xargs grep -i mbassiouny | grep -i “\.log” | awk ‘{print $1}’ | sort | uniq

Advertisements

About SoCRaT

Research Support and Infrastructure Engineer, OSS & Linux Geek
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s