When you buy through links on our site, we may earn an affiliate commission. Learn more.

How To Delete A Directory In Linux

how to delete a directory in linux
By prakash
Published on June 3, 2022

Want to know how to remove directory in linux? Well you have landed on the right article

Linux is one of the most efficient operating systems and is best known among qualified programmers and techno-savvy professionals. Linux is also considered best for beginners in the programming network where rookies are unsure which program to use.

How To Remove Directory in Linux

Are you also one of those beginners who is unsure which program to use while programming?

We have got you covered. You can experiment with all the types of programs, and we will tell you how to remove or delete an empty and non-empty directory in Linux.

Linux offers the programmers an open-source to practice which doesn't make them feel isolated or cornered. In this technology-driven world, a beginner will get hundreds of sources to learn and excel at Linux.

Linux will not limit your programming journey; therefore, you get a free hand with Linux.

You can delete a directory in many ways, i.e., graphically and via the command line as well. Everything that is stored in Linux is considered a file (including directory).

Therefore, when you refer to deleting a directory, you are deleting a pile of files from Linux. This concept is the same for all types of systems. You can either use Gnome's file to delete the directory graphically or use the command for the headless server.

It is not an overnight process where you can simply become an expert and remove the directory. Therefore, we have a step-by-step guide to explain the whole process. This piece of the article mentions all the ways in which you can delete your directory or remove it from the system.

Types of commands to delete a directory in Linux

Linux directories can be deleted using two types of commands, both of which are easy to adapt for the headless server. These commands are similar to deltree and del, which work on normal operating systems.

The only caution to be taken while deleting these files is that they cannot be recovered from the trash or recycle bin of the system. These commands delete the file permanently from the system. These two commands are:

  • Rm command - Using the rmdir command is beneficial for beginners because it deletes the specified files or folders from the system. Therefore, the empty directory can be deleted by this command because the non-empty directories cannot be deleted using this command.

  • Rm command - It is important to get comfortable with the Linux operating system before using the rm command because it deletes the unspecified files, which also include sub-directories.

However, you get an option to delete the non-empty directories using this command in this open-source operating system.

How to Delete a directory in Linux?

How To Delete A Directory In Linux

The two commands mentioned above will help you delete the directory in Linux. You can use either of these two commands to delete the directory and remove it from the system permanently.

There are no trash cans or recycle bins present in the whole system; therefore, delete the files cautiously. 

Linux Delete Directory: rmdir command

The Rm command is used to delete the directories that are empty. The empty directories refer to the empty folders in the system which can be deleted permanently.

Follow the steps to delete the empty directory using the rmdir command:

Step 1: Look for the directory on which you are currently working using the 'Is' command on the Linux system.

Step 2:  Select the file or directory you want to delete and ensure that it is an empty folder.

Step 3: In order to delete the folder which you are currently running, type " rmdir folder name ". 

Step 4: You can run the program 'Is' to see the content of the folder.

Step 5: The specified file will no longer exist.

If you want to delete five or six directories together, you can use the command 'ignore-fail-on-non-empty' to avoid termination of the whole process in between. You can resort to multiple options while using the rmdir command, some of which are:

  • i option to ensure and confirm that the specified file has been deleted.

  • Tree command to look for the files which the system has removed.

  • preserve-root for the qualified professionals.

  • one-file-system.

Note: The file deleted using the rmdir command shouldn't contain any content to successfully delete the file from the system. However, if the file specified in the command is not empty, you will receive an 'error' message on your screen.

It is one of the benefits while using the rmdir command. The programmer or developer will be stress-free of the file that is being deleted.

how to delete directory in linux: rm Command 

Rm is another command to delete the directories in Linux. Rm is different from the rmdir command as it has the feature of deleting empty as well as non-empty folders from the system.

Therefore, you can delete the directories or files which are full of content and information. Rm also functions differently as it doesn't work without options like -d and -r. You need to use '-r' (-recursive ) and -d to delete the files which have content.

You can follow the below-mentioned steps to delete the directory:

Step 1:  rm [folder name] You need to enter either of the two flags, i.e., '-d' or '-r' for empty and non-empty directories, respectively.

Step 2: The syntax will look like rm -r 'folder name' templates

Step 3: The second step is to confirm the process of deleting the file. Rm -rf 'folder name'.

Therefore, these three steps will delete all the contents mentioned in the specified folder. Use this command only when you are sure which folder you want to delete.

