If you want to avoid clobbering any backup files with GNU cp
, you can use numbered backups:
cp --backup=t source destination
Rather than overwrite a backup, this creates additional backups.
Example
As an example, let's consider a directory with two files:
$ ls
file1 file2
Now, let's copy file1 over file2:
$ cp --backup=t file1 file2
$ ls
file1 file2 file2.~1~
As we can see, a backup was made.
Let's copy it again:
$ cp --backup=t file1 file2
$ ls
file1 file2 file2.~1~ file2.~2~
Another backup was made.
Documentation
From man cp
, just before the end of the "description" section, the various possible options for --backup
are itemized:
The backup suffix is '~', unless set with --suffix or
SIMPLE_BACKUP_SUFFIX. The version control method may be selected via
the --backup option or through the VERSION_CONTROL environment
variable. Here are the values:
none, off
never make backups (even if --backup is given)
numbered, t
make numbered backups
existing, nil
numbered if numbered backups exist, simple otherwise
simple, never
always make simple backups
As a special case, cp makes a backup of SOURCE when the force and
backup options are given and SOURCE and DEST are the same name for
an existing, regular file.
cp
doesn't have to clobber backups. Use ` cp --backup=t` and it will create numbered backups with no clobbering.-b
option. This is specific to GNU coreutils.