okay so i know of grep -rhi, which extrats the line if string is found, but I want to copy the entire file so for example,

file.txt >

isexample #new
ieamama #new

now if I used grep -rhi "#new", it would extract only

isexample #new
ieamama #new

where as I want all the contents within the file including


How would I do this?

Of course the .txt file must contain #new in either one of its lines.


You have to check the return value of grep:

if grep -q '#new' "/path/to/inputfile"; then
  cp "/path/to/inputfile" "/location/of/outputfile"
  • what if i wanted to run it on C:/ for example meaning there would be multiple text files with the same string? – Thomas Bishop May 15 '17 at 17:57
  • if grep -q '#new' "c:/"; then cp "c:/" "/location/of/outputfile" fi would work? – Thomas Bishop May 15 '17 at 17:58
  • Have a look here: unix.stackexchange.com/questions/297006/… – ctx May 15 '17 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.