How to Copy Files with Duplicate Filenames and Rename Automatically

14.04files

On windows OS, when you copy a file into a directory that already has a file with that name, it asks you whether you want to:

  1. copy the file and replace/overwrite the existing one
  2. cancel copying the new file into the directory
  3. copy the file, but rename it (as something like "filename – copy (1)")

When I do this in Ubuntu, I don't have that 3rd option (which is a lot of times a very useful option). Is there any way to be able to do that in Ubuntu?

Best Answer

Unfortunately Nautilus doesn't have that option.

Option 1: A different file manager

You could try another file manager like Dolphin.

Install Dolphin (requires the Universe repository)

Option 2: Command-line

You can also use the command line program cp(1) with the backup option:

cp --backup -t DESTINATION SOURCE [SOURCE...]

This has the following effects which can be controlled with other options as described in the manual page of cp(1):

--backup[=CONTROL] ― make a backup of each existing destination file

-b ― like --backup but does not accept an argument

-S, --suffix=SUFFIX ― override the usual backup suffix

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

Example

cp --backup=existing --suffix=.orig -t ~/Videos ~/Music/*

This will copy all files in ~/Music to ~/Videos. If a file of the same name exists at the destination, it is renamed by appending .orig to its name as a backup. If a file with the same name as the backup exists, the backup is instead renamed by appending .1 and if that exists as well .2 and so forth. Only then is the source file copied to the destination.

If you want to copy files in subdirectories recursively use:

cp -R --backup=existing --suffix=.orig -t ~/Videos ~/Music
Related Question