Prevent Overwriting Files When Using Redirection


There's a way to prevent overwriting files when redirecting, howeverthe noclobber option. If you set noclobber to on, bash won't allow redirection to overwrite existing files without your explicit permission. To turn on noclobber, use this command:

$ set -o noclobber 


At that point, if you want to use redirection and overwrite a file, use >| instead of just >, like this:

$ pwd /home/scott/music $ ls -1F Hank_Mobley/ hank_mobley.txt Horace_Silver/ John_Coltrane/ $ ls -1F Hank_Mobley/* > hank_mobley.txt ERROR $ ls -1F Hank_Mobley/* >| hank_mobley.txt $ cat hank_mobley.txt 1958_Peckin'_Time/ 1960_Roll_Call/ 1960_Soul_Station/ 1961_Workout/ 1963_No_Room_For_Squares/ 


If you decide you don't like or need noclobber, you can turn it off again:

$ set +o noclobber 


To permanently turn on noclobber, you need to add set -o noclobber to your .bashrc file.



Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net