- How do I remove a directory and all its contents?
- Remove a Directory in Linux – How to Delete Directories and Contents From the Command Line
- The Linux rm Command
- rm Command Syntax
- Linux rm Command Example
- Conclusion
- How to Delete Folders in Ubuntu Command Line
- Delete folders using rm command
- Force remove a directory and its contents
- Removing empty directories with rmdir command
- Conclusion
How do I remove a directory and all its contents?
The following command will do it for you. Use caution though if this isn’t your intention as this also removes files in the directory and subdirectories.
«-f» is «—force» which overrides some sanity checks and prompting. A safer command to start with would be rm -r directoryname .
@JimParis I think the word «safer» is relative. Suppose you are writing a script to run on a remote computer. That script has a command which is supposed to remove a directory. Here, it would be «safer» to use rm -rf directoryname coz you wouldn’t want your script to pause execution, because it’s waiting for user input. Of course, you have to be sure that deleting the directory would do no harm.
if rm -rf directoryname fails you, try using rm -R -f directoryname , or rm —recursive -f directoryname .
If you are not having any luck with these, you should consider reinstalling rm or switching shells.
These were the options available on my rm man page, I looked it up by typing man rm to view my options on recursive deletion and the force options.
Does your rm man page list -r ? What does it do? (Try it in a directory that you create just for testing purposes, with only dummy files (and maybe subdirectories) in it.) What operating system are you using?
P.S. If rm -r doesn’t work, that would be an OS issue, not a shell issue. (Strictly speaking, it would be an issue with the version of rm that you’re using, so you could address it by installing a different version of rm , or searching your system to see whether you already have a different version of rm in some directory other than /bin .)
Ah, right. I forgot to mention I’m on Ubuntu 14.04 When I ran man rm in my terminal, it gave me a text file with the less text viewer. I scrolled found an indented entry with a whole that had the -R and —recursive options cozied up with the -r option, signifying that all of those arguments are identical.
edit: have you tried sudo rm -r directoryName ? The unwritten rules of the basic commands is that -r will allow a program to run recursively on every file your filesystem (starting where ever you choose!) and that -f will forcefully do things, even if it’s dangerous. ‘cd’, ‘mv’, ‘ls’ mostly holds this principle true. ls -r / is gonna be a duzie, and cp -rf / /dev/null will destroy everything on your filesystem.
Other answers show how to completely remove a directory’s content, but IMO they don’t address the literal question of the original post — that is, how can one delete subdirectories (as opposed to usual files). In other words, how can one delete empty directory structures while keeping subdirectories containing files ?
This can be achieved with find :
find directoryname -type d -delete
This command will recursively search for directories ( -type d ) through directoryname and -delete them only if their subdirectories or themselves don’t contain any files.
Remove a Directory in Linux – How to Delete Directories and Contents From the Command Line
Ilenia Magoni
Linux is a popular open source operating system, and its features are often available in your development environment. If you can learn its basic commands, it’ll make your life as a developer much easier.
In this guide you will learn how to delete directories and files from the Linux command line.
The Linux rm Command
The rm (short for remove) command is pretty useful. Let’s learn its syntax and look at a few examples to see it in action.
rm Command Syntax
The syntax is shown below, with args being any number of arguments (folders or files).
Without options you can use it to delete files. But to delete directories you need to use the options for this command.
The options are as follows:
- -r , «recursive» – this option allows you to delete folders and recursively remove their content first
- -i , «interactive» – with this option, it will ask for confirmation each time before you delete something
- -f , «force» – it ignores non-existent files and overrides any confirmation prompt (essentially, it’s the opposite of -i ). It will not remove files from a directory if the directory is write-protected.
- -v , «verbose» – it prints what the command is doing on the terminal
- -d , «directory» – which allows you to delete a directory. It works only if the directory is empty.
Linux rm Command Example
Let’s take a project_folder directory as an example. It has these files and folders inside:
Let’s use this directory to show how the various options work.
You can add the option -v to all commands so that it will write down step by step what’s going on.
So, let’s start with the first option, -r . You just learned that this removes files and folders recursively. You can use it like this rm -r project_folder or also rm -rv project_folder as the verbose option.
It has deleted the project_folder directory and everything inside it, in the order shown.
Let’s recreate the folder and try again.
What happens if you don’t use the -r option and you try to delete the directory anyway? It will not allow it and will instead show an error:
To delete directories you can use the -d option, but if you try to use it in this case it will give an error as the folder is not empty.
The -i option make so that it asks about each action individually.
And you need to press y or n and then Enter after each query.
If you select y for all queries it will delete everything:
If instead you decide to not delete some files or folders, with n it will keep those files and continue with the rest:
The last option we haven’t seen so far is -f , which will suppress errors.
For example writing as below you would be trying to delete two non existing files – there is not a rat.png file, and dog.pmg has a typo and it gives two errors. With the -f option, you will not see the errors.
Conclusion
The Linux command line is pretty useful if you’re a developer. In this article, you have seen one of its possible commands, rm , that you can use to delete directories and files.
Enjoy this new tool in your arsenal!
How to Delete Folders in Ubuntu Command Line
You probably already know how to delete files in the command line with the rm command.
The same rm command can also be used to delete folders (called directories in Linux terminology).
rm -r directory_name_or_path
The -r option is important and I’ll explain it in the later section of this article.
Creating a new folder in Ubuntu is simple. Deleting them might not be that straightforward, especially if the folders are not empty.
There is a dedicated rmdir command for removing directories. However, it can only delete empty directories. This is why you have to use the rm command for deleting folders.
Let’s see more about removing directories in the command line in Ubuntu Linux.
Delete folders using rm command
On a fundamental level, directories are just special files that have information about the location of files stored in the memory.
This means you can almost use the rm command to remove them, just like regular files. Almost because there is a slight twist here.
If you try to delete directories the same way as files, you may encounter the «rm: cannot remove XYZ is a directory»:
$ rm no_strings_on_me rm: cannot remove 'no_strings_on_me': Is a directory
This is just a failsafe so that you don’t accidentally delete a folder with multiple files inside it.
To purposely delete a folder with the rm command, you have to use the recursive option -r :
This way, you tell the Linux system that you know that you are deleting a directory recursively with all its content. The contents are deleted first and the directory is removed.
Force remove a directory and its contents
A directory may have numerous files and if some of those files are write-protected, you’ll be prompted to confirm the deletion.
$ rm -r new_dir rm: remove write-protected regular empty file 'new_dir/b.txt'?
It’s okay for a couple of files but if it keeps on asking this question for too many files, it will be tiring.
This is when the -f option comes to the rescue so that you can force delete the files. You won’t get promoted anymore.
rm -rf directory_name_or_path
If you use rm -rf dir_name/* it will delete the contents of the dir_name folder but the dir_name will still exist. Why? Because you are explicitely telling the system to delete the contents of directory, not the directory.
Removing empty directories with rmdir command
If you accidentally delete the wrong file, only one file is deleted. If you accidentally delete one directory, multiple files, and even nested directories will be deleted. Which would be a terrible loss and an irreversible one.
The rmdir command exists for this sole purpose. When you use this command, the directory will be removed only if it is found to be empty. If the directory contains any files or sub-directories, the command will stop with an error that the directory is not empty.
Let us first take a look at its syntax:
I have a non-empty directory and an empty directory, as shown below:
$ ls * my_empty_dir: non_empty_dir: string1 string2 string3 string4
Let us see how it looks when I try to remove both of these directories using the rmdir command.
$ rmdir -v my_empty_dir rmdir: removing directory, 'my_empty_dir' $ rmdir -v non_empty_dir rmdir: removing directory, 'non_empty_dir' rmdir: failed to remove 'non_empty_dir': Directory not empty
The rmdir command prevented me from removing ‘non_empty_dir’ because it is not empty.
If you want to stick to using the rm command, it has a «safe» delete option as well. The rm command, when paired with -d option will exit when the directory is not empty.
Since rmdir did not delete ‘non_empty_dir’, let’s see if rm -d deletes it.
$ rm -d non_empty_dir rm: cannot remove 'non_empty_dir': Directory not empty
Conclusion
To summarize what you just learned about deleting folders in Ubuntu command line:
Command | Description |
---|---|
rm -r dir | Delete the dir folder |
rm -rf dir | Force delete the dir folder |
rm -rf dir/* | Force delete the contents but not dir itself |
rmdir dir | Delete dir if it is empty |
The rm command is what I personally prefer, as it is used for the removal of files as well as directories, but rmdir command can be useful too, depending on the scenario.
Do let me know if you have any queries or confusion 🙂