How can I copy files with duplicate filenames into one directory and retain both files by having the duplicate(s) rename automatically?

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

Found this on superuser:

#!/bin/bash
cp -vn "$1" "$2"/ || cp -vn "$1" "$2"/"${1##*/}"~"$(md5sum "$1" | cut -f1 -d' ')"

The file that has the same name gets renamed to the file with the md5sum added to the name. If you save it to a filename like "saveCopy" you can use find like this to execute it:

find . -name 'z*.jpg' -exec ./saveCopy {} /tmp/Extracted/ \;

For more on this see the link.

Tags:

Files

14.04