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'm a careless terminal driver scared of accidentally deleting files, hence using some aliases like alias rm='rm -i' for rm, mv, cp. How can I get a similar confirmation behavior for file redirections (e.g echo "I'm silly" > very_important_file.txt).

The common case is that I usually use replace (>) instead of append (>>) and so I ended up accidentally deleting some mid-important files. What are your suggestions?

share|improve this question
2  
Aliasing in -i options and setting noclobber are temporary workarounds, but use them with caution - if they just encourage you to be more careless you'll run into trouble the day one of those settings gets reset or you have to use someone else's system. If you plan to spend any non-trivial amount of time in the terminal, a better protection is to train yourself to think about what you are doing before you press the enter key. I've learned my lessons from accidental terminal goofs, but most of them were not serious because I had backups. –  jw013 Aug 17 '12 at 15:52
1  
nice advice indeed, extra security is always welcome though –  gokcehan Aug 17 '12 at 15:55
    
@jw013 Training yourself to being a good driver is no excuse for not wearing a seatbelt. Losing data because you purposefully didn't turn on noclobber or cp -i is irresponsible. –  Gilles Aug 17 '12 at 23:07
    
@Gilles Beware the fallacy of over-stretching analogies. -i and noclobber are nothing like seatbelts because they change the way basic commands work, whereas fastening a seatbelt doesn't cause your car to refuse to go into reverse or ask for confirmation every time you step on the accelerator. (... continued ...) –  jw013 Aug 17 '12 at 23:41
    
@Gilles Your notion of irresponsibility also seems wrong. I actively fasten seat belts because 99% of the cars I've been in don't automatically fasten it for you. I actively use -i when appropriate, and never assume that the system has it aliased, nor do I advocate getting into the habit of assuming so. The way I see it, blindly assuming the car will fasten your seatbelt for you (or the system has noclobber set) is much more irresponsible. –  jw013 Aug 17 '12 at 23:44

1 Answer 1

up vote 6 down vote accepted

I don't think there's a way to get the exact behavior of -i, but I have noclobber set which prevents overwriting already existing files.

See this page for a usage example.

You can try out the command like this (and if you like it, include it in your startup file)

$ set -o noclobber

Example:

$ ls > ls.out
$ set -o noclobber
$ ls > ls.out
bash: ls.out: cannot overwrite existing file
$

Update:

As @jsbillings mentions in a helpful comment below, to override the noclobber in bash one can use >|

Since I primarily use tcsh (a csh variant), the override operator is >!

share|improve this answer
    
just what I need, thanks.. –  gokcehan Aug 17 '12 at 15:49
    
@gokcehan glad it helped. –  Levon Aug 17 '12 at 15:50
2  
If you have noclobber set, and you do want to clobber the file without having to change settings, bash has a >| redirect that ignores the noclobber setting. –  jsbillings Aug 17 '12 at 18:29
    
Just to add to this, the equivalent csh/tcsh override would be using >! –  Levon Aug 17 '12 at 23:39
    
@jsbillings Thanks for the comment, I paraphrased it in an update to my answer (I hope you are ok with that) and also added information on how to do the same with csh/tcsh –  Levon Aug 17 '12 at 23:43

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.