This is actually deceptively simple and I'm shocked nobody else has offered this answer yet, so here I go
Using the following for example...
$ mkdir foo
$ mkdir bar
$ mkdir ./bar/foo
$ touch ./bar/foo/file1 && touch ./bar/foo/file2 && touch ./bar/foo/file3
$ ls ./
bar/ foo/
$ ls ./bar/foo
file1 file2 file3
Now comes the magic part
$ mv ./bar/foo ./foo/
$ ls ./foo/
foo/
$ ls ./foo/foo/
file1 file2 file3
Now why does this work?
I can't give you the most precise answer as I'm just a neophyte - but I understand it comes down to how trailing /
characters are treated on folders.
Look closely at that mv command
$ mv ./bar/foo ./foo/
Notice how the target folder of files is specified without a trailing /
, while the destination sharing the target folder's name has been specified using a trailing /
'. This is your magic ticket, and can bite you in the ass if you aren't paying attention.
I'm sorry I can't offer a more elaborate answer than that at present, but perhaps someone more fluently knowledgeable will take the liberty of editing my answer.
In any event, this would seem to be the most simple and straightforward solution, with a single use of the ls
command and no creation of intermediary folders.
Hope it works for you, cheers!
foo
? Do those links now need to point tofoo/foo
?foo
(e.g. "recently opened files"). To keep those working,foo
itself has to be moved, rather than just its content. (otherwise those links would refer to the new parent directory which isn't desired)mkdir foo/foo; mv foo/* foo/foo
prints an error but still results in your expected outcome (at least on Ubuntu). Now: if you are interested on how inodes "move" then the result is not what you want (the originalfoo
directory is not the innerfoo
so if you access via inode or something you will not find the expected structure).