0

I was wondering about following

Let's say you have two directory's in linux dir1 and dir2. now when I do

mv dir1 dir2

then dir1 gets moved into dir2.

But what if I want to rename dir1 into dir2? And while dir2 already exist, let bash tell me so and ask me if I wish to override? or maybe just tell me that it can't be renamed for it already exists?

Is there a way for this?

boselfje
  • 57
  • 7
  • possible duplicate of [Is there a way to make mv create the directory to be moved to if it doesn't exist?](http://stackoverflow.com/questions/547719/is-there-a-way-to-make-mv-create-the-directory-to-be-moved-to-if-it-doesnt-exis) – ROMANIA_engineer Jun 17 '15 at 11:45

2 Answers2

1

what you can do is, use i and T options.

T will think the target as normal file, so that won't do "move into" if target dir existed; i will let you confirm. In your case, it would be:

mv -iT dir1 dir2

If dir2 doesn't exists, no confirmation message will show.

Note, if target dir2 is not empty, you cannot mv, even though you confirmed.

If you want to overwrite anyway, you need write a little function/script to do it.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • Thank, this is the solution to my wondering :p . That dir2 can't be renamed when not empty makes sense too, for then it would not only rename but also make a join of the files. I guess this is not in the power of this command? – boselfje Jun 17 '15 at 12:59
  • @boselfje you can write function/script check if the target exits, if true, ask confirmation, recieve 'y', do something like `mv dir1/* dir2` then rename dir2 -> dir1. of course you have to handle the existing sub-dirs if it is necessary. – Kent Jun 17 '15 at 13:05
0

If you use mv with option i it will prompt and check do you prefer to overwrite an existing directory or not if it is existing.

If dir1 and dir2 exists and you try as follows it will prompt you. If dir2 is not existing it will not prompt.

mv -i dir1 dir2
Steephen
  • 14,645
  • 7
  • 40
  • 47