Home > Article > Operation and Maintenance > How to delete a non-empty directory in Linux
In Linux, you can execute the "rm -r directory name" statement to delete a non-empty directory. The rm command is used to permanently delete a specified file or directory in the file system. When this command is followed by the "-r" option, recursive deletion can be achieved, deleting the specified directory and all its contents, including all subdirectories and files.
#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
Common shell commands to delete directories are: rm
rm
is a command line utility for deleting files and directories. Unlike the rmdir command, rm can delete both empty and non-empty directories. (The knowledge points about what the rmdir command is are added at the bottom!)
By default, without any options When used, rm will not delete the directory
as shown in the figure below:
First, let’s check how many directories we have:
There are two=>They are: First_linux_progarm and test1
##Try to use rm to delete test1How to delete empty directories under Linux system?
##=>If you want to delete empty directories, please use -d(– dir) option. That is:
rm -d 目录名
How to delete a non-empty directory under the linux system?
=>If you want to delete a non-empty directory and all its contents, use the -r (–recursive or -R) option. That is:
rm -r 目录名
: Recursive deletion, mainly used to delete directories, can delete specified directories and Everything included, including all subdirectories and files.
#First use ls
to check how many directories there are=>There are two First_linux_progarm and test1. #Use rmdir again to try to delete the First_linux_progarm directory => It shows that First_linux_progarm is not empty and cannot be deleted (
I will explain what the rmdir command is later). #At this point, we enable
rm -r First_linux_progarm. # Then use ls
to view the directory, only test1 is left.
Additional knowledge points: rmdir command is also deleted Directory shell command!
It’s just that
rmdir is a command line utility for deletingempty directories. Directories can only be deleted if the directory is empty. For example, to delete the directory named linuxmi, type:
rmdir linuxmi
If the directory
is not empty, an error will be reported : rmdir: 删除 'linuxmi' 失败: 目录非空
Related recommendations: "
The above is the detailed content of How to delete a non-empty directory in Linux. For more information, please follow other related articles on the PHP Chinese website!