LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   To grep a text from a list of files. (https://www.linuxquestions.org/questions/linux-general-1/to-grep-a-text-from-a-list-of-files-632635/)

ZAMO 04-03-2008 02:46 AM

To grep a text from a list of files.
 
hi all,


I want to locate a file which contains a particular script, say "xxxx". The file is located along with millions of files , inside a directory. The only clue , i have is ,the text is inside a file which is created on 12th feb.
I used
"ls -ltr |grep -e "Feb 12"
to list the files. Here i get thousands of files. how can i isolate the file which contains my text "xxxx" from the output of
"ls -ltr |grep -e "Feb 12"

Please assist.
Thanks

jschiwal 04-03-2008 03:40 AM

You might try something like this. I put grep inside of the find command.
Code:

touch --d 20080213 end
touch --d 20080212 start
find ./ -maxdepth 1 -type f -newer start -not newer end -exec grep "xxxx" '{}' \;

Another method is to iterate through the output, but there are two problems. First, the output contains more than the filename. Second, you may run out of memory.

Assuming you won't run out of memory, this will list just the filenames from the last field:
Code:

ls -l | awk 'BEGIN {FS=" *"} /Feb 12/{ print $NF }'
Using ls and filtering out just the filenames can be tricky if there might be white space in the filenames.
Code:

for file in $(ls -l | awk 'BEGIN {FS=" "} /Feb/{  for (i = 9; i <= NF; i++) printf "%s ", $i; printf "\n"}'); do
grep 'xxxx' "$file"
done

But with thousands of results, you may run out of memory due to the expansion of the arguments. The trailing space at the end of the filenames may cause a problem as well. Using the find command would be a better way.

matthewg42 04-03-2008 05:08 AM

Note that the date reported by ls is not the file creation date - it is the file modification date. Linux (and unix-like OSes in general) do not keep the creation date. In the case where the file has not been modified since it was created, it is the same thing but if the file has been modified, the creation date is lost (unless the creation date is part of the file name).

ZAMO 04-03-2008 06:55 AM

Thank you both...

I am going with
find ./dir -maxdepth 1 -type f -print0 | xargs -0 --max-args=200 grep -e 'xxxx'

allend 04-03-2008 07:10 AM

Have you looked at the -l option for grep?
Code:

find . -name "*" -exec grep -l xxxx '{}' \;


All times are GMT -5. The time now is 06:04 AM.