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
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
kuch nahi
  • 221
  • 3
  • 7

3 Answers3

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.

Daniel Pittman
  • 7,554
  • 2
  • 31
  • 18
  • 7
    this has to be the stupidest question on the site. thanks. – kuch nahi Mar 10 '12 at 02:13
  • 12
    There is nothing wrong with not knowing things. Everyone starts out from zero. – Daniel Pittman Mar 10 '12 at 02:17
  • 1
    “There is no shame in not knowing; the shame lies in not finding out.” -- Russian Proverb – Iain Holder Mar 10 '12 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. :) – Harv Mar 12 '12 at 17:55
  • Still, surely it would make some sense to _read the documentation_ for the language features you use, no? – Lightness Races in Orbit Nov 25 '14 at 09: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

Gowtham
  • 2,003
  • 2
  • 15
  • 14
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".

Chris Down
  • 122,090
  • 24
  • 265
  • 262