Linux provides different commands in order to remove or delete directories and files. But in some cases, this may not work as expected and we can get a message like rmdir: 'dir' Directory no empty
which simply means when we try to delete a directory with rmdir
command it is not completed. In this tutorial, we will learn how to remove and delete an empty or full directory.
Linux提供了不同的命令以删除或删除目录和文件。 但是在某些情况下,这可能无法按预期工作,并且我们会收到类似rmdir: 'dir' Directory no empty
的消息rmdir: 'dir' Directory no empty
,这仅意味着当我们尝试使用rmdir
命令删除目录时,该目录尚未完成。 在本教程中,我们将学习如何删除和删除空目录或完整目录。
用rm -Rf命令删除 (Remove with rm -Rf Command)
The most used command to delete a directory with its content is rm
command. But rm
command is not enough without options -Rf
. This will force the deletion of given directories in a recursive manner. In this example, we will delete the directory named backup
删除目录及其内容最常用的命令是rm
命令。 但是,如果没有选项-Rf
rm
命令是不够的。 这将强制以递归方式删除给定目录。 在此示例中,我们将删除名为backup
的目录
$ rm -Rf backup
使用详细信息删除 (Remove with Verbose Information)
Deleting directory with its children will not provide verbose information by default. But we can print detailed information about the operation with the verbose option -v
like below.
默认情况下,删除带有子目录的目录不会提供详细信息。 但是,我们可以使用如下详细选项-v
打印有关该操作的详细信息。
$ rm -Rf -v backup
Remove with Verbose Information使用详细信息删除
删除/删除目录之前进行确认(Confirm Before Removin/Deleting Directory)
If we will delete directories those contain some sensitive ones we may need to confirm before deleting them. We can use -i
option which will ask for a removal for confirmation. In this example, we will delete the directory named poftut1
which is not empty. For each directory and files inside poftut1 the remove directory FILE or DIRECTORY NAME will be asked to use. In order to approve, we can enter y
or yes
to deny we can use empty answer which is just enter or n
or no
.
如果我们要删除包含敏感目录的目录,则在删除目录之前可能需要确认。 我们可以使用-i
选项,该选项将要求删除以进行确认。 在此示例中,我们将删除名为poftut1
的目录,该目录不为空。 对于poftut1中的每个目录和文件,将要求使用删除目录FILE或DIRECTORY NAME。 为了批准,我们可以输入y
或yes
来拒绝我们可以使用仅是enter或n
或no
空答案。
$ rm -r -i poftut1
Confirm Before Deleting Directory删除目录前先确认
一次删除/删除多个目录(Remove/Delete Multiple Directories At Once)
We may want to remove multiple directories with a single rm
command. We do not have to run rm
command multiple times for different directory names. We can provide all directories we want to remove in a single rm
command. We can also provide the path of the directory like /mnt/test
etc.
我们可能想用一个rm
命令删除多个目录。 对于不同的目录名,我们不必多次运行rm
命令。 我们可以在一个rm
命令中提供要删除的所有目录。 我们还可以提供目录的路径,例如/mnt/test
等。
$ rm -rf -v Templates poftut1/Case poftut1/Database /home/ismail/SocialFish/
Remove Multiple Directories At Once一次删除多个目录
根特权删除/删除(Remove/Delete with Root Privileges)
In order to delete a file or directory, we need privileges. If the directory is owned by the root user and we are not we can not delete the directory. So we need to get root privileges with sudo
command like below.
为了删除文件或目录,我们需要特权。 如果目录由root用户拥有,而我们不是,则无法删除目录。 因此,我们需要使用sudo
命令获得root特权,如下所示。
$ sudo rm -Rf backup
用Python删除 (Remove with Python)
We can also use some python script or commands in order to delete a directory please look following example where we will use os
module remove()
function.
我们也可以使用一些python脚本或命令来删除目录,请看下面的示例,我们将使用os
模块remove()
函数。
LEARN MORE How To Remove Files Older Than 1 Day/1 Week/1 Month Recursively
了解更多信息如何以递归方式删除早于1天/ 1周/ 1个月的文件
How To Delete and Remove File and Directory with Python?
import os os.remove("/home/ismail/trash")
使用find命令删除/删除目录 (Remove/Delete Directory with find Command)
find
command is a command used to search and find different types of files and directories the specified path. Find command provides different useful features like regular expression search, extension search or file attribute search, etc. We can also use the find command in order to delete or remove specified directories. We will use the -exec
feature of the find command which will run given command for the matched files and folders. We will match the directory type with the -type d
option and provide the name or name pattern with the -name '*cache'
.
find
命令是用于搜索和查找指定路径的不同类型文件和目录的命令。 Find命令提供了各种有用的功能,例如正则表达式搜索,扩展名搜索或文件属性搜索等。我们还可以使用find命令来删除或删除指定的目录。 我们将使用find命令的-exec
功能,该功能将为匹配的文件和文件夹运行给定命令。 我们将使用-type d
选项匹配目录类型,并使用-name '*cache'
提供名称或名称模式。
$ find /var -type d -name '*cache*' -exec rm -r {} +
We can also use the -delete
option of the find command which will automatically delete matched files and folders. In the following example we will directly delete the
我们还可以使用find命令的-delete
选项,它将自动删除匹配的文件和文件夹。 在以下示例中,我们将直接删除
$ find /home/ismail -type d -delete
使用文件管理器删除/删除目录 (Remove/Delete Directory with File Manager )
Linux and Linux Desktop Enviroments provides different file managers which is used for different operations like add, remove, change files and directories. We can also use file manager in order to remove or delete directory. We will just right-click t the folder or directory we want to delete and find the item like Move to Trash
, Delete
, Remove
, Send to Trash
etc. and click.
Linux和Linux桌面环境提供了不同的文件管理器,用于不同的操作,例如添加,删除,更改文件和目录。 我们还可以使用文件管理器来删除或删除目录。 我们将在要删除的文件夹或目录上单击鼠标右键,然后找到“ Move to Trash
,“ Delete
,“ Remove
,“ Send to Trash
等项目,然后单击。