Jay Davis
Jay Davis

Reputation: 61

Grep exclude folder and search using linux

grep command

if I am in

/var/

I want to search /var/www

recursively

but not

/var/www/exclude

Upvotes: 4

Views: 5299

Answers (4)

praveen
praveen

Reputation: 1

Use --exclude=*.log* to skip files containing the word 'log'.

Upvotes: 0

Gyscos
Gyscos

Reputation: 1842

The option --exclude-dir=exclude might work.

It has some limitations, though : it will ignore all "exclude" folders, not just /var/www/exclude.

grep -r --exclude-dir=exclude pattern /var/www/

if you're already in var, then you can of course just use www :

grep -r --exclude-dir=exclude pattern www/

Upvotes: -1

xeor
xeor

Reputation: 5455

You can also use grep -v "/foldername/". -v takes away all matches.

grep -r string /var/www/ | grep -v "/exclude/"

Upvotes: 7

Pirooz
Pirooz

Reputation: 1278

1) Use grep -R -f file.txt, where in file.txt you list all the file and directory names, except /var/www/exclude

2) use the following bash script:

 for i in $( ls /var/www/ ); do
   if [ "$i" != "/var/www/exclude" ] ; then
      grep -R "my search term" $i
   fi
 done

Upvotes: -1

Related Questions