Home >Operation and Maintenance >Linux Operation and Maintenance >How to delete a non-empty directory in Linux

How to delete a non-empty directory in Linux

青灯夜游
青灯夜游Original
2022-03-24 18:40:3628929browse

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.

How to delete a non-empty directory in Linux

#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

How to delete a non-empty directory in Linux

##Try to use rm to delete test1

How to delete a non-empty directory in Linux


How 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 目录名

  • -r

    : Recursive deletion, mainly used to delete directories, can delete specified directories and Everything included, including all subdirectories and files.

  • As shown in the figure below:

#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.

How to delete a non-empty directory in Linux


Additional knowledge points: rmdir command is also deleted Directory shell command!

It’s just that

rmdir is a command line utility for deleting

empty 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: "

Linux Video Tutorial

"

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn