How to Delete a Directory in Linux
How to Delete a Directory in Linux

How to Delete a Directory in Linux 2023

Linux differs significantly from the Windows operating system. For example, on Windows, you can delete a folder by right-clicking on it and selecting Delete. Unfortunately, in Linux, things are not so simple. With Linux, you can delete a directory or folder using both the graphical user interface and the command line interface.

If you’re unsure how to delete a directory in Linux, we’ve put together a simple yet effective method for you. This article will demonstrate both the GUI and CLI ways for deleting folders in Linux.

In this guide, we will show you how to delete a directory in Linux using the command line. We will cover the different commands and options available for deleting directories, as well as some best practices for ensuring that your data is secure.

1. How to Delete a Directory in Linux Via GUI Method

This way of deleting files is simple and ideal for people who have recently switched to Linux. It is compatible with any distribution as long as a “Desktop Environment” and file manager are installed on the machine. We’ll be using Ubuntu 20.04 LTS with the Gnome desktop interface and the Nautilus file manager in this article. Here is how it works:

  1. Open your file manager, such as Nautilus or Thunar, by clicking on the corresponding icon in your applications menu or by pressing the shortcut key to open it.
  2. Navigate to the directory or folder that you want to delete.
  3. Right-click on the directory and select “Delete” or “Move to Trash” from the context menu. This will move the directory to the trash folder.
  4. If you want to permanently delete the directory, open the trash folder and right-click on the directory you want to delete.
  5. Select “Delete Permanently” or “Empty Trash” from the context menu. This will permanently delete the directory from your system.

Note that deleting a directory permanently will also delete all the files and subdirectories inside it. So make sure you have backed up any important data before proceeding with this method.

2. Delete Directory Via rmdir Command

The rmdir command is commonly used to remove empty directories, but it can also be used to remove non-empty ones. The command does not have many features or parameters, but it accomplishes the job.

In Linux, you can also delete a directory using the rmdir command. Here’s how:

rmdir <options> <directory_name>

The rmdir command has several options, such as:

--ignore-fail-on-non-emptyused to delete non-empty directories
-p, --parentsused to delete the directory along with its children specified
-v, --verboseused to get a diagnostic message for every directory
  1. Open the command prompt or terminal on your computer.
  2. Navigate to the parent directory of the directory you want to delete using the cd command. For example, if the directory you want to delete is located in the Documents folder, you would type cd Documents and press Enter.
  3. Type the rmdir command followed by the name of the directory you want to delete. For example, if the directory you want to delete is named my_directory, you would type rmdir my_directory and press Enter.
  4. If the directory contains any files or subdirectories, you will be prompted to confirm the deletion. Type y and press Enter to confirm.
  5. The directory will be deleted and you will see a confirmation message in the command prompt or terminal.

Note: The rmdir command can only delete empty directories. If the directory you want to delete contains files or subdirectories, you will need to delete them first before using the rmdir command.

Alternatively, you can use the rm command with the -r flag to recursively delete the directory and all its contents. However, be careful when using the rm command as it can permanently delete files and folders without prompting for confirmation.

3. Delete Directory Via rm Command

The rm command is an effective instrument that can erase both files and directories and has many useful capabilities. The command’s fundamental syntax is as follows:

rm <options> <file_name/directory_name>

The command can take the following options:

-fWhen this flag is used, the confirmation prompt will not appear, and all the nonexistent files and directories will be ignored
-iWhen this flag is used, the command will ask for confirmation from the user for each deletion.
-rWhen this flag is used, the command will remove all the contents of the directory specified.
-dThis flag is used to remove empty directories.
-vThis flag is used to get an explanation of what is being done currently.

To delete a directory using the rm command, you can use the -r option, which stands for “recursive.” This option will delete the specified directory and all its subdirectories and files.

If you want to remove a directory named “example_dir” and all its contents recursively, you can use the following command:

rm -r example_dir

Make sure to be careful when using this command, as it will delete all files and subdirectories within the specified directory, and this operation cannot be undone. To avoid accidental deletion, it’s a good idea to double-check the directory path and contents before executing the command.

Also note that some versions of the rm command may have different behavior, such as prompting you for confirmation before deleting each file or directory. It’s a good idea to check the documentation or man pages for your specific version of the command to understand its behavior and options.

Also Read:

8 Best Open Source Alternatives to Adobe Products for Linux

How to use Linux ifconfig command

7 Ways to Create a File through Linux Terminal

Linux From Scratch (LFS) – Make Your Own Operating system


No comments yet. Why don’t you start the discussion?

Leave a Reply