Overwrite

if i want to pipe output to a file, say,
cat abc.dat > abc.txt, how do i make it replace the existing file?

the greater than ">" symbol always creates or overwrites
the file ">file_name". The double greater than ">>" will
append to the file.

but if i use '>' in a script and the file already exists, it returns an error message saying that file already exists...

Yes, that is because you can't pipe data out of a file and back into itself as you are trying to do. It gives an error because you can't have a file send its contents to itself, overwriting the data it is ending out. This is not the way file I/O is designed. If you want to replace the original file, you pipe the data to a temporary file. When that operation is over, you then replace the original with the temporary file. If kernel designers allowd file descriptors to act as you are wanted to do with one operation, file systems would be corrupted extremely easily, files operations could be circular and grow without limits, etc. There are complexities with file descriptor management and interprocess pipelining. Simple is good, robust and elegant. What you are hoping to do is better done in two shell operations, not one.

If the error message is:
<I>filename</I>: File exists.

Then it sounds like you are using csh or tcsh as your shell.
In this case you have two options to force the overwriting of a file. Either issue the command "unset noclobber", or use the ">!" form of redirection. The first choice will affect all subsequent redirects in your current login. The second is one time only. You can also put "unset noclobber" into your .cshrc if you want it to take affect every time you login.

nono, i don't want to over write the original file...well i guess what's what I wrote, but I meant if I want to use

cat abc.dat > abc.txt

and abc.txt already exists

it gives me an error saying

abc.txt : file already exists

i'm using ksh

Thanks

In ksh the ">|" redirection overrides the noclobber variable. "unset noclobber" should also work, see above.

OK. Now it is clear. You want to overwrite an existing file with data from another file. Wheeew! I feel better now :slight_smile:

Strange, I never get a 'file exists' error in KSH and my problem is that I sometimes overwrite files I shouldn't have :slight_smile:

Example (as root) in KSH:


# echo hello > a
# echo world > b
# cat b > a
# cat a
world

Interesting. Perhaps you are getting the error because you don't have permissions to write over the file?

what is the noclobber variable? what does it do?

BTW, thx PXT, it worked!

Neo: indeed I have permissions to write over the file as I can delete, append etc...and now with >| i can overwrite : )

thanks everyone!

If you turn on the noclobber option with "set noclobber" then redirecting to an existing file using the > should produce an error. In csh/tcsh this can be overridden on a case-by-case basis with >!, in bash/ksh it is >|

In csh/tcsh you also have the >>! syntax. If noclobber is set, and you use >> to append to a file, you will get an error if the file does <I>not exist</I> (can't append to a non-existant file). If you use >>! you will override this behaviour, and create the file if it does not already exist. I dont believe that bash or ksh have this feature (file is always created if it does not exist).

noclobber is a handy option if you tend to frequently overwrite files that you intended to keep. See the relevant manual page for your shell to learn more about this and other options.

[Edited by PxT on 03-27-2001 at 12:34 AM]