How to Delete a Directory in Linux 


               


How to Delete a Directory in Linux



Linux


In this article, we show how to delete a directory in linux.

We will show how to delete a directory that is empty (has no contents inside of them) and how to delete a directory that has contents inside of it. The process is a little different for each.

How to Delete an Empty Directory in Linux

So we'll first go over how to delete an empty directory in linux.

So in order to delete an empty directory in linux, we use the rmdir command followed by the name of the directory.

If you are simply specifying the name of the directory after the rmdir command, this deletes the directory with that specified name in the current directory you are working in.

If deleting a directory in any other directory (other than the current working directory), then you need to specify the path to that directory.

In this example, we are simply going to delete a directory named Documents from the current working directory. Therefore, we simply specify the directory name after the rmdir command.

This is shown in the following code below.



So it's very basic. To delete an empty directory in linux, we simply have the rmdir command followed by the directory name; in this case, the directory name is Documents.

Running the code above gives us the following output shown below.

Deleting an empty directory in linux

We use the ls command to list the contents of the current working directory and we see that we have a directory named Documents before the deletion.

After we run the rmdir command, we see that the directory, Documents, disappears. It's permanently deleted.

This is how you can delete an empty directory in linux.

How to Delete a Non-empty Directory in Linux

Now we will go over how to delete a non-empty directory in linux; this is a directory that has contents inside of it such as files or other directories.

Deleting a non-empty directory is different than deleting an empty directory.

To delete a non-empty directory, we use the statement, rm -r, followed by the name of the directory that you want to delete.

So to delete the same Documents directory, now with contents inside of it, we use the following code shown below.



This will delete a directory and all of the content inside of it.

The following command terminal below shows how a file that has contents in it is deleted with the rm - r command.

Deleting a non-empty directory in linux

So we do the ls command to show the contents of the directories before and after the deletion.

The Pictures folder is completely deleted along with all of its content. The rm -r command does recursive deletion. It goes through the lowest subdirectories, deletes what's in them first, then deletes that folder and does the same for all parental folders until the whole folder is deleted.

This command, however, can be dangerous, because it just deletes everything with no type of confirmation prompt.

So there is another safer command in linux that you can use, rm -ri command.

This command will ask for confirmation before deleting all files and all directories in the directory you are deleting.

It will ask, are you sure you want to delete this file/directory?

That way, it's safer. It just doesn't delete everything all at once. You actually have to confirm. This can be a good thing or a bad thing. If you are sure you want to delete a directory and it has a lot of files, like hundreds or thousands, it would be impractical to delete a directory in this way, as you would have to confirm the deletion of hundreds or thousands of files, which is tedious.

However, if there are only a few files and you want to use this safer method, you would use the rm -ri command.

This is shown in the following output below.

Deleting a non-empty directory with a confirmation prompt in linux

So now you see above that with the rm -ri command, a confirmation needs to be done in order to delete each file and directory in the directory you are deleting.

If you had simply used the rmdir command, then the deletion attempt would have thrown an error.

To delete an empty directory, you can use the rm -ri command, but it would be unnecessary.

And this is how to delete a directory in Linux.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...