How do I delete a folder but not the contents?

My directory (in Linux) looks like com/com/{various,files} where com and com are folders of the same name and the second com holds the various files. I want the first com removed, but the second and files in the second kept.

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

Rename the outer directory out of the way, move the inner one to the name you want, and then delete the (now empty) outer directory:

$ mv com to-delete
$ mv to-delete/com com
$ rmdir to-delete

You could also mv com/com/* com and remove the inner directory, if you don’t have too many files and none of them are dotfiles, but the above is more general and more efficient.

Method 2

The only way to accomplish this is to move the “various files” up a level and then remove the second “com” directory.

Example

$ mv com/com/(various files) com/.
$ rmdir com/com

Method 3

So here you have this all in one:-

mv com/com/ SOME-TEMPORARY-NAME ; rm -rf com ; mv SOME-TEMPORARY-NAME com

The ; simply lets you run all the commands in one go.

And then I have used rm -rf so that even if you have any other files inside com/ all will be removed without user-interaction.


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x