# How to overwrite (wipe) a file?



## longtick (Aug 15, 2013)

AFAIK UFS is not copy on write. How would I go about overwriting a file in it? I have googled but I can't seem to find anything related. Namely I want to write new data on top of the old data, like it is possible with Linux's ext2, where simply doing `$ echo xxxxxxxxxxxxx > file` overwrites the actual bytes instead of allocating a new region, write the data there, move pointers around, invalidate the old region, etc., on-disk-consistency etc.

The file contains GELI keys for file systems on the machine; I usually keep the keys off the host for security and when they are needed I put them in a memory-backed file system so they never touch the local disk but this time it was not possible that way. I want to make sure its contents are overwritten before I simply remove it.


----------



## J65nko (Aug 15, 2013)

There are some caveats but quoting rm(1):


```
-P           Overwrite regular files before deleting them.  Files are over-
	     written three times, first with the byte pattern 0xff, then 0x00,
	     and then 0xff again, before they are deleted.  Files with multi-
	     ple links will not be overwritten nor deleted and a warning will
	     be issued.  If the -f option is specified, files with multiple
	     links will also be overwritten and deleted.  No warning will be
	     issued.

	     Specifying this flag for a read only file will cause rm to gener-
	     ate an error message and exit.  The file will not be removed or
	     overwritten.

	     N.B.: The -P flag is not considered a security feature (see
	     BUGS).
[snip]

[b]BUGS[/b]
     The -P option assumes that the underlying storage overwrites file blocks
     when data is written to an existing offset.  Several factors including
     the file system and its backing store could defeat this assumption.  This
     includes, but is not limited to file systems that use a Copy-On-Write
     strategy (e.g. ZFS or UFS when snapshots are being used), Flash media
     that are using a wear leveling algorithm, or when the backing datastore
     does journaling, etc.  In addition, only regular files are overwritten,
     other types of files are not.
```


----------



## SirDice (Aug 15, 2013)

longtick said:
			
		

> How would I go about overwriting a file in it? I have googled but I can't seem to find anything related.


Have a look at [cmd=]rm -P[/cmd], see rm(1).


----------



## longtick (Aug 15, 2013)

Great. Of course, rm(1), thanks!

@J65nko: of course you mean rm.


----------

