Skip to main content
added 128 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
grep -rlZ --exclude-dir=dir1 --exclude-dir=dir2 HUMAN . | xargs -r0 rm -f 

If you want find to do the directory traversal:

find . -type d \( -name dir1 -o -name dir2 \) -prune -o \ -type f -exec grep -lZ HUMAN {} + | xargs -r0 rm -f 

Portably/standardly, that would have to be:

find . -type d \( -name dir1 -o -name dir2 \) -prune -o \ -type f -exec grep -q HUMAN {} \; -exec rm -f {} + 

but that means running one grep per file.

For --exclude-dir={dir1,dir2} to work, you need a shell with brace expansion support like csh, tcsh, ksh, zsh, bash or fish.

grep -rlZ --exclude-dir=dir1 --exclude-dir=dir2 HUMAN . | xargs -r0 rm -f 

If you want find to do the directory traversal:

find . -type d \( -name dir1 -o -name dir2 \) -prune -o \ -type f -exec grep -lZ HUMAN {} + | xargs -r0 rm -f 

Portably/standardly, that would have to be:

find . -type d \( -name dir1 -o -name dir2 \) -prune -o \ -type f -exec grep -q HUMAN {} \; -exec rm -f {} + 

but that means running one grep per file.

grep -rlZ --exclude-dir=dir1 --exclude-dir=dir2 HUMAN . | xargs -r0 rm -f 

If you want find to do the directory traversal:

find . -type d \( -name dir1 -o -name dir2 \) -prune -o \ -type f -exec grep -lZ HUMAN {} + | xargs -r0 rm -f 

Portably/standardly, that would have to be:

find . -type d \( -name dir1 -o -name dir2 \) -prune -o \ -type f -exec grep -q HUMAN {} \; -exec rm -f {} + 

but that means running one grep per file.

For --exclude-dir={dir1,dir2} to work, you need a shell with brace expansion support like csh, tcsh, ksh, zsh, bash or fish.

Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

grep -rlZ --exclude-dir=dir1 --exclude-dir=dir2 HUMAN . | xargs -r0 rm -f 

If you want find to do the directory traversal:

find . -type d \( -name dir1 -o -name dir2 \) -prune -o \ -type f -exec grep -lZ HUMAN {} + | xargs -r0 rm -f 

Portably/standardly, that would have to be:

find . -type d \( -name dir1 -o -name dir2 \) -prune -o \ -type f -exec grep -q HUMAN {} \; -exec rm -f {} + 

but that means running one grep per file.