LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-03-2008, 02:46 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
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
 
Old 04-03-2008, 03:40 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 04-03-2008, 05:08 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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).
 
Old 04-03-2008, 06:55 AM   #4
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Thank you both...

I am going with
find ./dir -maxdepth 1 -type f -print0 | xargs -0 --max-args=200 grep -e 'xxxx'
 
Old 04-03-2008, 07:10 AM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
Have you looked at the -l option for grep?
Code:
find . -name "*" -exec grep -l xxxx '{}' \;

Last edited by allend; 04-03-2008 at 07:12 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep through tab completion list results tphyahoo Linux - General 2 11-18-2006 08:40 PM
Utility Needed - list folder tree and files in text file Optiker Linux - Software 21 11-17-2006 02:46 PM
How do I replace text with perl with a list fo files? nadavvin Programming 7 09-14-2006 07:12 PM
Grep text man_linux Linux - General 3 09-02-2006 12:06 AM
Command to select and move mutiple files from list in text file steve.paris Linux - Newbie 11 11-27-2005 12:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration