- How do I delete a directory in Linux terminal?
- How to delete a directory in Linux terminal
- How do I delete a full directory in Linux?
- Conclusion
- 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 to Delete a Directory in Linux
- Delete a directory using rmdir command
- Remove a directory using rm command
- Using find command
- Remove an empty directory
- Conclusion
- Delete / Remove a Directory Linux Command
- Commands to remove a directory in Linux
- rmdir command syntax to delete directory in Linux
- Delete directory Linux Command
- How to see a diagnostic message for every directory processed
- Removing directories with rmdir and wildcards
- Linux remove entire directory including all files and sub-directories command
- Are you getting permission denied error message while removing directories?
- Use find command to delete unwanted directories
- How to find and remove all empty directories
- Conclusion
How do I delete a directory in Linux terminal?
Let us see how to delete directories in Linux using the command line.
How to delete a directory in Linux terminal
Say you want to delete a directory named /home/vivek/data/, run:
rmdir /home/vivek/data/
rmdir -v
/data/
Verify directory deleted from the system with help of ls command:
ls /home/vivek/data/
ls
/data/
Please note that when attempting to remove a directory using the rmdir command, the directory must be empty. Otherwise, you might see an error message that read as follows on screen when execute rmdir -v /home/vivek/projects/ :
- 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 ➔
How do I delete a full directory in Linux?
As I said earlier rmdir command remove the DIRECTORY(ies) if they are empty. But, how do you delete a full directory that has many files and sub-directories? The solutions is to pass the -rf option to the rm command. The syntax is:
rm -r dir1
rm -rf dir2 dir3 /path/to/foo/
rm -rfv /path/to/bar/dir/
rm -rfv /home/vivek/projects/
Where,
- -r – Delete directories and their contents recursively
- -f – Forceful option i.e. ignore nonexistent files and arguments, never prompt for anything
- -v – Be verbose. Show what rmdir or rm command doing with given directory
- -i – Prompt before every removal of file/dir
- -I – Confirm (display prompt) once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes. Useful when working on a large number of files on Linux
Conclusion
This page showed how to delete both empty and non-empty directories along with all files/sub-directories using rm and rmdir command in Linux terminal application.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
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 to Delete a Directory in Linux
Earlier in one of the tutorials, we have explained how to create directory in Linux. Now let’s check how to delete a directory in Linux which is either empty or having subdirectories with files. This is especially when you need to free up some space on your system in order to save more files or install additional packages.
There are many ways in which you can remove a directory in Linux. You can make use of the file manager if you are using a GUI system such as GNOME, MATE or KDE Plasma, or you can do it over the terminal.
When working with a GUI system deleting a directory takes it to the crash can, the equivalent of recycle bin in Windows from where it can be restored. However, the scenario is different when working on a command line on a minimal system because once a directory is deleted, it is permanently removed and cannot be recovered.
This tutorial will take you through various ways in which you can delete a directory in Linux.
Delete a directory using rmdir command
The rmdir command, short for’remove directory’, is a command-line tool that is used to delete empty directories. The operation will be successful if and only if the directory is empty. The syntax for deleting a directory is as follows:
For instance, to remove an empty directory called ‘mydirectory’, run the command:
If the directory is not empty, an error will be displayed on the screen as shown:
The error is a clear indication that the directory contains either files or folders or both.
Remove a directory using rm command
Short for remove, the rm command is used for deleting both empty and non-empty directories.
The rm command is usually used for removing files in Linux. However, you can pass some arguments that can help you delete directories. For example, to remove a directory recursively ( remove the directory alongside its contents), use the recursive option -r (-R or —recursive) as shown below.
If a directory is write-protected, you will be prompted whether to continue deleting the files inside the directory and the directory as a whole. To save you the annoyance and inconvenience of constantly bumping into such prompts, add the -f option to force the deletion without being prompted.
Additionally, you can delete multiple directories at a go in a single command as shown in the command below. The command deletes all the directories and their subdirectories without prompting for deletion.
To exercise more caution, you can use the -i option which prompts for the deletion of the directories and subdirectories. However, as we saw earlier, this can be quite annoying especially if you have several subfolders and files. To address this inconvenience, use the -I flag to prompt you only once.
When you hit y for ‘Yes’, the command will remove all the subfolders and files in the directory without prompting any further.
To remove an empty directory, pass the -d option as shown below.
Using find command
Find command is a command-line tool that helps users search for files as well as directories based on specific search criteria/pattern or expression. Additionally, the command can be used to search for directories and delete them based on the specified search criteria.
For example, to delete a directory called ‘mydirectory’ in the current directory, run the command below.
Let’s break down the parameters in the command
( . ) — This denotes the directory in which the search operation is being carried out. If you want to carry out the search in your current directory use the period sign (.)
-type d — This sets the search operation to search for directories only.
-name — This specifies the name of the directory.
-exec rm -rf — This deletes all directories and their contents.
<> +- — This appends all the files found at the end of the rm command.
Let’s take another example:
Remove an empty directory
If you wish to remove all empty directories use the following command:
Again, let’s break this down
. — This recursively searches in the current working directory
-type d — This keeps the search to directories only
-empty — This restricts the search pattern to empty directories only
-delete — This will delete all the empty directories found including subdirectories.
If you have lots of empty directories then use a shell script to delete empty directory.
Conclusion
In this tutorial, we looked at how to delete a directory in Linux using the rm, rmdir and find commands. We hope you can comfortably delete a directory in Linux whether it contains files and other subdirectories, or simply if it is empty. Give it a try and get back to us with your feedback.
Источник
Delete / Remove a Directory Linux Command
Commands to remove a directory in Linux
There are two command to delete a folder in Linux:
- rmdir command – Deletes the specified empty directories and folders in Linux.
- rm command – Delete the file including sub-directories. You can delete non-empty directories with rm command in Linux.
Let us see some examples and usage in details delete the directories.
rmdir command syntax to delete directory in Linux
The rmdir command remove the DIRECTORY(ies), if they are empty. The syntax is:
rmdir directory-name
rmdir [option] directory-name
Open the terminal application and run command to delete given directory. For example, delete a folder named dir1:
rmdir dir1
Delete directory Linux Command
Open a command line terminal (select Applications > Accessories > Terminal), and then type the following command to remove a directory called /tmp/docs:
rmdir /tmp/docs
If a directory is not empty you will get an error message that read as follows:
rmdir letters
Sample outputs:
You can cd to the directory to find out and list all files:
$ cd letters
$ ls
Delete those files or directories. In this next example, remove data, foo and bar if bar were empty, foo only contained bar and data only contained foo directories:
cd /home/nixcraft
rmdir -p data/foo/bar
Where,
- -p : Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component.
How to see a diagnostic message for every directory processed
Pass the -v option to the rmdir command:
$ rmdir -v dir1
Sample outputs:
Removing directories with rmdir and wildcards
We can use wildcards such as ‘*’ and ‘?’ to match and delete multiple directories. For example:
$ ls -l dir*
We have three dirs named dir1, dir2, and dir3. To delete all directories starting with ‘dir’ in the current, you would use the following command:
rmdir -v dir*
Linux remove entire directory including all files and sub-directories command
To remove all directories and subdirectories use the rm command. For example, remove *.doc files and all sub-directories and files inside letters directory, type the following command:
Warning : All files including subdirectories will be deleted permanently when executed the following commands.
$ rm -rf letters/
Sample session:
Where,
- -r : Attempt to remove the file hierarchy rooted in each file argument i.e. recursively remove subdirectories and files from the specified directory.
- -f : Attempt to remove the files without prompting for confirmation, regardless of the file’s permissions
Are you getting permission denied error message while removing directories?
Only owners can delete their directories. However, a sysadmin can delete any directories created by anyone on the system. The syntax is:
sudo rmdir /path/to/dir/
sudo rm -rf dir2
When prompted, you need to provide root user or sudo user password.
Use find command to delete unwanted directories
Say you want to find out all directories named ‘session’ and delete them in the current directory, run:
find . -type d -iname ‘session’ -delete
- 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 ➔
How to find and remove all empty directories
Run:
find . -type d -iname ‘session’ -empty -delete
Where,
- -type d : Only search for directories and ignore all other files.
- -iname ‘session’ : Search directory named ‘session’. You can use wildcards here too. For example, -iname ‘dir*’ .
- -empty : Only match empty directories
- -delete : Deletes all found empty directories only
To delete all ‘.DS_store’ directories stored in /var/www/html, run:
sudo find /var/www/html/ -type d -name .DS_Store -exec rm <> \;
OR
sudo find /var/www/html/ -type d -name .DS_Store -exec rm <> +
The -exec option to the find command run an external command named rm to delete all files. The “ rm <> +/ ” is a better option as it uses one rm command to delete all .DS_Store directories.
Conclusion
This page showed how to delete a directory when it is empty. Further, it showed, how to remove folders using the rm and rmdir commands. See rm help page page for more info:
- For more information read man pages: rm(1)
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник