Delete A Directory In Linux – As we know, Linux is one of the best operating systems (OS) that has many enthusiasts. As for the types of Linux that are familiar like Debian, Ubuntu, Red Hat, Fedora, and others. Well, some functions are commonly used through Linux terminals, such as installing programs, running programs, creating, moving, copying, editing, and deleting folders and files, and much more.
There are two types of Linux operating systems, the first server and the second desktop. Unlike the type of desktop that provides a 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 types of server users, maybe you will be confused when operating it. Here’s how to Delete A Directory In Linux:
Delete A Directory In Linux
In this discussion, I will use the 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 implement them.
1. Removing Blank Directories
The rmdir command is used to delete empty directories on Linux. This command cannot be used for folders that contain or have folders / files in them. The methods include the following.
1. Open your terminal. Direct terminal commands to the directory to be deleted. Enter the parent directory of the directory you want to delete using the command:
For example, as in the image below, I want to delete the folder1 in my Documents / folders. The folder is empty, so I can use the rmdir command to delete it. Enter the directory with the command:
Documents / folders
2. After that type the command as follows.
rename the folder
To delete folder1 I use the command:
3. Your directory was successfully deleted. You can prove this 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 my folder directory.
2. Removing Empty Directories / Which Have Contents
The rm -r command can be used to delete empty directories or have contents, ie there are folders and / or files in them. Before successfully deleting a directory, this command will ask for confirmation of deletion when it is run against a directory that does not have write (write-potected) access rights.
As we know, directory permissions are divided into 3, namely read, write, and execute or usually written with rwx. Where read means that the user can only see file / folder, write means the user can write files / folders (including editing and deleting), and execute to execute files / folders (useful for entering into folders).
The following 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.
As for the image below, I will delete the folders. There is a file named file1.txt and the task folder in the folder directory2. You need to note that the task folder only has read (r) access rights, visible in the absence of the letter w in the line (dr – r – r–).
2. Type the command:
rm -r nama_folder
You can see the implementation of the image that is designated by the second arrow.
3. Next, the deletion you ordered will be processed. Confirmation will appear as in the picture where you can type y to be sure to delete the directory.
4. Your directory was successfully deleted. You can prove this 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 my folder directory.
3. Removing Directories / Files Without Permission
This command is similar to the previous one, namely rm -r. It’s just that the difference lies when the command is executed. The rm -rf command no longer asks for your confirmation (permission) to actually delete the directory that you want, but it will immediately delete it when it starts.
The following is the application of the rm-rf command.
1. Open your terminal and use the cd command to go to the directory you want to delete.
As for the image below, I will delete folder3. There is an exercise folder and a file named test.txt in the folder directory3. You need to note that the test.txt file only has read (r) access rights, visible in the absence of the letter w in the line (r – r – r–). This is more or less the same as the case of rm -r above, where in the directory you want to delete there is a file / folder that is write-protected.
2. Apply the delete command with the following syntax:
rm -rf folder_name
You can see the rm -rf command in the image, which is indicated by the second arrow.
3. Your directory was successfully deleted. The rm -rf command appears to not ask for confirmation again to delete the folder directory3. You can prove this by listing the contents of your parent directory with the ls -al command. In the sample image, it appears that folder3 is no longer in the folder directory.
The three commands above can you use to delete an empty folder. As for the folder that contains it, you can choose between the rm -r and rm -rf commands, depending on your taste. You can also use the rm -r command to check whether in your directory there is a protected file / folder. That’s how to Delete A Directory In Linux that we have summarized. Hopefully this information can be useful, good luck and thank you.