Table of Contents
This tip is about the how To remove Linux Directory. So read this free guide, How To remove Linux Directory step by step. If you have query related to same article you may contact us.
How To remove Linux Directory – Guide
Deleting a directory (also called a folder) in Linux is a common task that every user must perform at some point. This can be done from any installed desktop environment or from the command line with the rm command. Although this is a very basic feature, there are some important caveats to watch out for. In that guide, we’ll look at several examples of deleting a directory in Linux. Feel free to participate in your own system to master the rm command and GUI process.
But let’s get back to removing a directory in Linux. If you’re looking for ways to delete a directory graphically or via the command line, you’ve come to the right place. Programmers need to keep in mind that Linux treats everything like a file, even a directory. In another words, a directory is nothing more than a grouping of files placed together. For those moving from Windows OS or macOS, a directory is the same as a folder in Windows.
Delete a directory on Linux
Linux is a simple and straightforward operating system and offers a few ways to delete a directory on Linux. The first method is to graphically remove it from the desktop. For those using Gnome’s File, it is possible to 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 an encoder needs to remove several directories at the same time.
Delete a Linux directory using GUI
On Linux, graphical removal of a directory is easy and straightforward, similar to Windows or macOS. Furthermore, it must be remembered that all desktop environments are not the same. On the other hand, they don’t differ significantly and it’s easy to understand any Linux distribution.
For example, let’s use KDE Plasma is a graphical workspace environment. KDE Plasma is developed by an international software community known as KDE. Its main objective is to create free and open source software that can help the Linux community. It also develops cross-platform apps like Krita that can run on Unix, Android and even Microsoft Windows. Let’s look at the steps to remove a directory on Linux using the KDE Plasma GUI.
Delete a Linux directory using command line
Similar to major operating systems, even Linux offers a few ways to permanently remove a folder using the command line. The two Linux commands to exclude directories are rmdir and rm.
rmdir – The rmdir command is used to delete empty directories and folders in system.rm – The rm command is leveraged to remove directories that contain subdirectories.
The above commands are similar to del and deltree in Windows operating system. rmdir and rm are known as one of the most powerful commands in Linux. Furthermore, care must be taken 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, he can only recover it from a backup.
Removing directories with rmdir
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 anyone who wants to delete a directory or directories that are empty, as they don’t have to worry about whether they are empty or not. If the files are in them, the command will return with an error message. But first, let’s take a look at the syntax of the commands.
As mentioned earlier, rmdir excludes directories if they do not contain subdirectories and are empty. Here is the syntax for it:
rmdir directory-name
rmdir [option] directory name
For example, to remove abcd1, open the terminal and type the following expression:
rmdir abcd1An error message will be returned, such as the following:
rmdir: failed to remove ‘abcd1’: file or directory does not exist
In this case, you must manually move the contents 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 running it as it never removes a file. Also, one can use multiple directories and even write the folder path.
For example, to exclude multiple directories, all an encoder needs to do is enter the following expression:
rmdir abcd1 abcd2 abcd3In addition, for those who want to exclude a folder that is not the current directory, you can enter the full path of that specific directory to be excluded. Here is an example of the same:
rmdir / path / to / abcd
If the directory is empty, the command-line utility will silently and successfully delete it. Now, for example, you have several directories in a path and you want to exclude the empty ones, what do you do? It’s simple, just run the rmdir command and it will only delete the empty ones. rmdir doesn’t even make changes to files with a directory that has content. It is left untouched.
But rmdir works sequentially. So, if you run a command to delete five directories and the first one returns with the error message “Directory not empty”, the whole process ends. However, you 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 work / abcd1 folder has content, you must run the following command:
rmdir –ignore-fail-on-non-empty work / abcd1
There are several other options a programmer can use along with rmdir, such as the -p (parents) option. The -p command deletes not only the directory, but its parent directories as well. One reason -p is useful is that rmdir works from the target directory and falls back to the parent folder. Since rmdir only works when the folder is empty, it keeps rewinding up to the parent directory path.
Here is an example that demonstrates the method for using the -p option with rmdir.
rmdir -p work / abcd
Removing directories with rm
rm is another command-line utility that allows encoders to exclude directories. Furthermore, rm can also remove files and not just directories. Therefore, it is necessary that programmers take care before using it.
It’s one way rm is different from rmdir in that it removes not only empty directories, but also those that are full of content, if one is sure that’s a great way to exclude non-empty directories.
Interestingly, rm doesn’t work without using the -d or -r options. So if one wants to delete an empty directory, use the -de option and the -r (–recursive) option for those that have content. So, for example, to exclude an abcd1 directory including the entire contents of the folder, type the following:
rm -r abcd1
If the directory you are removing is write-protected, it will ask you to confirm the deletion. Now, if a programmer is sure and doesn’t want to see the prompt, they can use the -f option.
rm -rf abcd1
Like the rmdir command, you can use rm to remove multiple folders at the same time. All an encoder needs to do is separate the directory names with a “space” in the expression. Pairing the -r (recursive) option with rm will exclude all directories that have content, including subdirectories and files. Here is an example of the same:
rm -r abcd1 abcd2 abcd3
Interestingly, an encoder can also use the -i option with the rm command to confirm deletion of all subdirectories and files in a folder. The -i option is very useful because some folders have hundreds of files and using the -i option will only ask 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 to confirm the deletion, type “Y” and press Enter.
Now what if a user wants to exclude specific files that have something in common? Again, rm comes to the rescue and can easily remove specific files using regular expressions. For example, to exclude everything that ends with _xyz in the current directory, an encoder would type the following:
rm -r * _xyz
However, the programmer should be cautious when using regular expressions with rm as it is risky. The best way is to list all the directories first using the ls command. Using ls, one can see all directories before they are removed.
It is also possible to leverage rm to remove empty directories. To do this, an encoder must use the -d (directory) option. Interestingly, you can even use wildcards like * and? typing directory names is similar to just typing a filename. The expression will look like this:
rm -d abcd1
To remove multiple empty directories at the same time, type the following:
rm -d abcd1 abcd2 abcd3
As you may have noticed, there are several ways to delete a directory and you can use any one of them. While a programmer can use rmdir to remove empty folders, the same can be done with the rm command. But caution is required, especially when using the rm -rf command, as data can be lost forever.
It is always possible to use the tree command to better understand which files will be removed before executing the rm expression. First, the user must install a package before using the command. For Ubuntu users, you can take advantage of the apt-get command to install packages. For other Linux distributions, programmers can take advantage of the package management tool for that specific distribution. Therefore, the expression would be something like this:
sudo apt-get install tree
The tree command provides users with a simple understanding in a diagram formation of the directory structure and content, such as files below the directory.
Interestingly, a programmer can even specify a path to the tree command, so it starts with another directory on the file system.
tree path / to / directory
In addition, you can also use the –preserve-root, –one-file-system, –no-preserve-root options with the rm command. But it is highly recommended only for advanced programmers. The reason behind this is that a small accident or error can lead to deletion of the entire file system and therefore one should always avoid using it.
Final note
I hope you like the guide How To remove Linux Directory. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.