I want to mv files and not overwrite duplicates. The -b switch only makes a single backup file, whereas I may have many.

According to the mv man page:

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

I think I want to use the 'existing, nil' option... but I can't figure out how to call that.

If my command is:

mv $src $dest

How can I implement the 'existing, nil' option?

share|improve this question
    
This answer has a good response about using cp instead of mv: unix.stackexchange.com/a/9902 – Connor Leech Oct 26 '15 at 1:20
up vote 6 down vote accepted

To enable existing or nil (or any of the other options), pass them as values for the --backup option:

mv --backup=existing "$src" "$dest"
mv --backup=nil "$src" "$dest"

Be warned that this does not do what you want:

$ mkdir foo bar
$ cp blah/* foo
$ cp blah/* bar
$ mv --backup=nil bar/* foo/* -t blah
mv: will not overwrite just-created ‘blah/a.jpg’ with ‘foo/a.jpg’
mv: will not overwrite just-created ‘blah/b.ogv’ with ‘foo/b.ogv’
mv: will not overwrite just-created ‘blah/cd ef.JpG’ with ‘foo/cd ef.JpG’

You actually want numbered/t:

$ mv --backup=numbered bar/* foo/* -t blah
$ ls blah 
a.jpg  a.jpg.~1~  a.jpg.~2~  b.ogv  b.ogv.~1~  b.ogv.~2~  cd ef.JpG  cd ef.JpG.~1~  cd ef.JpG.~2~
share|improve this answer
    
that seems really cryptic. What is the backup option for? – Connor Leech Oct 26 '15 at 1:21
    
@ConnorLeech to control the backup settings? – muru Oct 26 '15 at 4:08
mv --backup=existing $src $dest

or

mv --backup=nil $src $dest
share|improve this answer
    
After many attempts, I got the syntax working -- but the nil option did not work. --backup=t does though. THANKS! – BeMy Friend Jan 23 '15 at 19:05
mv --backup=t "$src/$file" "$dest"
share|improve this answer
    
for details see man mv – Elder Geek Jan 23 '15 at 20:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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