I'm moving a file to a different folder and would like to add some kind of index to the newly moved file if a file with the same name exists already (the old one should remain untouched). For example, if file.pdf existed I would prefer something like file1.pdf or file_1.pdf for the next file with the same name.

Here I've found a variant for the opposite idea — but I don't want to make a "backup".

Does mv have some parameters out of the box for that scenario? I use Ubuntu Linux.

  • 2
    That is exactly what the solution in your linked question's answer does. Have you tried it? – Byte Commander Jun 15 '17 at 19:40
  • It gave me something like this: file.pdf and file.pdf.~1~ - I don't know what to think of it – Qohelet Jun 15 '17 at 19:48
  • file.pdf already existed in the destination directory. file.pdf.~1~ is the first file moved that would have klobbered file.pdf if it had not been renamed. – DopeGhoti Jun 15 '17 at 19:52
  • Doesn't it actually add the suffix to the original? – steeldriver Jun 15 '17 at 19:54
  • I'd prefer something like file1.pdf or file_1.pdf – Qohelet Jun 15 '17 at 19:56

As the answer to the question you linked already states, mv can suffix files that would otherwise get overwritten by the file you move with a number to give them a unique file name:

mv --backup=t <source_file> <dest_file>

The command works by appending the next unused number suffix to the file that was first in the destination directory. The file you are moving will keep its original name.

However, this will appends suffixes like .~1~, which seems to be not what you want:

$ ls

You can rename those files in a second step though to get the names in a format like file_1.pdf instead of file.pdf.~1~, e.g. like this:

rename 's/((?:\..+)?)\.~(\d+)~$/_$2$1/' *.~*~

This takes all files that end with the unwanted backup suffix (by matching with the shell glob *.~*~) and lets the rename tool try to match the regular expression ((?:\..+)?)\.~(\d+)~$ on the file name. If this matches, it will capture the index from the .~1~-like suffix as second group ($2) and optionally, if the file name has an extension before that suffix like .pdf, that will be captured by the first group ($1). Then it replaces the complete matched file name part with _$2$1, inserting the captured values instead of the placeholders though.

Basically it will rename e.g. file.pdf.~1~ to file_1.pdf and something.~42~ to something_42, but it can not detect whether a file has multiple extensions, so e.g. archive.tar.gz.~5~ would become archive.tar_5.gz

  • "Files that were originally present in the destination folder do not get modified in any way" - sure about that? try echo 'This is foo' > foo ; echo 'This is bar' > bar ; mv --backup=t foo bar ; cat bar (it's the original bar that becomes bar.~1~) – steeldriver Jun 15 '17 at 22:20
  • @steeldriver Oops, you're right. That's quite counter-intuitive behaviour in my opinion though. Editing... – Byte Commander Jun 15 '17 at 22:30
  • Yeah I don't know the history of why it was done that way – steeldriver Jun 15 '17 at 22:45
  • 2
    also --backup=numbered can be used instead of --backup=t – Baard Kopperud Jun 16 '17 at 20:51

While the simple substitution-based renaming of files in Linux is relatively facile, something to note on Linux distributions is that there are multiple rename command packages, as noted here:


Complicating this issue are more complex file renaming approached involving regex (regular expressions) commands.

The rename solution provided by @ByteCommander did not work for me; here are the specifics and a solution (I'm on Arch Linux).

$ which rename

$ rename --version
  rename from util-linux 2.36.1

$ pacman -Ss util-linux | grep installed
  core/util-linux 2.36.1-4 [installed]
  core/util-linux-libs 2.36.1-4 [installed]

$ cd /home/victoria/zzz/
$ pwd; ls -l
  total 16
  -rw-r--r-- 1 victoria victoria 5 Dec 17 09:17 'apples and bananas.mkv'
  -rw-r--r-- 1 victoria victoria 5 Dec 17 09:17 'apples and bananas.mkv.~123~'
  -rw-r--r-- 1 victoria victoria 5 Dec 17 09:17  apples.mkv
  -rw-r--r-- 1 victoria victoria 5 Dec 17 09:17  apples.mkv.~123~

## Note the use of bacticks ( ` ); for readability I split the `for` 
## command over two lines; however, it is a one-line command):

$ for f in *; do mv 2>/dev/null -v "$f" "`echo $f |
  sed -r 's/(.*)\.(.*)\.~([0-9]{1,})~$/\1_\3.\2/'`"; done

  renamed 'apples and bananas.mkv.~123~' -> 'apples and bananas_123.mkv'
  renamed 'apples.mkv.~123~' -> 'apples_123.mkv'

$ ls -l
  total 16
  -rw-r--r-- 1 victoria victoria 5 Dec 17 09:17  apples_123.mkv
  -rw-r--r-- 1 victoria victoria 5 Dec 17 09:17 'apples and bananas_123.mkv'
  -rw-r--r-- 1 victoria victoria 5 Dec 17 09:17 'apples and bananas.mkv'
  -rw-r--r-- 1 victoria victoria 5 Dec 17 09:17  apples.mkv



Ok, don't judge me by my not poor bash-skills but this solution worked for me. (I appreciated Byte Commanders solution yet it's due to its restrictions not what I was looking for)

So here's my small script that does the job...

  • $2 is the path
  • $3 the filename
  • $punktpdf is just ".pdf"

So here's my little script...

if [ -s $2$3$punktpdf ]; then
    for i in `seq 1 100000`;
            if [ ! -s $2$3$i$punktpdf ]; then
                if mv scan.pdf $2$3$i$punktpdf; then                        
                    echo $3$i$punktpdf
                    exit 0
                    echo 1
        #Regular mv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.