Terminus by Warp
Removing Directories in Linux

Removing Directories in Linux

Razvan Ludosanu
Razvan Ludosanu
Founder, learnbackend.dev

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

The short answer

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

 $ rm -r directory

[#AI-command-search]Remind yourself of the syntax using AI Command Search[#AI-command-search]

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

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

[#forcing-removal-directories]Forcing the removal of directories with [.inline-code]rm -f[.inline-code][#forcing-removal-directories]

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

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

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

 $ rm -rf directory

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

 $ 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 ([.inline-code]/[.inline-code]) or that include wildcards ([.inline-code]*[.inline-code]) in their path.

[#safely-removing-dirs]Safely removing directories with [.inline-code]rm[.inline-code][#safely-removing-dirs]

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

[#interactive-mode]Removing directories in interactive mode[#interactive-mode]

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

 $ rm -ri directory

You can then either press [.inline-code]y[.inline-code] to confirm or [.inline-code]n[.inline-code] to skip, followed by [.inline-code]ENTER[.inline-code].

[#wildcard-pattern]Testing a wildcard pattern with [.inline-code]ls[.inline-code][#wildcard-pattern]

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

 $ ls -R dir_*

Once you are certain of your selection, you can then replace [.inline-code]ls[.inline-code] with [.inline-code]rm[.inline-code] to actually remove them:

 $ rm -R dir_*

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

[#rm-and-rmdir]Removing empty directories with [.inline-code]rm[.inline-code] and [.inline-code]rmdir[.inline-code][#rm-and-rmdir]

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

 $ rm -d directory

Or use the [.inline-code]rmdir[.inline-code] command, which is specifically designed for that purpose:

 $ rmdir directory

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

 $ rmdir -p dir/subdir/

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.