For Linux users and system administrators, learning to utilize the Linux command line is a must. It allows you to perform basic maintenance operations like creating new files, going to specific folders, relocating existing ones, and how to delete a directory in Linux. Deleting a folder or directory using GUI, like in Windows 10 or 11, in Linux is a straightforward process.
However, you can also remove directories using SSH terminal commands if you don’t have access to the GUI. And that is what we will cover in this article.
What's In Here
Before You Start The Linux Remove Directory and Contents Commands
Before jumping and executing the Linux delete folder command lines, you must ask yourself the questions below.
- Do I need to run a Linux delete directory and contents on the server?
- Do I have the privilege to delete files and directories from the Linux server – or at least sudo access?
- Does the Linux server allow SSH connection so that I can connect using a terminal application tool such as puTTY?
- If I execute a Linux delete folder, will it affect other users who probably own those files or directories? Or are there any dependencies from other applications that may have an impact later on?
- Do I need to delete the entire directory/folder or only the files inside?
You must know these things first before you execute the Linux delete command. Because if you accidentally remove or delete a file, recovering them can be tedious, and worst, it is impossible.
Understanding The Linux Commands In To Delete A Directory
You can delete a directory from the terminal window or command line using one of two Linux commands:
rmremoves whole directories, together with their subdirectories and files.
- You can use the
rmdircommand to delete an empty directory.
It is important to note that the rm and rmdir command permanently remove directories without moving them to the Trash directory. In other words, what this means is that you cannot restore a folder removed using these commands.
Although it is not impossible to restore deleted files or directories, it can be tedious for non-advance Linux users. We can discuss how you can restore files or folders if you accidentally deleted them.
Different Linux Delete Directory and Contents Commands
First, you should know the name of the directory before deleting it. You can use the
ls command to find files and directories. Then Linux command
pwd can let you know your current working directory. Lastly, you can use the command
tree to show the directory hierarchy.
Here are some Linux delete directory command combinations you can use depending on your needs.
|Linux Commands||What does the command do?|
|This will delete or remove an empty directory.|
|This command will delete/remove directories and their contents recursively but will fail on write-protected folders/files.|
|Combining the |
|This will ONLY remove files and will prompt the user before deletion if the files are more than 3.|
|Unlike the “|
|Will delete the empty directory and its ancestor or subsequent sub-directories. For example, you have a parent directory, |
|Will verbosely delete the empty directory. This means the user will see what is being deleted.|
Now that you know the command to remove a directory in Linux, you can confidently manage your server.
Linux command-line deletion of directories and files requires knowledge of how to use the rm and rmdir commands. Remember the rule of thumb, the
rm command removes files and non-empty directories. Whereas the
rmdir command only removes empty folders.
It is worth remembering that Linux does not have a recycle bin or garbage folder. Using the command line, deleting files and folders will result in their permanent deletion. Therefore, before deleting the files and folders on your server, use these commands cautiously or make a backup.