PuTTYgen

Download PuTTYgen - Putty key generator

How to Remove or Delete a Directory in Linux

How to Remove or Delete a Directory in Linux

Linux is the best-known open-source operating system that is popular among programmers and computer professionals. Often new developers come across instances on Linux, where they are not sure on which command to use. For example, one of the most common queries among amateur developers is to know how to delete or remove the directory in Linux.

Not all programmers become experts overnight, and therefore we are here to help them in a step by step guide to delete a directory in Linux. We will be covering all the methods that programmers can use and choose the one that they find convenient.

Before we start, let us understand why Linux is so important and the reason there are more and more new Linux programmers. First and foremost, Linux is open-source, so there is always someone to help, and a programmer can never fill isolated. There are tons of resources online that can help anyone learn and master Linux. While a very common preconception that Linux is only for the developer is very untrue, as Linux is more common than many might believe.

It is the underpinning operating system of every Android phone. Android-based phones make up for more than seventy per cent of the total mobile market share. More so, almost every website is powered by Linux-based servers. Therefore, it makes it one of the most widely used operating systems in the world.

However, coming back to removing a directory on Linux. You have come to the right place if you are looking for ways to delete a directory either graphically or using the command line. Programmers must remember that everything is considered a file on Linux, even a directory. In other words, a directory is nothing but a group up files placed together. For those switching from Windows operating system or macOS, a directory is the same as a folder in Windows.

Ways to Delete a Directory on Linux

Linux is a simple and very straightforward operating system and offers a couple of ways to delete a directory in Linux. The first method is by removing it graphically from the desktop environment. For those who are using Gnome’s File can delete a directory using the graphical user interface.

Another way is to use the command line, which is more effective. The latter is common when a system is using a headless server. The command-line option is even more effective when a coder has to remove multiple directories at the same time.

Cautionary Points

Programmers must remember that graphically removing a directory moves it to the Trash. Therefore, it does not delete it permanently, and one can recover it. So, if you are sure that you don’t need the file, then go to Trash and empty it. However, when it comes to the command line one has to be extra careful as it deletes a directory permanently. Also, coders require the write permissions to the folder before removing it. If one does not have it, then the error message -‘Operation not permitted’ will popup.

Delete a Linux Directory Using GUI

On Linux, graphically removing a directory is easy and straightforward akin to Windows or macOS system. More so, one must also remember that all desktop environments are not the same. On the flip side, they do not differ significantly, and it is straightforward to understand any Linux distribution.

For example, we will use the KDE Plasma is a graphical workspace environment. The KDE Plasma is developed by an international software community known as KDE. Their main objective is to create free and open-source software that can help the Linux community. It also develops cross-platform application such as Krita that can work on Unix, Android and even Microsoft Windows.

Let us look at the steps to remove a directory on Linux using the KDE Plasma GUI.

  • Open file manager and select the directory that you want to remove.
  • Right-click on the directory and select ‘Move to Trash’ from the available options. One must also remember that other desktop environments can show different options, instead of “Move to Trash.” The other options include “Delete.” Some environments can also display both options, and it is always best to choose “Move to Trash” as one can recover it on a later stage.
  • After clicking “Move to Trash”, the file manager will reaffirm if you really want to remove the directory.
  • Conform, and the directory will move to Trash.
  • Now, in case you are sure that you don’t need the directory then delete it from the Trash as well.
  • Open Trash and right-click on the directory to delete permanently. Another method is to click on the white space that will provide multiple options. Choose “empty the trash” to say forever, say goodbye to the directory.

Programmers must remember that there is no coming back from it and one can not recover it ever.

Delete a Linux Directory Using Command Line

Akin, to major operating systems, even the Linux offers a couple of ways to permanently remove a folder using the command line. The two Linux commands to delete directories are rmdir and rm.

  • rmdir - The rmdir command is used to delete empty directories and folders on the system.
  • rm - The rm command is leveraged to remove directories that contain sub-directories.

The above commands are similar to del and deltree on Windows operating system. The rmdir and rm are known as one of the most powerful commands on Linux. More so, one must also take caution that unlike the GUI option, deleting files using the command line does not move the folder to the trash. The directory is permanently deleted from the system. However, if a programmer accidentally removed a file using the command line, then they can only recover it from a backup.

Below we will explain both of the above commands in detail with examples.

Removing Directories With rmdir

The rmdir is a command-line utility that programmers use to delete directories on a Linux machine. The command is often used to remove empty folders. Therefore, it is convenient for those who want to delete a directory or directories that are empty as they do not have to worry whether they are empty or not. If the files in them, then the command, will return with an error message.

But first, let us look at the syntax of the commands.

