grep all files in a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep all files in a directory
# 1  
Old 12-30-2009
grep all files in a directory

I am not sure if i am doing this correctly since it returns quickly. i need to grep for a keyword in all files in a directory

grep keyword /mydirectory

is that correct? I just want to know which files have a keyword in it.

i am using korn shell in solaris 5.1. There does not appear to be a -r option in this version.


grep -r "keyword" /path/pathdir
grep: illegal option -- r
Usage: grep -hblcnsviw pattern file . . .
# 2  
Old 12-30-2009
Try:

Code:
grep "keyword" /mydirectory/*

# 3  
Old 12-30-2009
grep will take file(s) as parameter, not directories. So try
Code:
grep keyword /mydirectory/*

To search recursively you can add -R to some versions of grep. I often use on the command line something like
Code:
find .| grep pattern

If you don't get output in which file the pattern was found, you can add maybe a -l to your grep.
# 4  
Old 12-30-2009
You can try this..

for all files in current directory
Code:
grep Keyword *

for all files in current and subdirectories
Code:
grep -R Keyword *

if you want to list only filenames
Code:
 
grep -l -R keyword *

# 5  
Old 12-30-2009
use -l (form look) option:-

Code:
grep -l "pattern" /path/to/Mydirectory/*

# 6  
Old 12-30-2009
Or (if you don't mind searching subdirectories but do want to avoid searching directory files):

Code:
find /mydirectory/ -type f -print | while read FILENAME
do
       grep -l "keyword" "${FILENAME}"
done

# 7  
Old 12-30-2009
Quote:
Originally Posted by methyl
Or (if you don't mind searching subdirectories but do want to avoid searching directory files):

Code:
find /mydirectory/ -type f -print | while read FILENAME
do
       grep -l "keyword" "${FILENAME}"
done

But why does that requires a loop,
Code:
find /mydirectory/ -type f -exec grep -l 'keyword' {} \;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directory containing files,Print names of the files in the directory that are exactly same content.

Given a directory containing say a few thousand files, please output a list of all the names of the files in the directory that are exactly the same, i.e. have the same contents. func(a_directory_name) output -> {“matches”: , ... ]} e.g. func(“/home/my/files”) where the directory... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

2. Shell Programming and Scripting

Grep for all ip addresses mentioned in all files in a Directory

I wish to traverse all files and folders under a given directory say "/tmp/configuration" and for all ip address mentioned therein. I tried find ./ -type f | xargs grep "*.*.*.*" but it does not populated the correct results. Can you please suggest. (1 Reply)
Discussion started by: mohtashims
1 Replies

3. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

4. AIX

How to set owner and permission for files/directory in directory in this case?

Hi. My example: I have a filesystem /log. Everyday, log files are copied to /log. I'd like to set owner and permission for files and directories in /log like that chown -R log_adm /log/* chmod -R 544 /log/*It's OK, but just at that time. When a new log file or new directory is created in /log,... (8 Replies)
Discussion started by: bobochacha29
8 Replies

5. Shell Programming and Scripting

Grep showing all files name of the directory

Hi All, Can any one help me here.I'm getting whole files name of the directory along with the grep result. below is the code var=`grep -i '127.3.3.1' _record` echo $var (9 Replies)
Discussion started by: netdbaind
9 Replies

6. Shell Programming and Scripting

How to grep all the files inside the directory and Sub directory

Hi, I have used the command cat * | grep -r <<String>> * It returns: cat : JAN : is directory *********************** ********************* My directory structure: log - JAN -catalina.out -FEB -catalina.out -MARCH ... (11 Replies)
Discussion started by: nanthagopal
11 Replies

7. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

8. Shell Programming and Scripting

FTP files from different directory from remote server to one directory in local

Hi All, I want to search for .log files from folders and sub folders in remote server and FTP them to one particular folder in the local machine. I dont want to copy the entire directory tree structure, just have to take all the .log files from all the folders by doing a recursive search from the... (3 Replies)
Discussion started by: dassv
3 Replies

9. Shell Programming and Scripting

Finding the executable files of a directory using Grep

Hi guys, Can you please help me print all the executable files of a directory(in this case /home) using grep? All i know is that this command should do it but it doesnt... ls -l ~ | grep -..x it shows me the following mesage grep: invalid option -- '.' Χρήση: grep ... ΥΠΟΔΕΙΓΜΑ ... (3 Replies)
Discussion started by: jimas13
3 Replies

10. AIX

Help Using Grep command to Find the string in the files in the root directory

I want to serch for a string in all the files in the root directory. i want the search to be limited to only the files in the directory i.e the search to be done only in the files not in the sub directory. the approaches tried are 1)grep "pattern string" this command was serching the... (3 Replies)
Discussion started by: Subbu_Angeline
3 Replies
Login or Register to Ask a Question