Linux 命令行删除指定扩展名文件 | 您所在的位置:网站首页 › linux删除指定名称的文件怎么删除 › Linux 命令行删除指定扩展名文件 |
导读
怎样根据文件扩展名来删除多个文件呢?今天我们来介绍几种方法。
我们大家都知道,在 Linux 中删除文件可以使用 rm 命令: rm [option] filename不过,使用上述命令删除文件的时候,需要知道确切的文件名。那么怎样根据文件扩展名来删除多个文件呢?今天我们来介绍几种方法。 方法1:使用 rm 命令按扩展名删除文件作为演示,假如我们要删除扩展名为 gif 的所有文件,可以这样做: rm *.gif不过一般情况下,直接删除是有风险的,在删除之前最好先检查一下: ls *.gif通常情况下,我在删除文件的时候会做如下操作: $ ls 1.gif 2.gif 3.gif 4.gif a.jpg b.png c.webp $ ls *.gif 1.gif 2.gif 3.gif 4.gif $ rm -v *.gif removed '1.gif' removed '2.gif' removed '3.gif' removed '4.gif' $ ls a.jpg b.png c.webp此外,还可以同时删除多个扩展名的文件,如下: $ ls f1.txt f2.txt f3.txt f4.txt not-txt-file.pdf random.txt $ rm -v *.txt *.pdf removed 'f1.txt' removed 'f2.txt' removed 'f3.txt' removed 'f4.txt' removed 'not-txt-file.pdf' $ ls random.txt在使用 rm 命令的时候,可以使用交互式的 -i 选项,该选项要求在删除文件之前进行确认。不过这个适用于删除单个或者几个文件,如果批量删除文件,这个选项就不方便了。 像上面的删除操作,它具体是怎样工作的呢?答案是使用的通配符。 简而言之,通配符是用于匹配特定模式的特殊字符。以下是一些常用的通配符: 通配符 用途 * 匹配一个或多个匹配项 ? 匹配单个 [] 指定匹配范围 ^ 从匹配中排除在上面的删除例子中,我们使用了 * 通配符,这表示它可以匹配任何字符。当你使用 *.gif 的时候,它实际表示的是与 .gif 组合的任何字符,也就是说,它提供了所有以 gif 为扩展名的文件。 * 和扩展名之间的点很重要有些人在使用通配符的时候,会这样写:*gif,这是不对的。* 和扩展名之间的点 . 至关重要。 看下面的例子,假如我们使用 *gif 来删除文件,看看会怎样。 $ ls 1.gif 2.gif 3.gif 4.gif definately-not-a-gif jpg-not-gif not-a-gif $ rm -v *gif removed '1.gif' removed '2.gif' removed '3.gif' removed '4.gif' removed 'definately-not-a-gif' removed 'jpg-not-gif' removed 'not-a-gif'可以看到,除了删除所有 gif 文件外,它还删除了文件名中带有字符串 gif 的文件,尽管它不是文件的扩展名。所以,删除带有通配符或正则表达式的文件时,应确保尽可能精确。 方法2:使用find命令递归删除具有特定扩展名的文件rm 命令仅删除当前目录中的文件。即使使用递归选项,它也不会从子目录中删除文件。 要递归删除具有特定扩展名的文件,可以组合find命令和rm命令。看下面的例子,在子目录中也有 .gif 文件: $ ls * file_0.gif file_z.txt not-a-gif not-a-txt dir1: file_1.gif file_a.txt not-a-gif not-a-txt dir2: file_2.gif file_b.txt not-a-gif not-a-txt dir3: file_3.gif file_c.txt not-a-gif not-a-txt dir4: file_4.gif file_d.txt not-a-gif not-a-txt要删除文件,可以使用find和exec命令,如下所示: find . -type f -name "*.gif" -exec以下是上述命令的输出: $ find . -type f -name "*.gif" -exec rm -v {} \; removed './dir1/file_1.gif' removed './dir3/file_3.gif' removed './dir2/file_2.gif' removed './file_0.gif' removed './dir4/file_4.gif下面我们拆开来说明下: find 后面的点 . 表示在当前目录中搜索; -name 选项指定文件的名称,我们可以在其中使用正则表达式; -exec 选项用于对 find 命令的结果执行 bash 命令; {} 大括号充当匹配文件结果的占位符,因此 rm-v {} 将删除find命令找到的文件; 最后,分号结束 shell 执行的命令(exec之后的命令),并使用反斜杠 \,以便正确转义分号。在下面的文章中可了解 find 命令的更多例子: 关于 find 命令的15个超级有用的例子 使用 find 命令处理多个扩展名文件上面显示的命令不包括查找具有多个扩展名的文件,如:rm *.gif *.txt 要实现这一点,可以使用 -o 参数,它表示逻辑或运算符,但需要用括号括起来,且必须使用反斜杠 \ 来转义括号。 $ ls * file_0.gif file_z.txt not-a-gif not-a-txt dir1: file_1.gif file_a.txt not-a-gif not-a-txt dir2: file_2.gif file_b.txt not-a-gif not-a-txt dir3: file_3.gif file_c.txt not-a-gif not-a-txt dir4: file_4.gif file_d.txt not-a-gif not-a-txt $ find . \( -name "*.gif" -o -name "*.txt" \) -exec rm -v {} \; removed './dir1/file_1.gif' removed './dir1/file_a.txt' removed './dir3/file_3.gif' removed './dir3/file_c.txt' removed './dir2/file_2.gif' removed './dir2/file_b.txt' removed './file_0.gif' removed './file_z.txt' removed './dir4/file_d.txt' removed './dir4/file_4.gif'在这里,我们可以看到所有扩展名为 txt 的文件和扩展名为 gif 的文件都被递归删除。 大家可能会觉得对每个文件扩展名类型单独使用 find 命令会更容易,实际上也是这样的... 原文来自:https://www.51cto.com/article/721810.html 本文地址:https://www.linuxprobe.com/linux-delete-extension-file.html编辑:KSJXAXOAS,审核员:逄增宝 Linux命令大全:https://www.linuxcool.com/ Linux系统大全:https://www.linuxdown.com/ 红帽认证RHCE考试心得:https://www.rhce.net/ 为您推荐一些与本文相关的文章:微软开源的安全工具 Application Inspectorspringboot核心原理Ubuntu 14.04 结束支持该如何应对?阿里云份额创历史新高:超谷歌ThinkPHP升级至 5.1.18 版本可广泛编程DNA计算机证实化学编程指日可待详解:nginx手机用户访问自适应页面GNOME 2017 亚洲峰会确定将在中国重庆举办捷讯:刘刚刚9月7日北京顺利通过RHCE认证。《正则指引》pdf电子书免费下载 相关文章[点此投稿] U盘安装Linux安装报错及解决方案 2022-10-01 0 个评论 安装配置JDK和Eclipse的步骤 2019-03-04 0 个评论 linux终端彩色字体输出教程 2020-06-29 0 个评论 Nginx负载均衡高可用 2019-08-24 0 个评论 教你玩转Git-提取远程仓库 2019-09-18 0 个评论转载必需保留本文链接: https://www.linuxprobe.com/linux-delete-extension-file.html 红帽RHCE认证:https://www.rhce.net Linux命令大全:https://www.linuxcool.com Linux系统大全:https://www.linuxdown.com 本文依据CC-BY-NC-SA 3.0协议发布,竭诚为读者提供Linux视频教程、Linux学习资料以及红帽考试资料等优质学习资源。 |
CopyRight 2018-2019 实验室设备网 版权所有 |