3 Ways to Delete a Directory on Linux via Terminal

As we know, Linux is one of the best operating system (OS) that has many devotees. The familiar Linux types like Debian, Ubuntu, Red Hat, Fedora, and others. For you Linux users are certainly no stranger to the use of its terminal. This Linux terminal is the same as the command prompt or cmd in Windows. Well, some of the common functions used through Linux terminals, such as the activities of installing the program, running the program, create, move, copy, edit, and delete folders and files, and much more.

There are two types of Linux operating systems, first server and second desktop. Unlike the desktop type that provides the GUI interface, the server type relies on the shell or command line, in other words you give commands through the terminal. For those of you new users of server type, you may be confused when operating it.

How to Delete a Directory in Linux

In this discussion, I will use a terminal to delete directories on Linux by applying Linux command line commands. Here are some command lines that you can use on the terminal to delete directories in Linux and how to apply them.

1. Removing Empty Directories

The rmdir command is used to remove an empty directory on Linux. This command can not be used for folders that contain or have folders / files in it. As for the way among others as follows.

1. Open your terminal. Go to terminal command to the directory to be deleted. Go to the parent directory of the directory you want to delete by using the command:

cd nama_folder

For example, as in the picture below, I want to delete folder1 located in the Documents / folders directory. The folder is empty, so I can use the rmdir command to delete it. Go to the directory with the command:

cd Documents / foldersaya

2. After that type the command as follows.

rmdir nama_folder

To delete folder1 I use the command:

rmdir folder1

3. Your directory was successfully deleted. This you can prove by listing the contents of your parent directory with the ls -al command. In the sample image, it appears that folder1 no longer exists in the folders directory.

2. Removing Empty / Owned Directories

The rm -r command can be used to remove empty directories or have contents, ie there are folders and / or files in them. Before successfully deleting the directory, this command will ask for a confirmation of deletion when it is run against a directory that does not have write-potable permissions.

As we know, directory permissions are divided into 3, ie read, write, and execute or commonly written with rwx. Where read means the user can only view files / folders, write means the user can write files / folders (including editing and deleting), and execute to execute files / folders (useful for entry into folders).

Here is an example of applying the rm -r command.

1. First, you open the terminal and use the cd command to go to the directory you want to delete.

cd nama_folder

As in the picture below, I will delete folder2. There is a file named file1.txt and a task folder in the folder2 directory. You should note that the task folder only has read (r) permissions, visible in the absence of the letter w in the row (dr-r-r-).

2. Type command:

rm -r nama_folder

Implementation on the picture you can see yanng appointed by the second arrow.

3. Next, the deletion you ordered will be processed. A confirmation will appear like in the image where you can type y to be absolutely sure to delete the directory.

4. Your directory was successfully deleted. This you can prove by listing the contents of your parent directory with the ls -al command. In the sample image, it appears that folder2 no longer exists in the folders directory.

3. Removing Directory / File Without Permission

This command is similar to the previous one, ie rm -r. It’s just the difference when the command is executed. The rm -rf command no longer asks your permission to actually delete the directory you want, but will remove it immediately when it is run.

The following is the implementation of the rm -rf command.

1. Open your terminal and use the cd command to go to the directory you want to delete.

cd nama_folder

As in the picture below, I will delete folder3. There is a training folder and a file named test.txt in the folder3 directory. Note that the test.txt file has only read (r) privileges, visible in the absence of the letter w in the row (r-r-r-). This is more or less the same as in the case of rm -r above, where in the directory you want to delete there are files / folders that are write-protected.

2. Apply the removal command with the following syntax:

rm -rf nama_folder

Implementation of the rm -rf command you can see in the picture, designated by mcfol a second arrow.

3. Your directory was successfully deleted. The rm -rf command appears not to ask for confirmation again to delete folder3 directory. This you can prove by listing the contents of your parent directory with the ls -al command. In the sample image, it appears that folder3 no longer exists in the folder foldersaya.

Cover

The above three commands you can use to delete an empty folder. As for the folder that contains, you can choose between command rm -r and rm -rf, depending on your taste. The rm -r command can also be used to check whether your directory contains protected files / folders.

That’s how to delete directories under Linux. If there is anything you want to respond to, please fill in the comments field below. thanks.