Removing Directories in Linux

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: January 31, 2024

On Linux and Unix-like operating systems, including Ubuntu and MacOS, directories can be removed and deleted using either the rm or rmdir bash commands.

The short answer

To remove an entire directory or folder, including the files and subdirectories it contains, you can use the rm command with the -r flag (short for recursive):

Bash
$ rm -r directory

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering remove directory in the AI Command Search prompt results in rm -rf directory, which you can then quickly insert into your shell by doing CMD+ENTER.

Forcing the removal of directories with rm -f

When trying to remove a read-only directory (write permission not set), the rm command will produce the following error message:

Bash
$ rm -r tmp
 rm: cannot remove 'tmp': Permission denied

To bypass this behavior, you can either change its permissions using the chmod command, or you can use the -f flag (short for force) in combination with the -r flag, which will remove it without prompting you for confirmation.

Bash
$ rm -rf directory

Note that in case the directory belongs to another user, you'll have to use the sudo command to gain elevated privileges in order to remove it.

Bash
$ sudo rm -rf directory

Also note that this command is potentially harmful to the operating system and must be used with extreme caution. As a rule of thumb, never use this command on directories that begin with a slash (/) or that include wildcards (\) in their path.

Safely removing directories with rm

Unlike when removing files and directories using the graphical interface, the rm command doesn't move the selected entries into the trash, but immediately and irreversibly erases them from the disk.

Removing directories in interactive mode

One option for safely removing directories, is to use the -i flag (short for interactive), which will prompt you for confirmation before attempting to remove any of them:

Bash
$ rm -ri directory

You can then either press y to confirm or n to skip, followed by ENTER.

Testing a wildcard pattern with ls

Another option for safely removing directories when using wildcards is to first test your pattern using the ls command, which will display the list of matched entries:

Bash
$ ls -R dir_*

Once you are certain of your selection, you can then replace ls with rm to actually remove them:

Bash
$ rm -R dir_*

Note that when using rm, the -r flag and the -R flag will have the same effect.

Removing empty directories with rm and rmdir

To remove empty directories only, you can use the rm command with the -d flag (short for directory):

Bash
$ rm -d directory

Or use the rmdir command, which is specifically designed for that purpose:

Bash
$ rmdir directory

The advantage of using rmdir, though, is that it allows you to recursively remove empty directories using the -p flag:

Bash
$ rmdir -p dir/subdir/
Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.