Linux中删除资料

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

删除/删除目录之前进行确认(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中的每个目录和文件,将要求使用删除目录FILEDIRECTORY NAME 为了批准,我们可以输入yyes来拒绝我们可以使用仅是enternno空答案。

$ rm -r -i poftut1

一次删除/删除多个目录(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/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()函数。

使用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.

LinuxLinux桌面环境提供了不同的文件管理器,用于不同的操作,例如添加,删除,更改文件和目录。 我们还可以使用文件管理器来删除或删除目录。 我们将在要删除的文件夹或目录上单击鼠标右键,然后找到“ Move to Trash “ Delete “ Remove “ Send to Trash等项目,然后单击。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值