grep recursive directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep recursive directories
# 1  
Old 07-22-2003
Question grep recursive directories

I am trying to locate a file or files with specific data in them. Problem is the file(s) could reside in any one of many directories.

My question is. Is there a way of recursively greping directories for the file(s) with the data I am looking for.

I have tried -
1.
$HOME> grep 47518 | ls -R > results

2.
$HOME> ls -R > grep 47518

3.
$HOME> ls -R | grep 47518

Help please!!!
Smilie
# 2  
Old 07-22-2003
pipe the results of 'find' to 'grep'.

a simple example would be you want to find all files on your system that contain the word 'unix'.

do this with:

find / | grep unix ( optionally: | less )
# 3  
Old 07-22-2003
try this....
find . | xargs grep whatever
# 4  
Old 07-22-2003
By George it works !!!!

find . | xargs grep -s 47518

Great stuff thanks sssow
# 5  
Old 07-23-2003
Hhhm, want to add one more option :

find /my/dir/to/start -name \*pattern -exec grep text {} \;

Example :

find /tmp -name \*ipt -exec grep pall {} \; -exec ls -l {} \;
echo "He pall, please log of asap !!!"
-rwxr-xr-x 1 root sys 183 Jul 17 15:47 /tmp/script
server1:/root/home 1#

@yourservice
David
# 6  
Old 07-23-2003
Question

If this could be explained, I might see if I can use it.
At this stage I do not know what this command is doing.
The explaination of each part would be great.

Thanks.
Smilie
# 7  
Old 07-24-2003
Hi,

Sorry, I will try to make things a bit more clear :

find /tmp -name \*ipt -exec grep pall {} \; -exec ls -l {} \;

The find command you probably know. It searches for files or directorys where you want him to search and to what pattern to match.

So :
"find /tmp" says only start searching in /tmp
"-name \*ipt" says to look for a file or directory ending with "ipt" the \ before the star says to not look for a file or directory named "*ipt" but to make it a pattern
"-exec grep pall {} \;" says that the result of find in /tmp with the pattern ending on "ipt" should do the following : "grep pall /tmp/script" so {} \; is replaced with the current result. It's hard to explain the syntax, I think you should just take this as it is and copy the syntax when you need it Smilie
"-exec ls -l {} \;" has the same syntax and executes the ls -l command for the current result. If you don't do this last -exec you don't know what file he grep the text from.

Hope you understand it know. Don't forget there is always a man page from find. I know it is large, but there is so much beautifull to explore in the man from find !!

@yourservice
David
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recursive search for files and copy to new directories

So I have extremely limited experience with shell scripting and I was hoping someone could point out a few commands I need to use in order to pull this off with a shell script like BASH or whatnot (this is on OS X). I need to search out for filenames with account numbers in the name itself... (3 Replies)
Discussion started by: flyawaymike
3 Replies

2. UNIX for Dummies Questions & Answers

Using grep command to find the pattern of text in all directories and sub-directories.

Hi all, Using grep command, i want to find the pattern of text in all directories and sub-directories. e.g: if i want to search for a pattern named "parmeter", i used the command grep -i "param" ../* is this correct? (1 Reply)
Discussion started by: vinothrajan55
1 Replies

3. Shell Programming and Scripting

Recursive looping through files and directories

hi; i need a script which will go to all directories and subdirectories and print the filenames as follow; here i m printing only files listing in current directory reason i m doing this is coz i want to perform some operations according to filename achieved so cant use find command;... (4 Replies)
Discussion started by: ajaypadvi
4 Replies

4. Shell Programming and Scripting

Command to sort directories after a recursive find

find -type d -name "TC_*" | sort That's what I have so far... it finds the appropriate directories and then sorts them. But, when it comes to nested subdirectories, it only sorts relative to the first subdirectory. I want it to sort based on the directory at the end of the path. Does anyone know... (3 Replies)
Discussion started by: crimsondarkn
3 Replies

5. AIX

recursive archive directories and subdirectories

Hi everyone, Maybe this is simple question for many of you, but I get confused.:confused: How to archive a parent directory which contains some subdirectories and some files? I have searched this forum, there are some commands like tar,etc, I tried but can not be implemented in my system.... (6 Replies)
Discussion started by: wilsonSurya
6 Replies

6. UNIX and Linux Applications

CVS recursive diff -- how to exclude specific directories?

I think I've seen out there that there is a command to ignore specific files within a directory when doing a (-R) recursive diff. I've never used this so I was wondering if there was anyone who could provide an example how I would run this. My thoughts are something like: cvs diff -i <fileName1>... (2 Replies)
Discussion started by: airon23bball
2 Replies

7. Shell Programming and Scripting

Recursive call to find files and directories in Shell script from current path.

################################################################ Copy this script to your path from where you want to search for all the files and directories in subdirectories recursively. ################################################################# code starts here... (2 Replies)
Discussion started by: Ramit_Gupta
2 Replies

8. UNIX for Dummies Questions & Answers

Recursive grep

Hello, First time post - I have no formal unix training and could use some help with this. I have a list of strings in File1 that I want to use to do a recursive search (grep) under a specific directory. Here is an example of the string I need to search: /directory/dire... (16 Replies)
Discussion started by: upstate_boy
16 Replies

9. Shell Programming and Scripting

Script problem due to recursive directories Help please

Hello everyone , I am looking for a better solution then the one I have created for the my Task The task is: Create an automated script that will check for Uploads in a specified Directory and move them to another specified Directory if the files are completely uploaded. Files are FTP'd to... (2 Replies)
Discussion started by: robertmcol
2 Replies

10. UNIX for Advanced & Expert Users

recursive chmod that only affects directories?

The man page for chmod doesn't list a way to recursively change permissions on directories only, without affecting the files themselves. Let's say that I wanted to change the permissions on the current directory and all subdirectories. I know I can write a bash script that would do this using... (1 Reply)
Discussion started by: retrovertigo
1 Replies
Login or Register to Ask a Question