Clobbering


In software engineering, clobbering a file or computer memory is overwriting its contents. The Jargon File defines clobbering as

POSIX

Overwrites in POSIX often happen unintentionally, such as using the > redirection operator. To prevent unintentional clobbering, various means can be used. For example, setting the shell parameter set -o noclobber or set noclobber will prevent > from clobbering by making it issue an error message instead:

$ echo "Hello, world" >file.txt
$ cat file.txt
Hello, world
$ echo "This will overwrite the first greeting." >file.txt
$ cat file.txt
This will overwrite the first greeting.
$ set -o noclobber
$ echo "Can we overwrite it again?" >file.txt
-bash: file.txt: cannot overwrite existing file
$ echo "But we can use the >| operator to ignore the noclobber." >|file.txt
$ cat file.txt # Successfully overwrote the contents of file.txt using the >| operator
But we can use the >| operator to ignore the noclobber.
$ set +o noclobber # Changes setting back

The default behavior of the mv and cp commands is to clobber their destination file if it already exists. This behavior may be overridden by invoking or aliasing the commands with the -i switch, causing the commands to prompt the user before overwriting the destination file.

Makefiles

In makefiles, a common target clobber means complete cleanup of all unnecessary files and directories produced by previous invocations of the make command. It is a more severe target than clean and is commonly used to uninstall software. Some make-related commands invoke "make clobber" during their execution. They check the CLOBBER environment variable. If it is set to OFF then clobbering is not done.

Assembly

In assembler programming, the term 'clobbered registers' is used to denote any registers whose value may be overwritten during the course of executing an instruction or procedure.