19

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?

αғsнιη
  • 35,660

4 Answers4

23

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
David Foerster
  • 36,264
  • 56
  • 94
  • 147
  • Thanks for the suggestion to use a different file manager. Unfortunately, Thunar also doesn't have the option that I'm referring to. It just has: Cancel, Skip All, Skip, Replace, Replace All.

    Will try out Dolphin.

    – heisenbergman Oct 19 '14 at 11:12
  • 2
    Dolphin works great for this :) – heisenbergman Oct 19 '14 at 13:05
  • Thanks for trying out the two. I'll update my question according to your findings. – David Foerster Oct 19 '14 at 13:11
  • @heisenbergman, Hi what did you mean by Dolphin works great for this? It does offer the option to rename, but it still only allows me to rename the files one by one. Unfortunately the "apply to all" option cannot be used with the option to rename. This is quite useless for a large amount of files. Is this just my version? Did you manage to rename all your doubles at once (like you can in Windows), using Dolphin? – Kvothe Feb 27 '18 at 13:46
  • The last part about appending number IDs to each file if it already exists isn't working for me :( It copies the first file file.json, then I get a file.json.json (using the --suffix=json) and then I get get an error saying it cannot overwrite an existing file. Using Ubuntu 18, piping results from find like this: find ./ -name 'meta.json' | xargs cp --backup=existing --suffix=.json -t ~/data/. I get 3 files as described above, then the error: cp: will not overwrite just-created '/home/user/data/meta.json' with './path/to/meta.json'. I also tried -I {} placeholders with xargs. – n1k31t4 Apr 20 '20 at 11:18
  • 1
    @n1k31t4 Please read the description of --backup=existing again. Hint: What happens in the following case: touch foo bar; cp -v --backup=numbered foo bar; cp -v --backup=existing foo bar? – David Foerster Apr 21 '20 at 11:59
  • Can anyone explain whether Dolphin really has the capability to rename duplicates "automatically" , i.e. you can apply it to all duplicates at once (adding something like (1) to the duplicates like in Windows.) I tested it (a while ago) and found I could not. Am I misunderstanding this answer? I don't understand how this answer was accepted and so upvoted if it really does not work. Note that the "automatically" is part of the title and I think the important thing. If you have to give the new name for each file it is not automatic. – Kvothe Aug 09 '21 at 09:47
2

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.

Rinzwind
  • 299,756
0

There was a solution (ultracopier) to this question in this forum before: see https://ubuntuforums.org/showthread.php?t=2251859 According to that discussion, it can eb integrated into Nautilus.

0

Copy this script to the top directory, make it executable and run it:

#!/bin/bash

## Get a list of all files
list=$(find . -mindepth 2 -type f -print)
nr=1

## Move all files that are unique
find . -mindepth 2 -type f -print0 | while IFS= read -r -d '' file; do
    mv -n $file ./
done
list=$(find . -mindepth 2 -type f -print)

## Checking which files need to be renamed
while [[ $list != '' ]] ; do
   ##Remaming the un-moved files to unique names and move the renamed files
   find . -mindepth 2 -type f -print0 | while IFS= read -r -d '' file; do
       current_file=$(basename $file)
       mv -n $file "./${nr}${current_file}"
   done
   ## Incrementing counter to prefix to file name
   nr=$((nr+1))
   list=$(find . -mindepth 2 -type f -print)
done
warhansen
  • 222