Deleting Directories in Linux

Deleting Directories in Linux

Deleting Directories in Linux

The command-line interface serves as the cornerstone of Linux’s effectiveness and efficiency.

Deleting files or directories within the Linux system is a fundamental operation that involves removing a folder and its contents. To accomplish this task, Linux provides specific commands. The ‘rmdir’ command, for instance, is designed to delete empty directories.

Jump To...

Deleting a Directory in Linux?

In Linux, the ‘rmdir’ command is utilized to remove empty directories, whereas the ‘rm’ command is employed for deleting files, including those with content.

Deleting Directories in Linux image 1

Deleting a Directory with the "rm" Command.

Deleting Directories in Linux image 2

To remove an empty directory in Linux, you can use the following command:

rmdir [directory_name]

To remove a directory with content in Linux, use the “rm -r” command.

rm -r [directory_name]

The ‘rm’ command may prompt for confirmation when removing multiple directories. Confirm with ‘y’ or skip confirmation with ‘-f’.

rm -rf [directory_name]

Using ‘rm -rf’ forcefully removes the directory and its contents without confirmation. Caution is advised to prevent data loss.

Deleting Linux Directories Using GUI File Manager.

Deleting Directories in Linux image 3

GUI file managers provide a user-friendly way to remove Linux directories, offering a visually intuitive method for file system management.

KDE uses Dolphin, GNOME uses Nautilus, and XFCE uses Thunar as their respective Graphical User Interface (GUI) file managers.

They provide a simple alternative to command-line functions, especially for users who prefer graphical methods for removing directories.

The GUI file manager method is the easiest way to remove directories, especially for users unfamiliar with command-line functions. Its graphical interface allows users to visually confirm directories, reducing the risk of unintentionally deleting important files. Ensure you have the necessary permissions before deleting files to avoid any issues during the process.

Certain file managers may request confirmation before permanently deleting files and directories. Confirm the action if prompted. Deleted items typically go to the trash or recycling bin. To free up disk space, empty the trash to permanently delete the directory.

Securely Deleting a Directory in Linux with the "shred" Command.

The ‘shred’ command in Linux is commonly used to securely delete files by overwriting their content, though it is not specifically designed for removing directories.

Deleting Directories in Linux image 4

For an alternative method to delete a directory along with its content, you can combine the “rm” command with the “shred” command, as demonstrated below:

shred -u -z -n 3 [directory_name]


  • ‘-u’ removes the directory entry

  • ‘-z’ adds a final overwrite with zeros to hide shredding

  • ‘-n 3’ specifies three passes of overwriting data

Command 'rm'

Deleting Directories in Linux image 5

The `-r` option stands for “recursive,” which allows removing directories and their data.

rm -r [directory]

The `-f` option forces you to delete files without asking for confirmation. Be cautious when using this option.

rm -f [filename]

The `-i’ option prompts for confirmation before deleting each file.

rm -i [filename]

This command interactively removes the non-empty directory and its contents, asking for confirmation before each deletion.

rm -ri [directory]

First, document the directories with the ls command to witness deleted directories before executing the command. You can remove all files in the current directory using the following command line with a `.txt` files extension.

rm *.txt

Deleting a Directory Form Linux via Command Line with the "rmdir" Command

Deleting Directories in Linux image 6

The `rmdir` command in Linux deletes an empty directory, specifically designed for removing directories without any files or subdirectories. The basic syntax is as follows:

rmdir [directory_name]

Deleting Directories with the "trash-cli" Command

The command below will restore a file from the trash.

trash-restore [filename]

Execute this command to permanently erase all items in the trash.


Removing an Empty Folder

Deleting Directories in Linux image 8

Input the specified command, replacing the directory name with the file name.

rmdir [directory_name]

The “rmdir” command is specifically designed to delete empty directories.

rm -d [directory_name]

Keep in mind that the previously mentioned commands are applicable only to empty directories.

Frequently Asked Questions

Most activities in Linux involve working with files, such as text or image files. Utilize directories, often referred to as folders, to effectively structure your files. Visualize directories as analogous to folders in a file cabinet. They act as containers for other files and directories, each having a distinct name, similar to files.

List all the file names following the “rm” command options to delete them collectively. Separate the file names with spaces. Deleting multiple files simultaneously is achievable with the rm command. Simply list the file names with spaces to remove several files at once. For instance, use the format “rm file1 file2 file3.”

If you encounter an error message, review the message for details. It could highlight specific issues such as insufficient permissions, write protection, or non-empty directories.

You can erase directories with content using the dedicated “-r” or “-R” options. Exercise caution when using these commands, as they will permanently delete all data from the specified directory.

Use the “rmdir” command with the directory name to eliminate an empty directory. For instance, input the command “rmdir my_directory.”

Execute the command “rm directory/” to delete all files in a directory. We recommend exercising caution when removing files within the specified directory, as deleted files cannot be recovered.

The “-i” command option permits you to reconsider your decision before deleting the chosen files, requesting confirmation before proceeding with the deletion process.

To override a write-protected file, ensure you possess the required permissions. Verify permissions using the “chmod” or “chattr” commands, and subsequently utilize “rm” or “rmdir” accordingly.

Indeed, the “rm” command with the “-r” option can remove a parent directory and all its contents, including subdirectories and files. Exercise caution when using it to prevent irreversible data loss.