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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep all the files inside the directory and Sub directory
# 8  
Old 08-24-2012
grep'ing some strings

If you want to search multiple strings in a particular folder use the below commands.

Code:
grep -r '<pattern1>\|<pattern2>\|etc' /path/to/dirctory

the above command will yield the output of line containing the pattern in all files in that particular directory.

If you want to list the files alone which has the pattern use the command given below

Code:
grep -lr  '<pattern1>\|<pattern2>\|etc' /path/to/dirctory


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-24-2012 at 05:28 AM.. Reason: code tags
# 9  
Old 08-24-2012
Quote:
Originally Posted by priyak
If you want to search multiple strings in a particular folder use the below commands.

grep -r '<pattern1>\|<pattern2>\|etc' /path/to/dirctory

the above command will yield the output of line containing the pattern in all files in that particular directory.

If you want to list the files alone which has the pattern use the command given below

grep -lr '<pattern1>\|<pattern2>\|etc' /path/to/dirctory
This will print/select only those lines which contain either <pattern1> or <pattern2> or .... That's not the same as <pattern1> and <pattern2> and ... which the OP seemingly desires.
# 10  
Old 08-24-2012
Hi elixir_sinari,

Have tried using that command. When i attempted it resulted me with both the patterns.I think you have misunderstood the symbol '|' Smilie
# 11  
Old 08-24-2012
Check this:
Code:
$ cat testfile
WORD1 WORD2
ONLY WORD1
ONLY WORD2

$ grep 'WORD1\|WORD2' testfile
WORD1 WORD2
ONLY WORD1
ONLY WORD2

Now, don't you wonder why I am getting all three lines when I require only the first line which has both WORD1 and WORD2?

With slight modifications:
Code:
$ grep 'WORD1.*WORD2\|WORD2.*WORD1' testfile
WORD1 WORD2

Do you still think I am mistaken?
This User Gave Thanks to elixir_sinari For This Post:
# 12  
Old 08-24-2012
sorry dude... now its clear... actually i was little bit confused
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Apache2 does not display files inside directory

Hello, I have been running Ubuntu14.04 + apache2. 000-default.conf: <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory... (5 Replies)
Discussion started by: baris35
5 Replies

2. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

3. Shell Programming and Scripting

remove a whole directory tree WITH files inside?

Assume I want to remove a whole directory tree beginning with /foo/bar/ The directory or sub-directories may contain files. The top directory /foo/bar/ itself should not be deleted. rm -f- r /foo/bar does not work because it requires a directory tree without files. How does it work... (3 Replies)
Discussion started by: pstein
3 Replies

4. UNIX for Dummies Questions & Answers

Zip files inside the directory, but not the directory itself

Hi, Im facing a problem that im stucked, I have the following structure: thales@pereirtc-vbox:/home/VfARM$ ls code config doc lib manifest.bak manifest.rel manifest.v3 ns pub if i try to execute zip -q -o arm.zip VfARM/* it will create a zip file with the folder VfARM.... (2 Replies)
Discussion started by: Thales.Claro
2 Replies

5. Shell Programming and Scripting

gzip files inside directory

Hello folks I hope every one is fine. I need to ask one question. I have directory /xx/abcd/data/ inside that data there are files like 11.txt 23.txt 12.txt *.txt i want that i will do compress each txt file inside that directory /xx/abcd/data/. But it will not gzip data... (1 Reply)
Discussion started by: learnbash
1 Replies

6. Shell Programming and Scripting

Find files inside the parent directory only

Hi All, The following find command lists the files which are 45 minutes older. But it searches for the sub directories also. $ find . -type f -mmin +45 -print ./hello.txt ./test/hi.txt ./temp/now.txt ls hello.txt test temp How can i modify the find command in such way that it finds... (4 Replies)
Discussion started by: Tuxidow
4 Replies

7. UNIX for Dummies Questions & Answers

searching files inside directory

hey, i need to use grep to search a bunch of header files inside a directory to return which file i can find the function i'm searching for in. how do i use wild cards to search through the files? i can only figure out how to search inside the directory, not inside the files that are in the... (4 Replies)
Discussion started by: kylethesir
4 Replies

8. Shell Programming and Scripting

files of size 0 need to be deleted inside a directory

Hiiii, I have written a script which takes backup of some log files. let say the backuplocation is --- /abc/backuplocation -rw-r--r-- 1 webmut2 spgroup 0 Jan 27 02:41 ansrpt23994.log -rw-r--r-- 1 webmut2 spgroup 0 Jan 27 02:41 ansrpt3601.log -rw-r--r-- 1... (2 Replies)
Discussion started by: namishtiwari
2 Replies

9. UNIX for Advanced & Expert Users

How to see directory and the files inside it.

Hi Expert, How can i able to see the directory and the files inside the directories. Is it possible, please suggest. Thanks shaan (4 Replies)
Discussion started by: shaan_dmp
4 Replies

10. Solaris

How to delete Directory and inside files using Find command

I am using the following Command to delete Directory with contents. But this command is deleting inside files only not directories. is there any change need in my command? find -type f -mtime +3 -exec rm -r {} \; Thanks (3 Replies)
Discussion started by: bmkreddy
3 Replies
Login or Register to Ask a Question