mv --no-clobber option | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

mv --no-clobber option

Shell Programming and Scripting


Tags
move, no-clobber

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-16-2009
Altay_H Altay_H is offline
Registered User
 
Join Date: Dec 2008
Last Activity: 21 January 2010, 7:25 AM EST
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
mv --no-clobber option

Hi,
I'm writing a script in which I need to move files from one directory to another. However, some of the files have the same name, and I want to keep the older file, so I can't use --update. I'm moving files from the newer directory to the older directory, so what I'm looking for is a way to automatically not overwrite. Basically, I need the behavior of mv with the opposite of --force option. I can't use the --interactive option either, because I'm copying multiple files and I don't want mv to hang.

There's no reason I must use mv, I just assumed it'd be the easiest way to accomplish what I need. If there's an easier way that doesn't involve mv I'm open for suggestions.

After searching around a while I found this recent webpage which makes it seem as though mv will now have a --no-clobber option which will do exactly what I need. I'm running Ubuntu on this computer, so I'm sure the webpage is relevant, but mv doesn't like --no-clobber despite the fact that my system is updated.

So basically what I need is explained in the first paragraph. I want a script to move files from one directory to another and automatically NOT overwrite: the oppsite of --force.

Thanks in advance for any help or suggestions!
Sponsored Links
    #2  
Old 01-16-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts

Code:
dest=/path/to/destination/directory
mv -fb * "$dest"
for f in "$dest"/*~
do
   mv -f "$f" "${f%.*}"
done

Sponsored Links
    #3  
Old 01-17-2009
Altay_H Altay_H is offline
Registered User
 
Join Date: Dec 2008
Last Activity: 21 January 2010, 7:25 AM EST
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, but it didn't work...

Thanks for the help, but I couldn't get it to work. I'm assuming your script is meant to be run from the "from" directory and that /path/to/destination/directory is meant to be replaced by the path to the "to" directory.

Unfortunately, when I run the script, not only are the files in the destination directory overwritten by the files from the origin directory, but the copies in the origin directory are also deleted. That's more or less the exact opposite of the desired behavior.

I need the copies in the destination directory to never be overwritten. I also would like to leave the duplicate copies in the original directory. Below is an example of the desired behavior:


Code:
$ ls -a ~/from
file1.ext
file2.ext
$ cat ~/from/file1.ext
original file
$ ls -a ~/to
file1.ext
$ cat ~/to/file1.ext
destination file
$ ~/magical_script.sh
$ ls -a ~/from
file1.ext
$ cat ~/from/file1.ext
original file
$ ls -a ~/to
file1.ext
file2.ext
$ cat ~/to/file1.ext
destination file

I hope that clarifies the desired behavior. As you can see, duplicates are left alone; only files that are not in the destination directory are copied. It doesn't look to me like what you posted should accomplish that considering you're using mv with a --force option.
    #4  
Old 01-17-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts


Code:
fromdir=/path/to/original/files
destdir=/path/to/destination/directory

cd "$fromdir" || exit 1

for file in *
do
  [ -f "$destdir/$file" ] || mv "$file" "$destdir"
done

Sponsored Links
    #5  
Old 01-17-2009
Altay_H Altay_H is offline
Registered User
 
Join Date: Dec 2008
Last Activity: 21 January 2010, 7:25 AM EST
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks! It worked.

Thank you. That did exactly what I needed.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
g++ and the -R option eternalflame Programming 0 04-14-2008 01:57 PM
option followed by : taking next option if argument missing with getopts gurukottur Shell Programming and Scripting 2 03-17-2008 12:46 PM
-n option ravi raj kumar Shell Programming and Scripting 1 01-03-2008 08:20 AM
cc option kuldeep_bora Programming 4 06-02-2006 09:38 AM
cut -f option 435 Gavea UNIX for Dummies Questions & Answers 1 11-10-2003 04:50 PM



All times are GMT -4. The time now is 02:13 AM.