Skip to content

How to remove a Directory in Linux?

Linux has come a long way from being the clunky command line interface it once was. Most Linux distros come with beautiful desktops and immaculate interfaces that make the OS extremely easy to use. 

However, the terminal’s power remains, and it’s still the fastest, most effective and hassle=free way of getting just about anything done on Linux PCs.

In this article, we’re taking a look at how you can remove directories in Linux. 


How to remove a directory using the Linux terminal?

Directories in Linux can be removed using the rmdir or rm command. rmdir removes empty directories while the rm command can remove directories and their files recursively. 

Remove empty directories using either of the following commands.

rm -d directory
rmdir directory

To remove non-empty directories and all the files within then, use the rm command with the -r flag. 

rm -r directory

If the directory or the file is write-protected, you’ll be shown a confirmation prompt before deleting the file. If you don’t want to be prompted, use the -rf flag instead. 

rm -rf directory

Just like removing multiple files, to remove multiple directories, use the following command. You can even play around with the wildcard to delete similar directories at the same time. 

rm directory1 directory2 directory3

You can include -r flag to recursively delete multiple directories with the rm command.


How to delete a Linux directory with the find command?

Linux command-line utility find allows users to search for files or folders based on a certain condition and perform an action on each matching file or directory. 

The most common use case for this command is when the user has to delete multiple directories based on a pattern. For example, to remove all the directories containing ‘_old’ in their names inside the current working directory, you would need to run the following command.

find . -type d -name '**_old' -exec rm -r {} +

Here’s a breakdown of the command above.

  • /dir: recursively search the current working directory (.)
  • -type d: restricts the search to directories only. 
  • -name ‘*_old’: Searches for directories that end with ‘_old’.
  • -exec rm -r: executes a system command. In this case, it rm -r.
  • {} +: this will append any files found to the end of the rm command, deleting them in the process.

Deleting all empty directories in a directory tree

This task is a bit more complicated than simply churning out a rmdir command. Here’s the command you’ll have to use instead.

find /dir -type d -empty -delete

Here’s a breakdown of the command:

  • /dir: search recursively in the /dir directory.
  • type -d: restricts the search to directories only. 
  • -empty: restricts the search to empty directories.
  • -delete: deletes all directories matching the criteria. Note that this command can only delete empty directories.

Be sure to use the -delete command very cautiously. The command is evaluated as an expression and if you add the -delete option first, the command will delete everything below the specified starting point. 


/bin/rm: Argument list too long

You can encounter this error when you’re trying to delete a directory that contains a lot of files using rm. This happens when the number of files is larger than the system limit on the command line argument’s size. 

You can either use cd to head over to the directory manually or use a loop to remove sub-directories one by one. 

However, the easiest solution is to use the following find evaluation where /dir is the directory you want to delete. 

find /dir -type f -delete && rm -r /dir

How to delete a directory in Linux using the GUI?

If the terminal hasn’t grown on you and you’d prefer removing directories using the graphical interface, the process is pretty much identical to the one in Windows.

How to remove a Directory in Linux? | Candid.Technology

All you need to do is right-click the directory you want to delete and then click on Move to Trash. This will place the directories and their files/folders into the Linux trash. From here, you can either recover the files or delete them permanently.

Also read: How to install Kali Linux on Virtualbox?

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. We're an ad-supported publication. So, if you're running an Adblocker, we humbly request you to whitelist us.

We may earn a commission if you buy something from a link on this page. Thanks for your support.







>