Are you a new user of Linux OR You are trying to delete a directory in Linux, and getting error “Directory is not empty”
Don’t worry anymore. This article will help learn “How to remove directory in Linux step by step by using a command-line interface and graphical interface both.
Content
- How to remove Directory in Linux
- How to remove Directory with its content
- How to remove multiple directories at the same time.
- How to remove directory by using GUI
- Video How to remove directory in Linux in the Hindi Language
- Conclusion.
How to remove Directory in Linux
Reading this article is showing your interest to learn new thing, After completing this section you will learn how to remove an empty directory in Linux by using commands.
rmdir command is used to remove an empty directory. Syntax is very simple as follow
$rmdir /dirctory_path
If the directory is not empty, rmdir command will not remove it. you will get the error “Directory is not empty”. In the next section, you will learn how to remove the directory with its content.
vijay@Ubuntu-19:~/Documents$rmdir no_content/ vijay@Ubuntu-19:~/Documents$rmdir old_data/ rmdir: failed to remove 'old_data/': Directory not empty vijay@Ubuntu-19:~/Documents$
rm is another command to use remove file and directory, but you must tell to rm you are removing a directory. Syntax is:
$rm -d /Directory_path
You don’t need to give the path if you are removing directory from the current directory.
See Examples:
vijay@Ubuntu-19:~/Documents$rm new_content/ rm: cannot remove 'new_content/': Is a directory vijay@Ubuntu-19:~/Documents$rm -d new_content/ vijay@Ubuntu-19:~/Documents$rm -d old_data/ rm: cannot remove 'old_data/': Directory not empty vijay@Ubuntu-19:~/Documents$
How to remove Directory with its content
By now you can easily remove an empty directory. I have directory name old_data is not empty. You have seen in the last section.
Use -r option with rm command.
$rm or /Directory_Path
vijay@Ubuntu-19:~/Documents$rm -r old_data/ vijay@Ubuntu-19:~/Documents$ls vijay@Ubuntu-19:~/Documents$
You should not go through mistakes
Using -r option is not enough to remove the directory with its content. I will remove everything forgiven directory without your confirmation. If you run this command by mistake you will repent only.
Use -ir option with rm command, it will better for you. If you are working as a system administrator and have much responsibility.
vijay@Ubuntu-19:~/Documents$rm -ir old_data/ rm: descend into directory 'old_data/'? y rm: remove regular empty file 'old_data/file5.doc'? y rm: remove regular empty file 'old_data/file1.doc'? y rm: remove regular empty file 'old_data/file4.doc'?
How to remove multiple directories at the same time
If you want to remove multiple directories at the same time then you can use rm command respectively followed by -r/-ir option as per your requirements.
The syntax is simple:
$rm -ir First_Directory Second_directory Third_Directory
vijay@Ubuntu-19:~/Documents$rm -ir dir1 dir2 rm: remove directory 'dir1'? y rm: remove directory 'dir2'? y vijay@Ubuntu-19:~/Documents$
If you want to remove multiple directories have a different locations. You should not worry anymore.
Go through the following example.
vijay@Ubuntu-19:~/Documents$rm -ir /home/vijay/Downloads/dir_2/ /home/vijay/Videos/dir_1/ rm: remove directory '/home/vijay/Downloads/dir_2/'? y rm: remove directory '/home/vijay/Videos/dir_1/'? y vijay@Ubuntu-19:~/Documents$
How to remove directory by using GUI
Most computer users love the graphical interface. It is easy to learn and easy to use.
Delete option is not available in Linux, Here is another option to delete directory called a move to trash.
Video: How to remove directory in Linux in Hindi Langugage
Conclusion
By now you are an expert to remove the directory and have good knowledge of using rm and rmdir commands.
The important thing, you should remove the directory safely by using the command line.
If you have any questions, please write in the comment box. For any specific discussion please Join our Group HERE