- Linux Delete Folder Using rmdir and rm Command
- Linux delete folder using rmdir
- Linux remove folder
- How to get additional information about what is happening when running rmdir
- Remove Folder and Its Ancestors
- Delete All Files and Folders Including Subdirectories
- GUI File Manager
- Conclusion
- How to Delete Files and Folders in Linux
- The rmdir Command
- Remove Folders in Linux Using the rmdir Command
- The rm Command
- Remove Files in Linux Using the rm Command
- Wrap Up
- How to Delete a Directory in Linux
- How to Delete a Directory in Linux
- Linux Delete Directory: rm Command
- Force Delete a Directory and Its Contents
- Delete Directory Linux: rmdir Command
- Conclusion
- How do I force delete a directory in Linux?
- How to force delete a directory in Linux
- Removing a directory that contains other files or directories
- How do I remove a full directory in Linux with verbose output?
- Delete a non-empty directory in Linux terminal
Linux Delete Folder Using rmdir and rm Command
Linux delete folder using rmdir
Let us see some examples that show how to remove a folder in Linux.
Linux remove folder
In this example, delete the directory called /tmp/letters/
rmdir /tmp/letters
For example, the following command would remove two empty folders named alpha and delta in the current directory:
rmdir alpha delta
How to get additional information about what is happening when running rmdir
Pass the -v (verbose) option as follows:
rmdir -v dir1
rmdir -v foo bar
Remove Folder and Its Ancestors
The -p option can delete directory and its subdirectories/sub-folders:
rmdir -p dir1/dir2/dir3
Where rmdir command command options are as follows:
- -p : Linux remove folder i.e. remove the parent folders of the specified directory
- -v : Output a diagnostic for every directory processed
- —ignore-fail-on-non-empty : Ignore each failure that is solely because a folder is non-empty.
Delete All Files and Folders Including Subdirectories
Use the following syntax:
rm -rf /path/to/dir
For example, delete /home/vivek/docs and all its subdirectories including files, enter:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Where rm command options are as follows,
- -r : Recursively delete a directory by first deleting all of its contents
- -f : Linux delete folder forcefully
- -v : Verbose output
GUI File Manager
The Nautilus file manager (GNOME desktop) provides a simple and integrated way to manage your files and applications. Just open it from Places menu and select folder and hit delete key.
Fig.01: Gnome File Browser
Conclusion
In this tutorial, you learned how to delete folders in the Linux using rmdir and rm command-line options. The rmdir command is used to remove empty folders in Linux. The rm command used to delete both files and folders even if they are not empty. See rmdir help page here for more info.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How to Delete Files and Folders in Linux
Linux command line fundamentals are absolutely essential for every future system administrator and advanced Linux user. Today we’ll cover another basic function – deleting files and directories in Linux using the command line.
The rmdir Command
The command used to delete empty directories in Linux is rmdir.
The basic syntax of this command is easy to grasp. here’s an example:
- rmdir is the command
- [option] is an optional modifier that changes the way the command behaves
- DirectoryName is the directory you want removed
If no option is provided, rmdir simply deletes the directory whose name is provided as the destination. Before using this command, you’ll have to log into your VPS server using SSH. Here’s an article to help you out.
Remove Folders in Linux Using the rmdir Command
Before using the rmdir command we suggest you check the files present in a directory with the ls command. In our case, we have a directory named Dir1.
This command will delete the empty directory named Dir1. Simple enough, right?
You can also delete multiple directories by separating their names by spaces. For instance:
After executing this command, the directories named Dir1, Dir2 and Dir3 will be deleted.
Let say we have a directory named Dir3. Dir3 has subdirectories and files inside of it. Now if we use following command:
We will receive an error like this:
As you may have guessed from the output, rmdir only works with empty directories.
The rmdir is a smart utility, and only allows you to delete empty directories as a built-in safeguard to prevent accidental loss of data. Remember it’s almost impossible to recover deleted data on any Linux distribution.
The -p option lets you delete the directory as well as its parent directories.
This above command will delete Dir3 and its parent directories Dir2 and Dir1.
The -v option outputs a diagnostic text for every directory processed. Using this option will output a confirmation listing all the directories that were deleted.
The rm Command
The rmdir command is great for safely removing unused and empty directories. If you want to remove files or directories containing files, you would have to use the rm command.
The basic syntax of this command is similar to rmdir:
Remove Files in Linux Using the rm Command
Use the rm command to remove the file named article.txt:
If we have a directory with the name Dir1 containing subdirectories and files we will have to attach the -r modifier. The command would look like this:
The -r option recursively removes directories and their content.
Another helpful option is -i. This will ask you to confirm the files that will be deleted individually. That way you can avoid any unpleasant mistakes.
You can also delete empty directories using -d option. The following command will delete an empty directory with name Dir1:
You can use a wildcard (*) and a regular expansions to match multiple files. For instance, the following command will delete all pdf files placed in the current directory.
You can use variation of all the commands listed above to delete files with other extensions like .txt, .doc, .odt, etc.
The -f option lets you forcefully delete everything placed in a directory. The command would look like this:
The above command will delete everything recursively and forcefully, residing under the Dir1 directory without prompting anything on the terminal.
You can also delete more than one directory at a time. The following command will delete three directories Dir1, Dir2 and Dir3 in a single command.
Congratulations, you successfully mastered all the basic functions of the rm and rmdir commands!
Wrap Up
In Linux deleting a single file by accident can lead to major issues. That’s why it’s essential to master the two main commands for file and directory removal – rm and rmdir. In this article we cover these two commands and the various options that can be used with them.
We hope you found this article useful! Remember, once you delete a file or directory from Linux, you can’t recover it, so be extra careful! Good luck.
Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward’s goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.
Источник
How to Delete a Directory in Linux
You can delete a directory in Linux using the rm command. The rm command can delete a directory if it contains files as long as you use the -r flag. If a directory is empty, you can delete it using the rm or rmdir commands.
- Career Karma matches you with top tech bootcamps Get exclusive scholarships and prep courses
- Career Karma matches you with top tech bootcamps Get exclusive scholarships and prep courses
How do I delete a directory in Linux? This is a common question asked by Linux newbies. As you start working with the Linux operating system, it’s inevitable that you’ll want to delete a directory at some point.
In this article, we’re going to discuss two methods you can use to delete a directory in Linux. We’ll also provide an example of each of these methods so you can get to work deleting directories as soon as possible.
How to Delete a Directory in Linux
In Linux, there are two ways to delete a directory:
- Career Karma matches you with top tech bootcamps Get exclusive scholarships and prep courses
- rmdir: This command deletes an empty directory.
- rm command: This command deletes a directory including its subdirectories. You must use the -r flag with this command if the folder contains any files.
You may be accustomed to dragging folders into your computer’s trash can when you’re ready to delete them. The trash acts as a storage facility for documents you are going to delete. Before the file is deleted, you need to empty out your trash can.
When you’re using the Linux command line, you should know there is no trash can. Once you’ve deleted a file or a directory, it’s gone.
To learn more about the command line, read our guide to learn the Linux command line.
Linux Delete Directory: rm Command
The rm command allows you to delete files and directories. This command can be used to delete both empty and non-empty directories, unlike rmdir as we’ll discuss in a minute.
Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!
Venus, Software Engineer at Rockbot
Find Your Bootcamp Match
The syntax for the rm command is as follows:
There are two flags which allow you to delete a directory using rm. These are:
- -d: Delete an empty directory
- -r: Delete a non-empty directory and all of its contents
Suppose we want to delete a folder called “lib” in our current working directory. If we run the following command we can see what contents our folder contains:
Our command returns:
Because this folder contains files, we need to use the -r flag to delete it. We can do so using the following command:
Our command deleted the “lib” folder and all of its directories.
You can also use rm to delete multiple directories. To do so, you can specify multiple folder names after rm. Suppose you want to delete the folders “lib” and “templates” in your current working directory. You could do so using this command:
Force Delete a Directory and Its Contents
By default, the rm -r command will prompt you to confirm the deletion of a file or folder. This happens if a file you are trying to delete has been protected. If you want to override this, you can specify the -f flag, like so:
This command will delete all directories and subdirectories in the “lib” directory permanently. You will not be prompted to confirm whether you want to delete any files when you run this command. So, you should use this command sparingly and only when you’re absolutely sure you want to delete the contents of a folder.
Delete Directory Linux: rmdir Command
The rmdir command allows you to delete empty directories. This command is useful if you know a folder is empty and want to remove it from your system.
Suppose our current working directory contains the following files and folders (which we can see by using the Linux “ls” command):
The “config” folder is empty. Suppose you want to delete the folder “config” in your current working directory. You could do so using this command:
If we run ls to see the contents of our current folder, our command returns:
You can see that the “config” directory no longer exists. It’s important to note that you cannot use rmdir to delete a directory that contains a file. If our “config” directory contained a file, the following error would have been returned:
Conclusion
Using rm and rmdir, you can delete directories in Linux. rm deletes non-empty directories. The rmdir command delete empty directories. It cannot be used to delete a directory that contains any files.
If you want to learn more about these commands, type in man, followed by the name of the command, in your terminal. This will show you the Linux manual page for the command.
Before you delete a file, make sure you have chosen the right one to delete. There is no turning back when you use a command like rm or rmdir.
To learn more about Linux, read our How to Learn Linux guide. This guide contains courses and resources to help you along your journey toward learning Linux.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.
Источник
How do I force delete a directory in Linux?
How to force delete a directory in Linux
Here is how to forcefully delete a folder in Linux:
- Open the terminal application on Linux.
- The rmdir command removes empty directories only. Hence you need to use the rm command to remove files on Linux.
- Type the command rm -rf dirname to delete a directory forcefully.
- Verify it with the help of ls command on Linux.
Removing a directory that contains other files or directories
The syntax is as following for deleting a directory:
rm -rf dirName
Say you have a directory named /tmp/data/ that contains two files and one directory as follows:
ls -l /tmp/data/
If you run the rmdir command, you will get an error as follows:
rmdir /tmp/data/
As explained earlier rmdir only delete the DIRECTORIES, if they are empty. Therefore you must use the rm command to remove a full directory in Linux:
rm -rf /tmp/data/
Verify it:
ls -l /tmp/data/
Use rm command to delete a non-empty directory in Linux terminal
How do I remove a full directory in Linux with verbose output?
Pass the -v option to the rm command as follows:
rm -rfv dirname
For example, delete a full directory named /tmp/bar in Linux and note down the output on the screen:
rm -rfv /tmp/bar/
Where,
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
- -r : Recursive delete
- -f : Force delete a directory
- -v : Verbose output
Delete a non-empty directory in Linux terminal
In case if you don’t have the permission to delete the folder run rm command as a root user. Otherwise, you will see permission denied message on the screen. Therefore to avoid that prefix sudo at the beginning of the rm command :
sudo rm -rf dirName
OR
sudo rm -rf /somedir/
To remove a folder whose name starts with a ‘ — ‘, for example ‘-backups’, use one of these commands:
rm -rfv — -backups/
OR
rm -rfv ./-bacups/
Источник