As aforementioned that the rmdir deletes directories if they do not contain any sub-directories and are empty. Here is the syntax for the same:

rmdir directory-name

rmdir [option] directory-name

For example, to remove the abcd1, open the terminal and enter the following expression:

rmdir abcd1

An error message will return, such as the following:

rmdir: failed to remove 'abcd1': No such file or directory

If this is the case, then one has to manually move the content of the directory or use the rm command-line utility.

It is one of the significant differences between the rmdir and rm commands. Therefore, using the rmdir command is very beneficial as programmers never have to worry about deleting when executing it as it never removes a file. Also, one can use multiple directories and even write the path of the folder.

For example, to delete multiple directories, all a coder has to do is enter the following expression:

rmdir abcd1 abcd2 abcd3

Also, for those who want to delete a folder that is not the current directory, then one can enter the full path of that particular directory that is to be deleted. Here is an example of the same:

rmdir /path/to/abcd

If the directory is empty, then the command-line utility deletes it silently and successfully. Now, for instance, you have multiple directories in a path and wish to delete the empty ones, what do you do? It is simple you just run the rmdir command, and it will only delete the empty ones. The rmdir does not even make any changes to the files with a directory that has content. It is left untouched.

But the rmdir works in a sequential manner. So, if you execute a command to delete five directories and the first one returns with the error message “Directory not empty”, then the whole process is terminated. However, one can use the following command : --ignore-fail-on-non-empty, to force the process to continue and ignore the directory that files. So, for example, if the folder work/abcd1 has content, then one has to execute the following command:

rmdir --ignore-fail-on-non-empty work/abcd1

There are various other options that a programmer can use along with the rmdir such as the -p(parents) option. The -p command deletes not only the directory but also its parent directories. One reason that -p is handy is that the rmdir works from the target directory and then work backwards to the parent folder. As the rmdir only works when the folder is empty, it keeps stepping back up to the path of the parent directory.

Here is an example that demonstrates the method to use -p option with rmdir.

rmdir -p work/abcd

The result is that the above command deletes both "work" and "abcd" folders.

Removing Directories With rm

The rm is another command-line utility that allows coders to delete directories. Beyond that, rm can also remove files and not only directories. Therefore, it is a must that programmers take caution before using it.

It is one way the rm is different from rmdir as it removes not only empty directories but also the ones that are filled with content if one is sure that it is a great way to delete non-empty directories.

Interestingly, the rm does not function without using the options -d or -r. So, if one wants to delete an empty directory, then use the -d option and use -r (--recursive) option for the ones that have content. So for instance, to delete a directory abcd1 including all of the content in the folder, type the following:

rm -r abcd1

In case the directory you are removing has write-protection than it will prompt to reaffirm the deletion. Now, if a programmer is sure and does not want to see the prompt, then one can use the -f option.

rm -rf abcd1

Similarly to the rmdir command, one can use the rm to remove multiple folders at the same time. All a coder has to do is separate the directory names with a “space” on the expression. Pairing with the -r (recursive) option with rm will delete all directories that have content including sub-directories and files. Here is an example of the same:

rm -r abcd1 abcd2 abcd3

Interestingly, a coder can also use the -i option with the rm command to confirm the deletion of all subdirectories and files in a folder. The -i option is very handy as some folders have hundreds of files and using -i option will only prompt for confirmation once instead of individually for all files. Below is an example of using rm with the -i option:

rm -rI abcd1

The command will return for confirmation of the deletion, type “Y” and press Enter.

The expression will remove all files recursively without prompting for permission to delete each file in the folder. The rm is very useful and is often regarded as one of the most popular commands on Linux.

Now, what if a user wants to delete specific files that have something in common? Once again, the rm comes to rescue and can easily remove particular files using regular expressions. For instance, to delete all the that ends with _xyz in the current directory, a coder has to type the following:

rm -r *_xyz

However, the programmer must be cautious when using regular expression with rm as it is risky. The best way is to first list all the directories by using the ls command. Using ls, one can view all the directories before being removed.

One can also leverage the rm to remove empty directories. To do so, a coder has to use the -d (directory) option. Interestingly, one can even use wildcards such * and ? when typing directory names akin to just typing a file name. The expression will look something like this:

rm -d abcd1

To remove multiple empty directories at the same time, type the following:

rm -d abcd1 abcd2 abcd3

As one must have noticed that there are multiple ways to delete a directory and one can use any of them. While a programmer can use the rmdir to remove empty folders, the same can be possible with the rm command.

But cautious is required, especially when using the rm - rf command as one could lose the data for good.

One can always use the tree command to understand better what files will be removed before executing the rm expression. First, a user has to install a package before using the command. For Ubuntu users, one can leverage the apt-get command to install packages. For other Linux distributions, programmers can leverage the package management tool of that particular distribution. So the expression would look something like this:

