Preventing Text File Destruction in Shell – Common Mistakes

io-redirectionshelltext processing

I have destroyed a bunch of non-essential files and I don't know why. I have been executing commands like:

tr -sc 'A-Za-z' '\n' > somefile.txt | less

there is no output (blank page with flashing END) and upon checking all the content from the file is erased.

Another command that erased a full text file

grep someword > someotherfile.txt  | less

Best Answer

The > operator means "take the output of the command, truncate the named file, and then write the output of the command to that.

Reading that command line I guess you want <, which is "read standard input from this file, and feed it to the command" instead.

Related Question