在 Linux 上查找并删除文件和目录在 Linux 上查找并删除文件和目录在 Linux 上查找并删除文件和目录在 Linux 上查找并删除文件和目录
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

在 Linux 上查找并删除文件和目录

在本文中,我们将了解 Linux 中的 find 命令以及如何使用 find 命令在 Linux 中删除文件和目录。

查找命令

Linux中的find命令是一个功能强大的命令行实用工具,可以帮助您根据用户指定的匹配模式搜索、查找或过滤文件和目录,并允许您对文件和目录执行后续操作得到的结果。操作可以是打印找到的文件,或者删除、读取内容等。

文件搜索将从当前位置开始,并递归地继续到层次结构中存在的所有目录、子目录。用户可以通过提供所需的目录或子目录作为匹配模式,将搜索限制在当前目录的某个级别。

find 命令允许您按文件、目录、名称、文件或目录的创建日期、修改日期、所有者和权限进行搜索。

句法

以下是 find 命令的语法 -

$ find [path] [options] [expression]

参数

所有参数都是可选的,默认情况下 find 命令将获取当前工作目录并列出层次结构中存在的所有文件。

  • path - 这里的path是您要搜索文件的位置。

  • options - options 参数只不过是 find 命令搜索文件的模式。例如,它可以是文件或文件夹的名称、权限、创建或修改时间或日期。

  • 表达式 - 您想要对结果执行的操作。例如 -delete、-print 等。该表达式可以以不同的方式使用,并且将在下面我们尝试的示例中详细了解。

例子

在此示例中,为了执行删除操作,我们将使用 -delete 表达式。假设您想从当前工作目录中删除一个 .txt 文件,命令如下:

find  .  -name filepattern  -delete

在上面的示例中,(.) 指的是当前工作目录。

  • -name 选项用于获取文件模式,例如在我们的例子中,它可以是文件的名称,例如 test.txt,也可以是文件扩展名(“.txt”)。如果给出文件名,它将仅删除具有该名称的文件,如果给出文件扩展名,它将删除该工作目录中具有 .txt 的所有文件。

  • -delete 表达式将执行文件删除。

让我们在命令行中尝试同样的操作。

$ ls
demo/  img.jpg  img2.jpg  img3.jpg  img4.jpg  test.txt  xyz.txt

在当前目录中,我有 demo/ 子目录和文件。让我们尝试从中删除 test.txt。

localhost:~/testFindCommand# find . -name test.txt -delete

上面的命令将删除该文件,并且不会给您任何输出。但是如果您现在检查该文件夹,test.txt 将消失,如下所示。

localhost:~/testFindCommand# ls
demo      img.jpg   img2.jpg  img3.jpg  img4.jpg  xyz.txt

使用相同的命令尝试删除所有扩展名为 .jpg 的文件。如果您在文件夹 demo/ 中看到,您将获得以下文件:

localhost:~/testFindCommand/demo# ls
abc.txt   img5.jpg

因此,当您尝试删除扩展名为 .jpg 的文件时,当前目录中的所有文件以及带有 .jpg 的子目录 demo 都将被删除。让我们运行命令并检查结果。

localhost:~/testFindCommand# find . -name '*.jpg' -delete

现在,如果您检查该文件夹,您将得到以下输出 -

localhost:~/testFindCommand# ls
demo     xyz.txt

在演示/中您将获得以下详细信息

localhost:~/testFindCommand# ls
demo     xyz.txt
localhost:~/testFindCommand# cd demo
localhost:~/testFindCommand/demo# ls
abc.txt

因此,现在从您当前的文件夹和子文件夹中将不会有任何 .jpg 文件。

例子

在本例中,我们将删除一个目录。因此,让我们知道可以使用哪些模式和选项来删除目录。

仅当目录为空时,-delete 选项才允许您删除给定目录。如果该目录存在任何文件,则会抛出错误。让我们看一个例子。因此,在我们当前的工作目录中,我们有以下文件和子目录。

localhost:~/testFindCommand# ls
demo      testDemo  xyz.txt

我们有一个 demo 和 testDemo 子目录,让我们检查一下它们的内容:

localhost:~/testFindCommand# cd demo
localhost:~/testFindCommand/demo# ls
abc.txt

demo文件夹不为空,并且有一个文件abc.txt。 testDemo文件夹为空,如下所示:

localhost:~/testFindCommand# cd testDemo
localhost:~/testFindCommand/testDemo# ls
localhost:~/testFindCommand/testDemo#

现在,让我们在 demo 和 testDemo 上使用带有 -delete 的 find 命令。

find . -name demo -delete
Or,
find . -type d -name demo -delete //The -type d in above command is referred for directories.

上面的命令不会删除 demo 文件夹并抛出错误,如下所示:

localhost:~/testFindCommand# find . -name demo -delete
find: ./demo: Directory not empty

现在,让我们尝试删除空目录testDemo。

localhost:~/testFindCommand# find . -name testDemo -delete
localhost:~/testFindCommand# ls
demo     xyz.txt

空目录被删除,没有任何问题。因此,请记住使用 -delete 选项来删除空目录。

例子

exec 选项允许您删除目录的所有内容。我们使用 -delete 选项遇到的问题将使用 -exec 选项进行排序。在详细介绍使用 -exec 命令之前,让我们先了解一下如何使用它。

我们将在 find 命令中使用以下表达式。

  • -exec rm -rf {} \; - 这允许您将 rm(删除命令)与 exec 一起使用。目录和子目录中存在的所有文件都将使用 -rf 选项递归删除它。找到的文件放置在大括号 {} 占位符中。

  • -exec rm -rf {} + - 该命令或多或少与上面的命令相似,唯一的区别是:它在末尾使用了+号。与 (;) 相比,在查找匹配文件时,使用 + 的性能据说更好。另外,当您使用 (;) 时,您需要使用 (\) 对其进行转义,而 + 则不需要对其进行转义。

  • -exec rm -rfi {} + - 此命令也类似,但它具有代表交互式的 -i 选项,并且在删除文件之前会请求您的许可。

让我们用示例来测试该命令:

-exec rm -rf {} \;

localhost:~/testFindCommand# find . -type d -name demo -exec rm -rf "{}" \;
find: ./demo: No such file or directory
localhost:~/testFindCommand# ls
xyz.txt

-exec rm -rf {} +

localhost:~/testFindCommand# find . -type d -name test -exec rm -rf "{}" +
localhost:~/testFindCommand#

-exec rm -rfi {} +

localhost:~/testFindCommand# find . -type d -name demo -exec rm -rfi "{}" +
rm: descend into directory './demo'? y
rm: descend into directory './demo/test'? y
rm: descend into directory './demo/test/abc'? y
rm: descend into directory './demo/test/abc/tt'? y
rm: remove directory './demo/test/abc/tt'? y
rm: remove directory './demo/test/abc'? y
rm: remove directory './demo/test'? y
rm: remove directory './demo'? y
localhost:~/testFindCommand#  

例子

您可以使用 xargs 命令和 find 命令得到的结果来删除目录或文件。删除所有扩展名为“*.txt”的文件的命令

find . -name “*.txt” | xargs rm -rf
localhost:~/testFindCommand# ls
demo     xyz.txt
localhost:~/testFindCommand# find . -type f -name "*.txt"|xargs rm -rf
localhost:~/testFindCommand# ls
demo

使用 xargs 删除目录的命令。

localhost:~/testFindCommand# ls
demo     xyz.txt
localhost:~/testFindCommand# find . -name demo | xargs rm -rf
localhost:~/testFindCommand# ls
xyz.txt
©2015-2025 艾丽卡 support@alaica.com