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? |
"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. |
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. |
Quote:
|
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 |
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]; |
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 |
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. |
Quote:
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 edit: If you have nested subdirectories, you might need to throw in a Code:
mkdir -p $(dirname $target) |
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. |