Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
1  
You may want to read what is meant by connecting STDOUT and STDIN? –  Gilles Mar 10 '12 at 2:58

3 Answers 3

up vote 19 down vote accepted

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.

share|improve this answer
5  
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

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

share|improve this answer
    
+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".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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