How to rename a folder in Linux?

2

I want to remove folder aaa and rename folder bbb to aaa. Both have subfolders in them.

Seems simple enough using:

rm -r aaa; mv bbb aaa

However, any subfolders in bbb disappear where I would expect them to stay in bbb (now aaa).

I can get around this using:

rm -r aaa; cp -r bbb aaa; rm -r bbb;

But this seems a long-winded way around it.

I have read about using -r with mv, but apparently this isn't possible (if it ever was).

My question is, how do I rename (move) a folder and keep its subfolders?

Neil Belch

Posted 2014-02-21T15:31:09.307

Reputation: 121

1You should remove directories with rmdir (if they are empty). If they have subdirectories or files, use rm -R (or if you are venturous rm -Rf to delete without asking). And: mv -r is not needed, because mv just renames (moves to a new name) a directory (or file) independent of its content. – erik – 2014-02-21T15:37:51.407

Sorry I should have clarified that. I am using -r with rm to remove all subfolders. but when I used mv it does not keep the subfolders in bbb when it becomes aaa - I need these subfolders as well. – Neil Belch – 2014-02-21T15:56:03.297

3mv bbb aaa is the proper command for that. mv doesn't move anything in this case, it just renames the folder. Is it possible you made a mistake? – Dennis – 2014-02-21T15:59:13.120

1I've double checked my code using mv (my first code example), and it has kept the subfolders - so I don't know what I managed to do the first time around. Sorry for wasting your time and thanks for the assistance. – Neil Belch – 2014-02-21T16:10:07.977

1You can execute history to check what went wrong the last time. – Dennis – 2014-02-21T16:19:30.167

No answers