How to Delete a Directory in Linux?

Learn how to delete a directory in Linux with this step-by-step guide. Explore efficient commands and techniques to remove directories, ensuring a clean and organized file system on your Linux-based operating system.

How to Delete a Directory in Linux?

HOW TO DELETE A DIRECTORY IN LINUX ?

 

Linux is very different from the Windows operating system. For example, on Windows, you can delete a folder by right-clicking it and selecting "Delete." However, things are not as straightforward with Linux. In Linux, you can delete a directory or folder using either the graphical user interface or the command-line interface. If you're not sure how to delete a directory in Linux, we have a simple yet effective answer for you. In this post, we will look at  command-line ways for deleting folders in Linux.

How to Remove or delete a directory in Linux?

In Linux, removing files or directories is a fundamental capability that all users must understand. Although it may appear to be a simple process, there are several methods for deleting files or directories, each with its own use case.

The following article will show you ways to delete files or directories in Linux. We'll additionally discuss the programs used for removing folders, directories, and their contents.

If you delete a file, any references to it vanish from the file system. The file doesn't get instantly destroyed from the memory of the device; rather, the space is marked as accessible for reuse.

 

Using the “rm” command: 

To delete a directory in Linux using the rm command, use the -r (recursive) option, which deletes both the directory and its contents. Following is the step-by-step process for the same:

 

  • Launch the Terminal

On most Linux distributions, you may find the terminal in the program menu or use the keyboard shortcut “Ctrl + Alt + T."

  • Navigation

To delete a directory, use the “cd” command to navigate to its parent directory.

For example, if the directory you want to remove is named "my_directory" and it is situated in your home directory, you can use:

cd ~/my_directory

  • Use the “rm” command

Once in the parent directory, type rm -r, which is followed by the name of the directory you want to delete. For instance, if the directory is called "my_directory" the command would be:

rm -r my_directory 

(Note : Using “rm” command with “-r” will recursively delete the mentioned directory with its content)

  • Confirm for deletion.

In accordance with the system settings, you may be asked to confirm the deletion or enter your password. Some systems may ask you for confirmation of each file's removal from the directory.

  • Verify the deletion of the directory

Use the “ls” command to list the contents of the parent directory in order to verify that the directory has been destroyed. For example,if you have deleted a directory named “my_directory”located in home directory then type “ls ~/” . 

 

ls ~/my_directory

 

(The folder named my_directory ought to disappear from view.)

Some options that can be used with “rm” command:

  •  -r  : Removes directories and their content recursively.

  •  -f  : Forces the removal of all files or directories.

  •  -i  : Prompts for confirmation before removing.

  •  -I  : Prompts once before removing more than three files or when removing.

  • -d  : Removes empty directories.

  • -v  : Provides a verbose output.

  • –helps : Displays the help text.

  • –version : Displays the command version.

 

Using the “rmdir” command: 

To get rid of empty directories in Linux, use the “rmdir” command.

If the directory has sub directories then you will have to use “rm” command”.Here are the steps to delete a directory with “rmdir” command : 

 

  • Launch the terminal.

On most Linux distributions, you may find the terminal in the program menu or use the keyboard shortcut “Ctrl + Alt + T."

 

  • Navigation.

To delete a directory, use the “cd” command to navigate to its parent directory.

For example, if the directory you want to remove is named "my_directory" and it is situated in your home directory, you can use:

 

cd ~/my_directory

 

  • Use”rmdir” command.

After locating the directory now you can use “rmdir” command followed by the name of the directory , for example : 

 

rmdir my_directory

 

(Note : If the respective directory is not empty then you will receive an error message amd the command won’t be executed)

 

  • Verify the deletion.

Use the “ls” command to list the contents of the parent directory in order to verify that the directory has been destroyed. For example,if you have deleted a directory named “my_directory”located in home directory then type “ls ~/” . 

 

ls ~/my_directory

 

(The folder named my_directory should disappear from view.)

Some options that can be used with “rmdir” command:

  • –ignore-on-non-empty : Doesn't show an error message when trying to remove a non-empty directory.

  • -p : Removes the directory along with its parent in the hierarchy.

  • -v : Provides a verbose output.

  • –help : Displays help text.

  • –version : Displays the command version.

 

Conclusion

It’s always a good idea of checking on the directory before deleting it with the help of “rm” or “rmdir”command . While there are various options that can be used with the main command but we should always be cautious while using option “-r” with “rm” as the directories are deleted recrusively with this option. By using all the commands and options mentioned in this article you can noe easily delete any directory you want.