Are you getting permission denied error messages while removing directories?

The Linux operating system allows only the owners to delete their directories. But, there are times when you will be denied permission while deleting one such directory, and you will end up receiving an error message. You can use the following syntax to solve this problem:

  • Sudo rmdir /path/to/dir/

  • Sudo rm ‘-rf dir2’

When you run this particular program, you will be prompted to provide the system with the root user password. Therefore, when you enter the password, you will be able to delete the directory.

How to see a diagnostic message for every directory processed? 

'-v' option can be run in the rmdir command to see a diagnostic message for every directory processed. For example rmdir '-v' 'folder name'.
Linux delete all files in Directory

Delete folder in linux in a directory is a very easy process of three steps:

Step 1: Select the terminal application where you want to run the syntax of deleting everything.


Step 2: Run the command ‘rm/path/to/dir/*. This syntax will delete everything that is in the directory.


Step 3: Run the command ‘ rm -r /path/to/dir/*. This syntax will remove the remaining sub-directories in the file.

Delete a Linux directory using GUI

Removing a directory using graphics is one of the easiest methods on the Linux operating system. All the systems are not very different from each other while using a graphic to delete the directory. The steps are as follows:

Step 1: Select the file manager and choose the directory you want to delete.

Step 2:  You can simply right-click and choose the option to "delete" or "move to trash."

Step 3: If you move the directory to trash, you can recover the directory easily from the recycle bin. However, if you choose to delete the directory from your system, it will permanently delete your directory without any conflict.

Conclusion

All of these methods will help you to delete the directory (a pile of files) from the Linux operating system. If you are a beginner and still exploring this operating system, this is a perfect guide for you that explains the whole process step by step.

However, if you have a good level of experience with Linux, you will understand the process of deleting the directory much better after reading this article.

I hope you liked our article on how to remove directory in Linux, if you have any comments or suggestions do share them in the comments below. 

Frequently Asked Questions

1. How do I delete a directory in the command prompt?

It is easy to delete a directory in the command prompt. 'Del' command is built into the windows and helps to delete the directory. Using the 'del' command will delete the specific files from your system.

The deleted files cannot be retrieved because it doesn't move to the recycle bin from where the files are retrieved. You can delete a directory in the command prompt using the following steps:

Step 1:  Open the 'Start' menu and look for 'Command Prompt'.

Step 2:  Select 'Run as administrator from the start menu.

Step 3:  Click on 'Yes' for the user account control prompt. A command prompt page will open where you need to enter the command and press 'enter.'

Step 4:  Your file, which is specified in the command prompt, will be deleted from your system.

2. How do I delete a directory in Lunix whitout a prompt?                                                           

You can delete a directory in Linux without a prompt by un alias 'rm alias.' It is a common or a general method that is used to remove the files from the system by adding the '-f' force flag for the command 'rm.'You should ensure that you are aware of the files you are removing.

Therefore, use the command force only when you know what files you are deleting

3. Which command is used to delete a directory?

You can delete the directory, including all of its content and other stored files, using the 'rmdir command.' You, however, should make sure that you will not need these files again from your system because these files, once deleted, are not transferred to the recycle bin and, therefore, cannot be recovered.

4. How do I delete a non-empty directory in Lunix? 

The rm command along with -r will recursively delete everything from your non-empty directory. Be cautious while using this command because it will delete all the other contents stored in the files, i.e., sub-directoriessmall files, and other content.

5. How do I delete a directory in the Linux terminal?

Linux terminal that provides an interface through which you can print a text with the help of a command can be deleted using the command 'rm -r' and the name of the Linux terminal directory which you want to delete from your system. For example, rm -r 'directory name".

prakash

Follow me here

About the Author

Prakash is a passionate individual who loves to live his life up to full potential. An avid traveller and reader, he loves to explore various places and has quite a knack for research. He is intuitive by nature and possess the ability to handle multiple informational resources at one time. Prakash is dedicated and sincere in approach and he loves networking with like-minded people.

You may also like

How To Hide Apps In Oppo?
How To Increase Jio Network Speed
How To Put ATM Card In Machine?
How to Become Flipkart Plus Member
Youtube Premium Free Trial
ALT Balaji Free Trial
How To Become Detective In India
How to Become RTO Officer
How to Hide Apps in Redmi?
How to Unblock SBI Debit Card?
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>