sudo apt-get install tree

The tree command gives users a simple understanding in a diagram formation of the directory structure and the contents such as files beneath the directory.

Interestingly, a programmer can even specify a path to tree command, so it begins with another directory from the files system.

tree path/to/directory

Beyond that, one can also use the --preserve-root, --one-file-system, --no-preserve-root, options with the rm command. But it is highly recommended for advanced programmers only. The reason behind it is that one minor accident or mistake could lead to the deletion of the entire file system, and therefore, one should always avoid using it.

Removing Files With rm

While we have learnt to remove a directory with rm, it is best to also know on how to delete a single file using the rm command. It is essential to learn it as it comes handy, especially when a coder wants to remove a single file from a current directory.

Here is the simplest expression to remove a file named - “work_1.txt.”

rm work_1.txt

In case, the file is placed in another directory; then one has to provide the path and the file name. Below is the example for the same.

rm ./path/to/the/file/work_1.txt

The rm command is convenient as a programmer can even delete multiple files. Check out the below example:

rm work_1.txt work_2.txt work_3.txt

As aforementioned, the rm command is highly useful on Linux systems as it saves a lot of time and eliminates the manual work. For instance, if you want to delete all files .png files, then it would be a cumbersome task to find and manually remove all files. But instead using a wildcard such as * and ?, one can easily remove all specific files from the current directory. The * denotes multiple characters, meanwhile the ?, indicates a single character. The below example shows the method to delete all .png files in the current directory:

rm *.png

Now, type the following expression to remove all files with a single character extension:

rm *.?

The above will delete a file with a single extension such as work.3. Therefore, it won’t remove a file with the name - work.32. The command can prompt permission to delete if the file has write-protection. All a user has to do is type “Y” and hit “Enter.”

Akin to deleting the directory with rm, one can even use the options -i and -f for removing files. For new programmers, it is best to use -i (interactive) option as it will ask them to reaffirm before deletion. The -f (force) option is the opposite and removes files without reconfirmation. Below are the examples of using -i and -f option for removing files.

rm -i *.png

rm -f work_1.txt

Removing Directories With find

While the two most common command-line utilities to delete a directory are rm and rmdir, there is one other option as well. To one’s surprise, a programmer can even leverage the find command to delete a directory. It is a command-line utility that permits programmers to search for files and directories and then perform a specified action on the files and folders that match the expression.

One another important use of the find command is to remove directories. For example, if one wants to delete all folders that end with _tmp in the current directory, then type the following command:

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

Let’s breakdown the expression.

  • -type d - it will limit the search to directories and won’t include files.
  • -name ‘*_tmp’ - it only lists folders that end with _tmp.
  • -exec - it carries out an external command which in this case are rm -r.
  • {} + - it attaches a file to the end rm command.

Now, what if one wants to remove all empty directories in a directory tree? Deleting an empty directory is very easy and let's check the below example:

find /dir -type d -empty -delete

Let us understand the above expression. The /dir and -type d indicate the same as the previous example. Meanwhile -empty option limits the search to only empty directories. Also, -delete option will remove all empty files in the subtree. Programmers must remember the -delete will only remove empty folders.

One must take caution before using the -delete option. The find command is considered as an expression, and if a programmer adds -delete option, it will remove everything empty from the specified location. It is best to use -delete as a last resort. Also, it wouldn’t harm to test the expression before executing on a vital directory.

Common Errors Using rm, rmdir and find

Each of the command-line utilities is very beneficial and eases the work of removing files. But it is not always smooth sailing as at times one can come across error messages. As aforementioned, the most common error message when using the rmdir command is one witness is “Directory not empty.” Programmers can always bypass it by using the --ignore-fail-on-non-empty command. However, there are multiple other errors that one can face when executing the command.

For example, one can get the error /bin/rm: Argument list too long when executing the find and rm command. The error is common when the rm command attempts to delete a folder with a huge number of files. It happens because the size of the data is more than the size limit of the command line argument.

However, coders don’t have to worry as there are multiple solutions to overcome the issue. Programmers can use cd to the directory or even use the loop to delete subdirectories sequentially. The best solution for the error is to remove all files in the current directory with the find command and then try and delete the folder. Here is the expression for the same:

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

The above is a detailed brief on ways to delete or remove a directory from Linux. Linux offers flexible and powerful commands to perform any task. Some Linux users prefer GUI interface while others use a headless system such as the Raspberry Pi that entirely functions on command-line.

An additional perk of using Linux is that it does not matter whether you use Bash or any other shell; it is easy and convenient to perform most tasks. Coders can quickly delete directories from the terminal command.