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.

I have a little question here.

If I have two files, say filea and fileb, mv filea fileb would

  • delete fileb
  • rename filea to fileb

Then if I have two directories, say dira and dirb, mv dira dirb would

  • move dira into dirb (it will become dirb/dira)

Noting that in both cases there are no notice or message, then this is pretty inconsistent to me. I think mv dira dirb should just overwrite dirb with the contents of dira (or merge the two directories under a directory named dirb).

I remember reading somewhere that a directory name with a slash (like dira/) is treated like a directory, and name with no slash (like dira) is treated like a file (to certain extents, of course). Anyway now I want to make the shell (zsh and possibly bash) respect my notation of a directory by using a slash. Is there a terminal option which enable me to enforce that?

To clarify, here is my desired behaviour:

  • mv dira dirb results in dirb being overwritten with the contents of dira
  • mv dira dirb/ results in dira being moved into dirb (in dirb/dira)

Has anyone thought the same way as me? Or am I just weird?

share|improve this question
1  
I think your desired behavior would be rather dangerous. –  starblue Aug 20 '10 at 14:36
    
@starblue: yes I agree that it is dangerous, but I'd rather live with something dangerous, than inconsistency –  phunehehe Aug 21 '10 at 15:40
add comment

2 Answers 2

up vote 7 down vote accepted

Yes, this is a bit inconsistent, even within the GNU tools.

One problem with your proposal is that non-empty directories cannot be removed. Apart from that, -T (in GNU land, anyway) approximates what you want (the first case):

$ ls dira/ dirb/
dira/:
a

dirb/:
b
$ mv -iT dira/ dirb/
mv: overwrite `dirb/'? y
mv: cannot move `dira/' to `dirb/': Directory not empty
$ rm dirb/b 
$ mv -iT dira/ dirb/
mv: overwrite `dirb/'? y
$ ls dira/ dirb/
ls: cannot access dira/: No such file or directory
dirb/:
a
share|improve this answer
    
You could also add an alias, e.g. alias ren=mv -T. –  Mikel May 13 '11 at 3:57
add comment

If you just want to move everything from dira to dirb you can run mv dira/* dirb.

As per mv man page on my HP-UX.

The mv command moves:

+ One or more directory subtrees (directory1, [directory2, ...]) to a new or existing directory (dest-directory).

share|improve this answer
1  
Your command would miss all dotfiles. –  qbi Aug 20 '10 at 12:21
    
Who needs dot files anyway ;) –  Robert Massaioli Aug 20 '10 at 13:45
    
And if it's an issue, shopt -s dotglob in bash will fix that. –  David Z Aug 21 '10 at 6:41
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.