grep: input file is also the output
This error can happen when using grep recursively and saving the result to a file in the same directory.
Edited: 2023-09-26 13:08
grep: ./example.txt: input file is also the output
This safety measure can be triggered by using grep recursively, and directing the result to a file in a location that will also be read by grep.
It probably happens to prevent an infinite loop from writing data repeatedly and filling up the entire hard disk.
E.g. Grepping in a log file location for a specific HTTP error, and saving the output to a file in the same directory:
grep -R '" 421 ' ./ > 421-abusers.log
The grep command is very powerful, but without this security measure, it could potentially be dangerous as users risk filling up their entire hard disk space via infinite loop situations (recursive command use).
To avoid getting yourself into input file is also the output situations, simply direct the output to a file in a different location. E.g:
grep -R '" 421 ' ./ > ~/421-abusers.log
This would instead write the result to a file in your home directory, thereby avoiding infinite grep loops.
Tell us what you think: