+5 votes
77 views
in Linux by (242k points)
reopened
Linux: delete directory - that's how it works

1 Answer

+3 votes
by (1.6m points)
 
Best answer

Delete directories via the terminal
Delete directories using the file manager

How to delete directories on Linux using the file manager or the terminal..

image image

Deleting directories is no more difficult on Linux than it is on Windows - well, almost. In our instructions, we will show you how you can also remove write-protected folders via the terminal or the file manager .

Delete directories via the terminal

In the command prompt, i.e. a terminal window , you can easily remove folders with the "Remove" command . In the Linux world, rm stands for r e m ove. This works in the same way as with files, especially since folders are also files under Linux - and yet there are two small restrictions : With a simple " rm myfolder / " you get the error message that it is a folder. Problems with missing rights can also arise. The general syntax is:

rm [OPTION] Datei/Ordner

Deleting directories always requires the " -r " parameter as an option, which stands for recursion. So the correct command would be " rm -r myfolder / ". To do this, you have to understand the Linux logic: Even if a folder is a file, it is a special case of a file, as it can contain other files. In this respect, the entire content of a folder must inevitably be deleted, which works via recursive deletion. And it doesn't matter whether there are actually files in the folder or not..

An example : We would like to delete the folder "Example folder" including all files and subfolders in the directory ~ / Downloads . Assuming you are already in the ~ / Downloads directory , the following command must now be entered:

rm -r Beispielordner/

image
After deleting the directory, the ls command will no longer display the folder. So the folder was deleted.

The other possible error message refers to a lack of rights - in this case you must run the rm command with root rights . To do this, you can either switch completely to the "root" user - simply enter " su " and then your root password . Or you just execute the individual command with the appropriate rights: " sudo rm -r myfolder / " of course only works if your current user is in the sudo group. With Ubuntu, Mint and many other popular distributions, the sudo variant would be the standard, with Debian the permanent user change with su.

After you have issued the command, the program asks whether the folder should be "descended" (if it is a deeper subfolder) and whether the (write-protected) folder should really be deleted - confirm both with "Yes" (j) or "Yes" (y) ..

Delete directories using the file manager

Of course, you don't have to type around in the terminal, the file manager can of course do this as well. Simply right-click on the directory and select the entry " Delete " or " Move to the trash " from the context menu - there is no need to explicitly specify the recursion here. The rights problem can still exist. In such a case under Windows you would be asked whether the folder should be deleted with admin rights. Common file managers under various Linux distributions are unfortunately not that comfortable. If you ask, fine - if not, the terminal helps again: Start the file manager from the terminal with sudo or su - then you also have root rights in the graphical file manager.

image
The directory can also be deleted with a right-click and the delete function.

If you are not sure what the name of the manager is, just open it as normal and look at the top of the file menu under " Help ", the " ? " Or " About " - under one of the points there is guaranteed to be the name of the program. That depends on the respective desktop environment, with the LXDE desktop the typical command would be, for example, " sudo pcmanfm ". So started you should be able to delete pretty much anything.


...