Introduction
Understanding the core Linux commands is essential for server management because Linux is a widely used operating system for hosting services. Across all Linux individuals and system admins, understanding how to operate the Linux command line is an urgent requirement. It allows you to perform basic maintenance operations like making new files, accessing certain folders, and relocating existing ones.
In this post, we’ll demonstrate how to remove a directory in Linux command line to delete files and directories.
System Requirements
Before beginning, you must be aware of the following predetermined requirements:
- A Linux-based system.
- A sudo-privileged user account.
- the possibility to use the command line and terminal window.
In Linux, multiple users can interact at once. Learn how you can Add/Remove users and groups in Linux. A Complete Guide to useradd Command in Linux with Examples
How to Remove Directory in Linux?
You can delete a directory from the command line as well as the terminal window using one of the following remove a directory in Linux commands:
When using the rm command, all files and subdirectories in a directory get deleted completely.
By using the rmdir command, empty directories get deleted.
Delete a Directory in Linux using rmdir Command
Linux has a command called rm that attempts to undermine directories and files. It adopts the following syntax:
There are various rm command parameters, including:
- Removes all directories or files with the -f option.
- Before uninstalling, the -i option asks for authorization.
- When deleting over three files or performing a recursive deletion, the -I option prompts the user once.
- The -r option recursively deletes folders and their contents.
- -d: Deletes empty folders.
- A verbose output gets provided via the -v option.
- —help: Makes the help text visible.
- —version: Indicates the command version.
When attempting to delete a directory in Linux with the rm command with no parameters, the following message appears:
By adding the -d parameter to the rm command, you can delete an empty directory:
Example:
To remove a directory that includes files and subdirectories, utilize -r flag. It has many text files in the Dir1 & Dir2 subdirectories:
In contrast to the -v flag, which produces a list of every stage of the procedure, the -r flag deletes the complete directory, with subdirectories and files:
Example:
You get prompted to confirm the removal of the directory when using the -i option. Enter after typing Y to verify.
Example:
If a directory is write-protected, it cannot get deleted automatically. Within this type of directory, include:
Example:
Utilize the following commands to know how to delete the directory:
Example:
To confirm removal, type Y. Use the -f switch or give the command sudo-level access to omit the confirmation:
Example:
Example:
By using the command prompt whether other files and directories are present inside the write-protected directory:
Remove a Directory in Linux using the rm Command
All empty directories can get deleted with the Linux rmdir command. This command seems to have the following format:
These choices are available with the rmdir command in Linux:
- When trying to delete a non-empty directory, the option —ignore failure on non-empty doesn’t display an error message.
- -p: Erases the hierarchy’s parent directory together with the working directory.
- -v: Produces output that is verbose.
- —help: Provides a help message.
- —version: Shows the versions of the command.
A non-empty directory causes an error when the rmdir command gets used on it. Therefore in this instance, the Test subdirectory gets found under the Example directory:
Adding them in the opposite order of the hierarchy will allow you to delete them using the rmdir command. The -v option’s output gives an outline of each stage in the process:
The -p parameter along with the name of the subfolder makes this process easier. With this, the subdirectory’s hierarchical parent is also removed:
Utilizing wildcards, you can execute the rmdir command to eliminate several directories with similar titles. To delete, for reference, the directories Example1, Example2, or Example3:
To process the arguments in Linux Xargs Command is required. Learn how to use the xargs Command in Linux
Cautionary Measures while using the rmdir Command in Linux
Due to the lack of a recycle bin as well as a trash folder, files and folders in the Linux system are permanently erased. Linux only allows you to restore accidentally deleted files and directories from backups.
As it only functions for empty directories, using rmdir command in Linux minimizes these unpleasant effects. If there are files in the folder, it will give you the following error code:
The rmdir function has the following syntax:
To avoid using a command line parameter, delete it from the syntax. Don’t forget that the command line considers the case. To avoid confusion, enter the folder name in uppercase if that’s the case.
For instance, the following command removes the Simple-Directory folder:
Moreover, the rmdir command support various empty directories. On Linux, you can remove all files in a directory by including the rmdir in the command via extra arguments.
So let’s look at several parameters for the rmdir command. A subdirectory or its parent directory can get deleted with the first option, -p. Use the rmdir command in Linux below if your directory path, for instance, is /Directory/SubDirectory:
On the Directory path, the above command will start by deleting the SubDirectory folder. Following the removal of the SubDirectory, the Directory folder will get erased if it is empty.
The -v command prompt parameter specifies the verbose option, and it represents the next choice. To confirm the removal of the chosen directory, this parameter will print a text. An example of this command is below:
It will appear in the output message:
RMDIR: ‘Simple-Directory’ removal of a directory
Conclusion
Using the rm or rmdir commands in Linux correctly is necessary to delete files and directory structures in Linux. The difference between the two commands is that the rm command removes files or non-empty directories, but the rmdir command removes just empty folders.
Keep in mind that Linux does not have a recycle bin/trash folder. By utilizing the command line, you can permanently delete directories and files. Because of this, use caution while using these commands or make a backup before removing any content on your hosting server. After going through this tutorial, you will be able to use the terminal window and perhaps the command line to delete directories in Linux.