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

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.

  • 7
    this has to be the stupidest question on the site. thanks. – kuch nahi Mar 10 '12 at 2:13
  • 12
    There is nothing wrong with not knowing things. Everyone starts out from zero. – Daniel Pittman Mar 10 '12 at 2: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 9:35

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

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

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.