I have a little question here.
If I have two files, say filea and fileb, mv filea fileb would
- delete
fileb - rename
fileatofileb
Then if I have two directories, say dira and dirb, mv dira dirb would
- move
diraintodirb(it will becomedirb/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 dirbresults indirbbeing overwritten with the contents ofdiramv dira dirb/results indirabeing moved intodirb(indirb/dira)
Has anyone thought the same way as me? Or am I just weird?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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).
Method 3
The following are solutions to obtain an overall more consistent behaviors (at least with the mv and cp commands).
For the move (mv) command
One can get a slightly more consistent behavior by using the -i option:
mv -i SOURCE DEST
This would warn and ask confirmation when filea would overwrite an already existing fileb but would give no warning in any of the other cases, that is, one gets a warning and confirmation only when data loss is about to happen.
$ mv -i filea fileb :
- if
filebdoes not already exist,fileais silently renamed tofileb - if
filebalready exist, confirmation is requested before overwritingfilebwithfilea
$ mv -i dira dirb :
- if
dirbdoes not already exist,dirais silently renamed todirb - if
dirbalready exist,dirais silently moved intodirb
A permanent solution is to create an alias: $ alias mv='mv -i'
For the copy (cp) command
For the cp command a similar -i option exists. However cp SOURCE DEST would not behave as mv -i SOURCE DEST if SOURCE is a directory, because cp would omit any directory if the -r option is not specified.
So, to get a similar behavior as with mv -i one can rather use the following:
$ cp -ir SOURCE DEST
This would treat files and directory similarly (as the mv -i command would) and would warn and ask confirmation before overwriting.
$ cp -ir filea fileb :
- if
filebdoes not already exist,fileais silently copied tofileb - if
filebalready exist, confirmation is requested before overwritingfilebwith a copy offilea
$ cp -ir dira dirb :
- if
dirbdoes not already exist,dirais silently copied todirb - if
dirbalready exist,dirais silently copied intodirb
A permanent solution here is to create an alias: $ alias cp='cp -ir'
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0