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?
# 8  
Old 07-27-2005
There has to be a way to do this on the command line without creating a shell scriopt. Search for a string in all the sub dirs and print out the files and location?
# 9  
Old 07-27-2005
Thanks for re-posting. Could you explain how the parts of that search are working. I mean, it works but would like to learn something here.
# 10  
Old 07-27-2005
Code:
find . -type f -exec grep -l "somesearchstring" {} /dev/null \;

find . -type f # find all files from the currend directory down
-exec grep -l "somesearchstring" {} # execute the grep command on each file found {} means the found file
the use of /dev/null is a hack so that grep will output the name of the file indicated by {} because it thinks it is searching two files
\; is the end of the exec command.


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

find all files and use xargs to pass them as arguments to grep, for a full explanation of xargs I would suggest you read the man page.
# 11  
Old 07-27-2005
Cool. Appreciate the help
 
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