21

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нιη
  • 36,350

4 Answers4

25

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 according to the following rules:

  • if <FILENAME>.1 exists, use a numbered name to back the original file up, i. e. append a dot (.) followed by the lowest number that doesn't correspond to an existing file,

  • otherwise append the chosen back-up suffix .orig.

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,890
  • 56
  • 97
  • 151
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
  • 309,379
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.

Adalbert Hanßen
  • 823
  • 15
  • 31
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
  • 230