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.

88 views

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:

  1. Understanding file permissions in Unix / Linux based systems, and how to make files immutable.
  2. How to search a directory and subdirectories for a given string of text in Linux with the grep command.
  3. Worth knowing in order to make a bootable USB memory stick with Windows on from Linux.
  4. This is why I decided to encrypt my new Flash Drive, and why I think you should too.
  5. About the problem with using sudo with graphical programs in Linux.

More in: Linux Tutorials