using grep to find a value in current dir and sub dirs?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers using grep to find a value in current dir and sub dirs?
# 1  
Old 07-27-2005
using grep to find a value in current dir and sub dirs?

Hey guys,

I would like to find all files which contain "client1.dat". I would like to search from the current directory and all subs and print out all the files that have this. Any help would be greatly appreciated.

Thanks much.
# 2  
Old 07-27-2005
Quote:
Originally Posted by ecupirate1998
Hey guys,

I would like to find all files which contain "client1.dat". I would like to search from the current directory and all subs and print out all the files that have this. Any help would be greatly appreciated.

Thanks much.
"which contain 'client1.dat'" Smilie what do you actually want?,.. show what directory (or subdirectory) contains a file client1.dat or what file in each dir or subdir contains the word "client1.dat"?

for print each client1.dat in dir or subdirs

#printername is your printername configured in your /etc/printcap file

for i in `find . -name "client1.dat" -print`; do lp -d printername $i; done

best regards!

Last edited by infierno; 07-27-2005 at 04:45 PM..
# 3  
Old 07-27-2005
error when trying the example

tried the example and got an error. Do I need to set something up to use this?
error:
grep: illegal option --R
Usage: grep -hblcnsviw pattern file ....
# 4  
Old 07-27-2005
i have re edited my answer, I recently noted that you want that each client1.dat on any dir or subdir may be printed, is that right?
# 5  
Old 07-27-2005
For example,

I would like to find all files that contain "tim" in the current dir and sub dirs. Show the files on the screen.
# 6  
Old 07-27-2005
that is what the -R option is for on grep (GNU) Smilie,.. but anyway... you can do that like this as a script:

Code:
#!/bin/sh

for i in `find . -name "$1" -print`; do
   if grep $2 $i 2>&1 > /dev/null; then
      echo "$i"
   fi
done

so, you invoke from your shell like this (assuming its name is "xgrep")

./xgrep *.txt tim

or

./xgrep * tim


best regards!
# 7  
Old 07-27-2005
The questions is answered about one a week on these forums. Please use the search functionality before posting.

But just this once I'll repost the answer.

Code:
find . -type f -exec grep -l "somesearchstring" {} /dev/null \;

or
Code:
find . -type f | xargs grep -l somesearchstring

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string in files in all dir and sub dirs

Hello, I need to replace xml version='1.1' with xml version='1.0' in all xml files under /app/jenkins/ in all dir and sub dirs in my CentOS VM, I tried below command but it didn't help, looks like I'm missing a character somewhere. grep -rl "xml version='1.1'" . | xargs sed -i 's/"xml... (2 Replies)
Discussion started by: mahesh Madpathi
2 Replies

2. Shell Programming and Scripting

Save all the file of current date in another dir?

Hi i want to copy all the files of current date in another directory. for example, below i want to save all the file of 26 march to copied in debug dir. $ ls -lrt | tail -5 -rwxrwxrwx 1 khare guest 73 Jan 6 12:35 chk -rw-r--r-- 1 khare guest 770 Mar 26 02:21 cc1... (2 Replies)
Discussion started by: scriptor
2 Replies

3. Shell Programming and Scripting

how to copy current date files to another dir

i have directory /abcd and i want to copy all today date files in /xyz directory. i am able to see the files by using below command but not able to understand copy. find . -mtime -1 -type f -exec ls -l {} \; (2 Replies)
Discussion started by: learnbash
2 Replies

4. Emergency UNIX and Linux Support

find all the dirs starting with particular name

Hi Experts, I want to find all the dirs , subdirs on the sever which start with "sr". Can anyone let me know command for the same. find . -type d -name sr* I tried this but it is not working. Thanks, Ajay (4 Replies)
Discussion started by: ajaypatil_am
4 Replies

5. Shell Programming and Scripting

KSH - Find paths of multiple files in CC (dir and sub-dir))

Dear Members, I have a list of xml files like abc.xml.table prq.xml.table ... .. . in a txt file. Now I have to search the file(s) in all directories and sub-directories and print the full path of file in a output txt file. Please help me with the script or command to do so. ... (11 Replies)
Discussion started by: Yoodit
11 Replies

6. Shell Programming and Scripting

A script to find dir, delete files in, and then del dir?

Hello!! I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory: drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories... (3 Replies)
Discussion started by: bigben1220
3 Replies

7. UNIX and Linux Applications

CPIO Problem, copy to the root dir / instead of current dir

HI all, I got a CPIO archive that contains a unix filesystem that I try to extract, but it extract to the root dir / unstead of current dir, and happily it detects my file are newer otherwise it would have overwrited my system's file! I tried all these commands cpio -i --make-directories <... (2 Replies)
Discussion started by: nekkro-kvlt
2 Replies

8. UNIX for Dummies Questions & Answers

grep usage - skipping dirs

I am in Linux and would like to use the -d skip option for skipping directories in a grep search. What is the order of switches and strings? switches I'd like to use: -irl -d skip '/dirtoskip' text I am searching for: 'fax' dir in which to begin: /var/www/kewbie/* How should I... (1 Reply)
Discussion started by: kewbie
1 Replies

9. Shell Programming and Scripting

calling current working dir from script

Hello, I am having problem in setting current working directory from shell. I want to set pwd as an environmental variable in a script. I am following an existing script which is defined as HOME=$(shell dirname `pwd`) C_HOME=$(shell echo $(HOME) | sed -e 's:\/:\\\/:g' ) But when I am trying... (3 Replies)
Discussion started by: chandra004
3 Replies

10. UNIX for Dummies Questions & Answers

Finding current working dir path

Hi Folks, In a Unix (ksh) script, is there a way to determine the current working directory path of another logged-in user? Of course, I can use "pwd" to find my own path. But, how do I find it for another active user? Thanks for any input you can provide. LY (6 Replies)
Discussion started by: liteyear18
6 Replies
Login or Register to Ask a Question