在 Linux 上查找和删除文件和目录
在本文中,我们将了解 Linux 中的 find 命令,以及如何使用 find 命令在 Linux 中删除文件和目录。
find 命令
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 的文件时,当前目录以及子目录 demo 中所有扩展名为 .jpg 的文件都将被删除。让我们运行该命令并检查结果。
localhost:~/testFindCommand# find . -name '*.jpg' -delete
现在检查文件夹,您将获得以下输出 -
localhost:~/testFindCommand# ls demo xyz.txt
在 demo/ 目录中,您将获得以下详细信息
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 或者, find . -type d -name demo -delete //上述命令中的 -type d 指的是目录。
上述命令不会删除 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
相关文章
有用资源
linux 参考教程 - 该教程包含有关 linux 的更多信息:https://www.cainiaomax.com/linux/

