Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In a GUI environment, a drag-and-drop with replace will replace files and entire directories (including contents) with whatever is being copied in. Is there a way to accomplish this same intuitive result with the 'mv' command?

share|improve this question
add comment

4 Answers

up vote 12 down vote accepted

Not with mv.

The core function of mv (despite its name) is to rename an object. One of the guarantees that UNIX makes is that renames are atomic -- you're never allowed to see a partially completed rename. This guarantee can be very useful if you want to change a file (/etc/passwd, for example) that other programs may be looking at, and you want them to see either the old or new version of the file, and no other possibility. But a "recursive rename" like you describe would break that guarantee -- you could stop it in the middle and you'd have a half-moved tree and probably a mess -- and so it doesn't really fit in with the philosophy of mv. That's my guess as to why mv -r doesn't exist.

(Never mind that mv breaks that philosophy in other, smaller ways. For example, mv actually does a cp followed by rm when moving files from one filesystem to another.)

Enough philosophy. If you want to recursively move ("drag-drop") a tree from one place to another on the same filesystem, you can get the efficiency and speed of mv as follows (for example):

cp -al source/* dest/ && rm -r source/*

The -l flag to cp means "create a hard link instead of copying" -- it's effectively creating a new filename that points to the same file data as the old filename. This only works on filesystems that support hard links, though -- so any native UNIX-ish filesystem is fine, but it won't work with FAT.

The && means "only run the following command if the preceding command succeeded". If you like, you can run the two commands one at a time instead.

share|improve this answer
    
Extremely informational. Thank you very much! –  user7089 May 24 '12 at 14:36
add comment

mv -f /path/to/source/folder/* /destination/folder/

Will move everything in /path/to/source/folder, including files and directories to /destination/folder.

And will overwrite existing files and directories.

share|improve this answer
1  
Only empty directories –  Kevin May 23 '12 at 20:05
add comment

I don't think you can replicate the drag-drop behavior you're describing with mv, as non-empty subdirectories in the target won't be replaced.

Perhaps rsync? Something like rsync -a -r source/ target/? Run with the -v -n to do a verbose dry-run first to make sure it does what you want.

share|improve this answer
add comment

You're probably going to want to change the correct answer to this:

https://github.com/iaindooley/pickdrop

Example:

say I have:

test/
test/index.php
test/images/
test/images/a.jpg
test/images/thing.png

and I want to move these things to /site

so it looks like:

site/
site/public/
site/public/index.php
site/public/a.jpg
site/public/thing.png

I can go:

cd images &&
pick a.jpg thing.png
cd .. &&
pick index.php &&
cd .. &&
mkdir site &&
mkdir site/public &&
cd site/public &&
drop

This is literally cut & paste.

share|improve this answer
    
Very cool, and definitely worth looking into. However, the question was specifically about 'mv' so I think I'll leave the correct answer as the one that explains the philosophy behind mv in detail. Thank you for this contribution though, definitely very helpful. –  user7089 May 25 '12 at 16:29
    
Also notable, from the readme: "It's not robust at all - it stores your picks in ~/.pick until you call drop at which time it uses the file paths in ~/.pick to create a new executable ~/.drop file to copy each of the files you picked to the current working directory." –  user7089 May 26 '12 at 16:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.