- 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
- 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
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.
Источник
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
Источник