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

2020年07月28日

本文参考引用自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


sicnature ---------------------------------------------------------------------
Your current IP address is: 54.175.120.161
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source https://www.myzhenai.com/post/3538.html

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注