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.

1 Answer 1


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? May 15, 2017 at 17:57
  • if grep -q '#new' "c:/"; then cp "c:/" "/location/of/outputfile" fi would work? May 15, 2017 at 17:58
  • Have a look here: unix.stackexchange.com/questions/297006/…
    – ctx
    May 15, 2017 at 18:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .