bash - How to remove files using grep and rm? -


grep -n magenta *| rm * 

grep: a.txt: no such file or directory

grep: b: no such file or directory

above command removes files present in directory except ., .. . should remove files contains word "magenta"

also, tried grep magenta * -exec rm '{}' \; no luck. idea?

use xargs:

grep -l --null magenta ./* | xargs -0 rm 

the purpose of xargs take input on stdin , place on command line of argument.

what options do:

  • the -l option tells grep produce filenames without matching text.

  • the --null option tells grep separate filenames nul characters. allows manor of filename handled safely.

  • the -0 option xargs treat input nul-separated.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -