4

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 
1

3 Answers 3

19

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.

5
  • 7
    this has to be the stupidest question on the site. thanks. Commented Mar 10, 2012 at 2:13
  • 12
    There is nothing wrong with not knowing things. Everyone starts out from zero. Commented Mar 10, 2012 at 2:17
  • 1
    “There is no shame in not knowing; the shame lies in not finding out.” -- Russian Proverb Commented Mar 10, 2012 at 15:04
  • 1
    Ooooh. I've never had the redirect operators explained in that way, comparatively. I've been a casual linux user for years (not full time, obviously), and never learned properly how to use the < operator. Now I know, and this answer is what did it. @kuchnahi, even the veterans and long-time users are learning new things, every day. Don't sweat it. :) Commented Mar 12, 2012 at 17:55
  • Still, surely it would make some sense to read the documentation for the language features you use, no? Commented Nov 25, 2014 at 9:35
12

While you make yourself familiar with I/O redirection, you might find it "safer" to enable noclobber shell setting. This prevents unintentional clobbering of your files. See your shell man page and http://en.wikipedia.org/wiki/Clobbering

2
  • +1 great tip, and this is something I do, even with many years of experience. Typos happen, and this helps. Commented Mar 10, 2012 at 14:34
  • I still have to think twice with < and >. Thanks for the tip. Commented Mar 15, 2012 at 20:33
6

These commands have clobbered the text file because you told it to (> file will truncate any existing file before writing to it). You are probably looking for <, which means "redirect standard input from here".

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.