I want to write a bash command that greps all *.txt file with a pattern in current folder to another folder. Should I use find or for loop? I tried using find but it seems to complicate things.
Edit: I want to copy files with a specific pattern to a different folder. For example:
A.txt
B.txt
C.txt
all have the word “foo” in them. I want grep to remove “foo” and send it to a different folder with the same name. I don’t want to change the original file in any way.
Using
forwould probably be a lot easier for this thanfind. Something like this:If your
grepdoesn’t understand-qthen:In any case,
grepreturns a true value to the shell if it finds a match and theX && Yconstruct executes theYcommand ifXreturns a true value.UPDATE: The above solution assumes (as noted by Johnsyweb) that you want to remove any lines that contain “foo”. If you just want to remove “foo” without removing whole lines, then
sedis your friend:Or: