Linux下遍历有空格的文件夹或文件名的方法

本文参考引用自hostloc ID:嗷嗷 命令作者:志明

在Linux下用脚本或命令遍历目录下文件夹或文件名的时候,假如文件名里有空格的时候,我们把这些文件名放进用空格分割的数组里会变成几个文件名。

这个时候,我们只需要加一个print0就可以了。


[root@localhost ~]# find -name "*.txt"
./1 2.txt
./a b.txt
[root@localhost ~]# find -name "*.txt" -print0
./1 2.txt./a b.txt[root@localhost ~]#find -name "*.txt" -print0|xargs ls -l
xargs: WARNING: a NUL character occurred in the input.  It cannot be passed through in the argument list.  Did you mean to use the --null option?
ls: cannot access './1': No such file or directory
ls: cannot access '2.txt': No such file or directory
ls: cannot access 'b.txt': No such file or directory
[root@localhost ~]# find -name "*.txt" -print0|xargs -0 ls -l
-rw-r--r--. 1 root root 0 Jul 26 20:23 './1 2.txt'
-rw-r--r--. 1 root root 0 Jul 26 20:23 './a b.txt'
[root@localhost ~]#

 

相关链接:Linux Shell枚举目录下相同文件脚本 https://www.myzhenai.com.cn/post/2269.html

标签:
IP 地址 216.73.217.54
区域位置 美国加利福尼亚蒙诺维亚
系统信息 🇨🇳 系统 浏览器
最后修改:2020 年 07 月 28 日

赞赏支持

文章二维码