How to Remove Directory in Linux
Last Updated :
04 Jan, 2025
In Linux, directories are used to organize files and other directories into a hierarchical structure. Just like folders in Windows, directories in Linux can contain files, other directories, and links. Removing directories in Linux is a common task that you might need to perform as you manage files and organize your file system.
When you want to remove a directory in Linux, you need to consider whether it is empty or contains files and other directories.
To delete a directory in Linux, there are two commands you can use: rmdir
to delete empty directories and rm -r
for removing directories with files or subdirectories.
Method 1: Use 'rmdir' Command - Removing an Empty Directory
To remove an empty directory in Linux, you can use the rmdir
command. This command is straightforward and safe because it only allows you to delete directories that are empty, preventing accidental deletion of directories with content.
Syntax:
rmdir [options] [directory name]
Common Options for rmdir:
The rmdir
command includes the following options:
--ignore-fail-on-non-empty
: Doesn't show an error message when trying to remove a non-empty directory.-p
: Removes the directory along with its parent in the hierarchy.-v
: Provides a verbose output.--help
: Displays help text.--version
: Displays the command version.
Step 1: Check Your Current Directory
Firstly, open your Linux terminal. Now, before deleting the directory, use the pwd command to determine where you are in the filesystem:
pwd
Step 2: Remove the Directory
Now to delete an exsisting empty directory, use the rmdir command:
rmdir gfg
Step 3: Confirm the directory was deleted (Optional)
Again write ls command to be assured that the directory was deleted:
ls
gfg directory should no longer appear.
Method 2: Using the rm Command to Remove Directory
The rm command in Linux is a powerful tool for deleting files and directories. When it comes to removing a directory, the '-r' (recursive) option is crucial. This ensures that the directory and its contents are deleted.
Syntax:
rm [options] [file or directory name]
Common Options for rm:
The different rm
command options include:
- -
f
: Forces the removal of all files or directories. -i
: Prompts for confirmation before removing.-I
: Prompts once before removing more than three files or when removing recursively.-r
: Removes directories and their content recursively.-d
: Removes empty directories.-v
: Provides a verbose output.--help
: Displays the help text.--version
: Displays the command version.
Step 1: Navigate to Parent Directory
Open your linux terminal and navigate to the Parent Directory using the cd command:
cd /home/kali/Documents/
Navigating to the Parent DirectoryStep 2: Remove the Directory
Remove the Directory To remove the directory and its contents, use the rm command with the -r (recursive) option.
rm -r GeeksforGeeks
Removing the GeeksforGeeks directory in a Recursive mannerStep 3: Optional Confirmation
Confirm Removal If you want to confirm each deletion, you can add the '-i' option to prompt for confirmation.
rm -ri directory_name
Confirming Removal of DirectorySo using the rmdir command, we can delete the directory of our system. As you can see in the image, initially, when we created the directory, it was in our system. However, after the deletion, it was not shown in our file directories, and the file was successfully deleted.
Conclusion
In conclusion, removing a directory in Linux is a straightforward process that involves utilizing the 'rmdir' or 'rm' command, depending on whether the directory is empty or contains files. Linux users can efficiently manage their file system by employing these commands to delete unwanted directories and streamline their directory structure.
It is crucial to exercise caution while using the rm command, ensuring the correct directory is specified to prevent accidental data loss. Regularly cleaning up directories enhances system organization and optimizes storage space on Linux systems.
Similar Reads
How to remove a directory in R?
In this article, we will discuss how to remove a directory using R programming language. To remove a directory in R we use unlink(). This function deletes the named directory. Syntax: unlink(directory-name, recursive = BOOLEAN) Parameter: directory-name: a character vector with the names of the dire
1 min read
How to Manage Directories in Linux?
Directories in Linux or any operating system help to organize the data and files, which is then easier to find and manage. In this article, you will learn everything you will need to manage directories in Linux. We will try to cover every topic from creating, and copying to deleting the directories
6 min read
How to Copy a Directory in Linux
There is this famous quote for Linux by Jamie Zawinski that goes like this "Linux is only free if your time has no value." Certainly, that's true as people start using Linux they have to learn more about it, and when encountering it we have to solve it by ourselves, and that way we learn more about
5 min read
How to Rename a Folder in Linux
Renaming a folder in Linux is possible with Graphical User Interface (GUI) file managers as well as with powerful command-line utilities such as mv, rename, find, and rsync. Be it a novice utilizing Ubuntu, Debian, CentOS, Fedora, or Kali Linux or an expert dealing with bulk renaming in the terminal
12 min read
Remove a Symbolic link to a Directory in Linux
Symbolic links (symlinks) are very important when managing files under Linux since they allow users to make shortcuts of directories and files without copying data. While they help improve the system organization and efficiency, sometimes you might be required to remove a symlink from a directory to
7 min read
How to Check the Size of a Directory in Linux
Many times during working on Linux we may encounter situations, where we want to check the total size occupied by a directory, especially when working with large files and directories. Knowing the file or directory size can help a user perform tasks like storage allocation, size comparison, etc. At
7 min read
How to Remove Users from Groups in Linux?
Groups in Linux are an important part of organizing the system's access control. Creating separate groups for separate types of roles of users allows the administrator to manage the access control of the Linux system efficiently. It is an essential skill to understand how to add, remove, and update
4 min read
How to Move File in Linux | mv Command
The `mv` command in Linux is like a superhero tool that can do a bunch of cool stuff with your files and folders. Think of it as a digital moving truck that helps you shift things around in your computer. Whether you want to tidy up your folders, give your files new names, or send them to different
7 min read
How to Recover a Deleted File in Linux?
We all have often faced a problem where we have accidentally deleted some files in Linux, that we regretted deleting later on after we did not even find it in the trash. But what if we can recover them? Here, we will discuss How we can recover a Deleted File in Linux.Whenever we delete something fro
4 min read
How to Count Files in Directory Recursively in Linux
When exploring directories on your server, you may have come across folders with a large number of files in them. You might wish to know how many files exist in a certain directory or across many directories at times. To put it another way, you wish to count the number of files saved in a directory
5 min read