How do I move files without overwrite exitng files?
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
"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
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.
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.
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.
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 01:52 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.