在 Linux 上查找和删除文件和目录
在本文中,我们将了解 Linux 中的 **find** 命令,以及如何使用 **find** 命令在 Linux 中删除文件和目录。
find 命令
Linux 中的 **find** 命令是一个强大的命令行实用程序工具,它可以帮助您根据用户指定的匹配模式搜索、查找或过滤文件和目录,并允许您对获得的结果执行后续操作。这些操作可以是打印找到的文件、删除、读取内容等。
文件搜索将从当前位置开始,并递归地进入层次结构中的所有目录和子目录。用户可以通过提供所需的目录或子目录作为匹配模式来将搜索限制到当前目录的某个级别。
find 命令允许您按文件、目录、名称、文件或目录的创建日期、修改日期、所有者和权限进行搜索。
语法
以下是 find 命令的语法:
$ find [path] [options] [expression]
参数
所有参数都是可选的,默认情况下 find 命令将获取当前工作目录并列出层次结构中存在的所有文件。
**path** - 这里 path 是您要搜索文件的路径。
**options** - options 参数只是 find 命令搜索文件的模式。例如,它可以是文件或文件夹的名称、权限、创建或修改时间或日期。
**expression** - 您要对结果执行的操作。例如 -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 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
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP