前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >find和xargs命令组合使用处理带空格文件名的文件

find和xargs命令组合使用处理带空格文件名的文件

作者头像
吴易娃
发布2024-01-12 18:00:59
1180
发布2024-01-12 18:00:59
举报
文章被收录于专栏:易娃的杂货铺易娃的杂货铺

当用find命令查找文件然后用xargs来批量处理文件时,当文件名中包含空格字符时,就会导致处理失败,因为xargs会认为空格前后分别是两个不同的文件。如下图:

https://cdn.jsdelivr.net/gh/alexwuyh/pic-host@master/photo/20220706110451.png
https://cdn.jsdelivr.net/gh/alexwuyh/pic-host@master/photo/20220706110451.png

我们查看find命令帮助文档可以发现,它有一个专门针对该情况并配合xargs命令的参数:-print0

代码语言:text
复制
-print0
              True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses).  This allows file names that con‐
              tain newlines or other types of white space to be correctly interpreted by programs that process the find output.  This option corresponds to the -0 option of xargs.

find默认的-print参数相比,它输出的序列不是以空格分隔,而是以null字符分隔。而xargs也有一个参数-0,可以接受以null非空格间隔的输入流。

所以,假如我们要找到当前目录下所有文件名以1).jpg结尾的文件并将它们全部删除掉时,就可以像下面这样操作:

代码语言:text
复制
find . -name "*1).jpg" -print0 | xargs -0 rm -f

https://cdn.jsdelivr.net/gh/alexwuyh/pic-host@master/photo/20220706110529.png
https://cdn.jsdelivr.net/gh/alexwuyh/pic-host@master/photo/20220706110529.png
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-07-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档