- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- grep in subdirectories
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 07:24 AM
тАО06-18-2002 07:24 AM
i have been working on this question for quite some time - but havent found an answer. pls can anyone suggest a solution -
when we use the grep command - it searches for text in the present working directory - but if i want to search the pattern ( text ) also in the files in subdirectories and the sub -sub directories then how do i do that ?
this could be very useful i think for many.
hope to hear from someone very soon.
Ninad
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 07:29 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 07:29 AM
тАО06-18-2002 07:29 AM
Re: grep in subdirectories
Use find:
find /directory_name -exec grep -l "search for this text" {} \;
That should do it,
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 07:32 AM
тАО06-18-2002 07:32 AM
Re: grep in subdirectories
you can find this option in the man page
of the command grep
just check the man page - after all the option
Search all files in the current directory for the string xyz:
grep xyz *
Search all files in the current directory subtree for the string xyz,
and ensure that no error occurs due to file name expansion exceeding
system argument list limits:
find . -type f -print |xargs grep xyz
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 07:32 AM
тАО06-18-2002 07:32 AM
Re: grep in subdirectories
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 07:33 AM
тАО06-18-2002 07:33 AM
Re: grep in subdirectories
actually just "find" and "grep" is too dangerous!
So usually I do it that way:
cd /your/start/directory
find . -type f -print |
while read name; do
case "$(file $name)" in
*text*) grep "YOUR-PATTERN-HERE" $name /dev/null ;;
esac
done | more
Just my $0.02,
Wodisch
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 09:20 AM
тАО06-18-2002 09:20 AM
Re: grep in subdirectories
It's a resource hog, and should be used lightly (it spawns a "grep" for every file), but I've never heard it called dangerous.
Please, do tell why!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 09:49 AM
тАО06-18-2002 09:49 AM
Re: grep in subdirectories
You can get it from the software porting and archive centre.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 10:38 AM
тАО06-18-2002 10:38 AM
Re: grep in subdirectories
I hesitate to answer for Wodisch, but the obvious problem with piping the output of find directly to grep is that it you encounter compiled binaries, special files, etc. you will hose your terminal session and fill your screen/output file with garbage. Wodisch's case statement tests for the string "text" in the output of the file [FILENAME] command, which prevents such ill-behaved activity.
I don't know that I would call it dangerous, but it is certainly annoying.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 10:46 AM
тАО06-18-2002 10:46 AM
Re: grep in subdirectories
Here is your answer
find /var -name '*' -exec grep -i -l '15AUG00 16:38' {} \; > /tmp/output_file
This will find all files contained within the /var directory (and any subdirectories). Notice that the wildcard * is enclosed in single quotes. The output of the find is sent to a grep with a -i (case insensitive) and -l (ell) switch. The -l swich is necessary as this causes the grep to return the name of the file in which the search text was found (as opposed to simply returning a copy of the line itself).
In the above example, I have searched for the string '15AUG00 16:38' (note the single quotes again around the string) and sent the output to a file called /tmp/outpu_file.
Regards
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 11:03 AM
тАО06-18-2002 11:03 AM
Re: grep in subdirectories
notice the back quote
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 01:24 PM
тАО06-18-2002 01:24 PM
Re: grep in subdirectories
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 01:57 PM
тАО06-18-2002 01:57 PM
Re: grep in subdirectories
Use "find /directory -type f -exec grep -l
Also you can use "grep
Replace the pattern with the string you are looking for.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 04:18 PM
тАО06-18-2002 04:18 PM
Re: grep in subdirectories
I don't know if I would call it dangerous, either... which is why I asked.
Wodisch, now I'm really curious!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 05:07 PM
тАО06-18-2002 05:07 PM
Re: grep in subdirectories
find . -type f | perl -ne 'chomp; print "$_\n" if -T;' | xargs grep -l pattern
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-18-2002 10:39 PM
тАО06-18-2002 10:39 PM
Re: grep in subdirectories
thanks everyone for their very prompt and useful reply.
the grep -l option shows only the filenames and not the line containing the pattern - but still it serves the purpose.
and procura - i will definitely try the gnu option , thanks for the link.
Ninad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-19-2002 05:24 AM
тАО06-19-2002 05:24 AM