Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete a non-empty directory by linux

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to delete linux non-empty directory, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

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 the command is followed by the "- r" option, it can be deleted recursively, 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.

The common shell command to delete a directory is: rm

Rm is a command line utility for deleting files and directories. Unlike the rmdir command, rm can delete empty and non-empty directories. (the knowledge of what the rmdir command is added at the bottom!)

By default, rm does not delete directories when used without any option

As shown in the following figure:

First, take a look at how many directories we have:

There are two = >: First_linux_progarm and test1

Try to delete a test1 using rm

How to delete an empty directory under the linux system?

= > if you want to delete an empty directory, use the-d (- dir) option.

That is:

Rm-d directory name

How to delete a non-empty directory on a 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 directory name

-r: recursive deletion, mainly used to delete directories, you can delete the specified directory and all its contents, including all subdirectories and files.

As shown in the following figure:

# first use ls to check several directories = > there are two First_linux_progarm and test1.

# then use rmdir to try to delete the First_linux_progarm directory = > shows that First_linux_progarm is not empty and cannot be deleted (we will explain what the rmdir command is later).

# at this point, we enable rm-r First_linux_progarm.

# and then when you use ls to view the directory, only test1 is left.

Knowledge points to add:

The rmdir command is also a shell command to delete a directory!

It's just that rmdir is a command-line utility for deleting empty directories. You can delete a directory only if it 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: delete 'linuxmi' failed: directory is not empty Thank you for reading this article carefully. I hope the article "how to delete a non-empty directory in linux" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report