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.

Say I have a directory /hello.

Then I do mv /hello /hello2.

Am I moving /hello into /hello2/hello? Or am I moving /hello to /hello2?

It seems that when I do something like this, I get inconsistent results.

share|improve this question
1  
In what case is the result inconsistent? It seems whenever the target directory exists it moves into the target directory, otherwise it gets renamed. –  jmathew Jan 15 '13 at 17:27
    
That is inconsistent. The same command should do the same thing. That means the command is ambiguous. –  Jim Thio Jan 15 '13 at 18:28
    
That's not really inconsistent because it acts consistently within context. Although I can see how it's confusing but the more I think about it, the command has to be that way in order to work for all scenarios. –  jmathew Jan 15 '13 at 18:37
    
I would rather say that's how it is designed and its also logical..if you use mv with files it does the same thing.just renaming.but with mv with directories It's not good to delete the whole directory with subdirectory because of one mv command which is not safe. so it is moved as test1/test2. but if you still want to acheive it write a alias which check whether the parameter given is directory and then do rm -rf the directory and uses the mv command but use it at your own risk. –  harish.venkat Jan 15 '13 at 18:46
    
I suspect your example is based on misremembering something that has happened in the past, based on "It seems that I do something like this". If both /hello and /hello2 are directories, then I've noticed that cp /hello /hello2 is the one that's inconsistent. Depending on what combination of trailing slashes are there, sometimes the contents of /hello are copied into /hello2, and sometimes the directory itself is copied in. –  Izkata Jan 15 '13 at 19:27
show 2 more comments

3 Answers

up vote 8 down vote accepted

mv a b attempts to move a into b if b is a directory or a symlink to a directory. Otherwise, it will rename (or copy and delete if on different file systems) a to b.

To get a consistent result to move a file into a directory, you can do:

mv file dir/

or

mv file dir/.

If dir doesn't exist, you'll get an error, and it won't rename file to dir.

If, on the other hand, you want to do a rename without having to worry if the destination file exists as a directory or not, with GNU mv, you can do:

mv -T file dest

In that case, file will not be moved into dest if dest is a directory. However, if both file and dest are directories and dest is non-empty, file will be renamed to dest (and the original dest removed).

Same, if file and dest are not directories (and that includes symlinks including symlinks to directories), file will be renamed to dest (though you will get a prompt if you don't have write permission to dest), and the original dest will be removed. There's a difference with mv file dest in the case where dest is a symlink to a directory. With -T, file is renamed to dest, but without -T, file is moved into the directory pointed to by dest.

So to sum up, after mv -T file dest, either file will have been renamed to dest or you'll get an error message (or a prompt). If the command succeeded, the original dest, if it existed beforehand will have been removed.

share|improve this answer
add comment

This is entirely dependent of if there was already a directory named '/hello2' already in existence or not.

If /hello2 exists AND is a directory, then move will always move /hello to /hello/hello2

If /hello2 does not exist, then move will always rename /hello to /hello2

If /hello2 exists AND is a file, you will get an error, "cannot overwrite non-directory 'hello2' with directory 'hello'.

share|improve this answer
add comment

I tried these

mkdir test1
mv -v test1 test2
output:`test1' -> `test2'
mkdir test1
mv -v test2 test1
output: `test2' -> `test1/test2'
touch test2
output:mv: cannot overwrite non-directory `test2' with directory `test1'

hope this explains everything. -v is verbose mode.

share|improve this answer
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.