LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   clobbering a directory with mv (https://www.linuxquestions.org/questions/linux-newbie-8/clobbering-a-directory-with-mv-4175440322/)

Xinc 12-06-2012 01:18 PM

clobbering a directory with mv
 
How the heck did I manage it?
I used the command
%mv dir1 /path/to/dir2
with the intent of making dir1 a subdirectory of dir2. That is, so it would end up at
/path/to/dir2/dir1
But instead, mv renamed dir1 to dir2, clobbering everything in dir2. Yet I've been unable to reproduce this behavior. Trying to figure out what went wrong, I've tried
%mv test_dir1 /path/to/test_dir2
numerous times, and it's been working the way I expected and wanted: test_dir1 ends up at /path/to/test_dir2/test_dir1
Can anyone enlighten me?

In the meantime, I've aliased mv to 'mv -nv' to try to avoid future mishaps.

tshikose 12-06-2012 02:11 PM

Code:

mv dir1 /path/to/dir2/

teckk 12-06-2012 02:22 PM

Directory has to exist before you can copy into it.
Code:

mv /path/dir1/* /path/dir2
Put the contents of dir1 into dir2 -n for no clobber

Code:

mkdir /path/dir2/dir1 && mv /path/dir1/* /path/dir2/dir1
Put dir1 into /path/dir2/dir1

David the H. 12-06-2012 02:43 PM

While I'm not sure about why that happened to you, a safer option is usually to use the -t option to explicitly specify the target directory. There's also, of course, the -n option to force no-clobber, or -i for interactive confirmation.

Adding a trailing backslash to the directory name can't hurt either, although I don't know if it has any effect here.

Code:

mv -n -t /path/dir2/ dir1


All times are GMT -5. The time now is 09:44 AM.