LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How do I move files without overwrite exitng files? (https://www.linuxquestions.org/questions/linux-general-1/how-do-i-move-files-without-overwrite-exitng-files-497301/)

nadavvin 10-31-2006 02:56 PM

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?

pixellany 10-31-2006 03:15 PM

"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

R00ts 10-31-2006 03:24 PM

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.

nadavvin 10-31-2006 03:30 PM

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.

pixellany 10-31-2006 03:47 PM

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....

ntubski 11-01-2006 12:24 AM

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.

nadavvin 11-02-2006 04:41 PM

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.

soggycornflake 11-03-2006 12:07 PM

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.

nadavvin 11-03-2006 01:22 PM

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.

soggycornflake 11-03-2006 01:48 PM

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.

nadavvin 11-03-2006 02:49 PM

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


All times are GMT -5. The time now is 04:40 AM.