LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 10-31-2006, 03:56 PM   #1
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Rep: Reputation: 21
How do I move files without overwrite exitng files?


There is option in mv to move files only if there newer with the -u --update option.

But I don't want to overwrite files even if they old.

which command do it?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-31-2006, 04:15 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
"mv" is really maybe better thought of as a renaming command.

I'm fairly sure this is how it works:

When you "move" a file into another directory, you are not actually moving the bytes on the disk. Rather, you are changing how it is associated with the directory tree.
So:
"mv filename newdir/filename" can really be thought of as "renaming" the file to associate it with another directory.

If "newdir/filename" already exists, then just do this:
"mv filename /newdir/filename_a"

You can also of course use mv to rename in the same directory, e.g.:
mv filename filename_a
 
Old 10-31-2006, 04:24 PM   #3
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 545

Rep: Reputation: 30
Well 'mv' only acts as a renaming command if you are moving it to the same hard disk partition, correct? When I mv a file from one hard drive to another, that's a physical transaction that takes place. Same thing for when I move a file mounted on a CD to some place in my home directory.


Anyway, back to the original question I'm not sure if there's a command that does that. You could make your own of course! Just write a shell script that tests if the destination for the move already exists and if it doesn't, call the mv command. You could call it safemv or something.
 
Old 10-31-2006, 04:30 PM   #4
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Original Poster
Rep: Reputation: 21
But I don't want to move file if it exist or rename it.

I want to do it for my installing script in LFS to install with DESTDIR to directory and to move it files to the real location.

therefore I don't want to overwrite existing files.
 
Old 10-31-2006, 04:47 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Quote:
Originally Posted by R00ts
Well 'mv' only acts as a renaming command if you are moving it to the same hard disk partition, correct? When I mv a file from one hard drive to another, that's a physical transaction that takes place. Same thing for when I move a file mounted on a CD to some place in my home directory.
Touche!! I keep forgetting that....
 
Old 11-01-2006, 01:24 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,543

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
You can use the the -i option which will ask if you want to overwrite if the file exists already. Of course you will not want to type n <enter> a billion times, so use
Code:
yes|tr y n|mv -i files dest
The yes command outputs y's, then use tr to change the y's to n's, since there doesn't seem to be a no command.
 
2 members found this post helpful.
Old 11-02-2006, 05:41 PM   #7
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Original Poster
Rep: Reputation: 21
Thanks but for some curiosity since it's small program I check its source code.

The y is only a default but you can get it any word:
Code:
  program_name = argv[0];
so I just write "yes n" and it is work.
 
3 members found this post helpful.
Old 11-03-2006, 01:07 PM   #8
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
star (Joerg Schilling's version of tar) does not overwrite existing files by default, so you could tar them up and untar them, e.g.

Code:
star -c <files> | star -x C=/dest/dir
which would probably be more efficient than mv'ing every file individually. You can get it here.

Last edited by soggycornflake; 11-03-2006 at 01:11 PM.
 
Old 11-03-2006, 02:22 PM   #9
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Original Poster
Rep: Reputation: 21
but It's do more work than just moving files.

star will archive them and then extract them one by one to its new place.

It's always do one after one, but you can't see in the command the move action and it's occure inside the star.
 
Old 11-03-2006, 02:48 PM   #10
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
Quote:
Originally Posted by nadavvin
but It's do more work than just moving files.

star will archive them and then extract them one by one to its new place.

It's always do one after one, but you can't see in the command the move action and it's occure inside the star.
Yes, it but spawns two instances of star, rather than 1 instance of mv for each file (hundreds? thousands?). In general (i.e. the src and dest are on different partitions), it will be much more efficient.

However, if you're moving the files to a directory on the same partition, then obviously you want to avoid shifting the actual file data anywhere, in which case something like:

Code:
for f in $(find /src/dir -type f); do target=/dest/dir/$(basename $f); if [[ ! -e $target ]]; then mv $f $target; fi; done
should suffice.

edit:
If you have nested subdirectories, you might need to throw in a

Code:
mkdir -p $(dirname $target)
before the mv command.

Last edited by soggycornflake; 11-03-2006 at 02:52 PM.
 
Old 11-03-2006, 03:49 PM   #11
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Original Poster
Rep: Reputation: 21
thanks.

I guess this will work:

Code:
for f in $(find /src/dir -type f); do target=/dest/dir/$(basename $f); if [[ ! -e $target ]]; then mkdir -p $(dirname $target);mv $f $target; fi; done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot using FTP move command to move files adrianmak Linux - Networking 4 04-21-2009 01:01 PM
Cannot overwrite files on NFS mount? dirdej Linux - Networking 6 03-05-2006 07:46 AM
the best way to move files around..... thegreggwong Linux - Software 5 08-28-2004 06:20 PM
Lost files during move. spacedoubtman General 3 03-12-2004 04:00 AM
Overwrite ftp files running on RH9 sanjit Red Hat 2 08-29-2003 11:13 AM


All times are GMT -5. The time now is 11:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration