find命令中的print0和xargs -0
2016-01-18 20:25
686 查看
看到命令find . -name '*.h' -print0 | xargs -0 checkout-cache -f --
不明白其中-print0和 xargs -0的用法。查了一下,转载一篇备忘。
xargs命令的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题
以下内容转自http://blog.163.com/laser_meng@126/blog/static/16972784420117102638257/
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'),
因此我们看到的 find 的输出都是一行一行的:
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; ls -l
total 0
-rw-r--r-- 1 root root 0 2010-08-02 18:09 file1.log
-rw-r--r-- 1 root root 0 2010-08-02 18:09 file2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log'
./file2.log
./file1.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; bye
比如我想把所有的 .log 文件删掉, 可以这样配合 xargs 一起用:
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log'
./file2.log
./file1.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log' | xargs rm
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log'
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; bye
嗯, 不错, find+xargs 真的很强大. 然而:
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; ls -l
total 0
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 1.log
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log'
./file 1.log
./file 2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log' | xargs rm
rm: cannot remove `./file': No such file or directory
rm: cannot remove `1.log': No such file or directory
rm: cannot remove `./file': No such file or directory
rm: cannot remove `2.log': No such file or directory
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; bye
原因其实很简单, xargs 默认是以空白字符 (空格, TAB, 换行符) 来分割记录的, 因此文件名 ./file
1.log 被解释成了两个记录 ./file 和 1.log,
不幸的是 rm 找不到这两个文件.
为了解决此类问题, 聪明的人想出了一个办法, 让 find 在打印出一个文件名之后接着输出一个 NULL 字符 ('\0')
而不是换行符, 然后再告诉 xargs 也用 NULL 字符来作为记录的分隔符. 这就是 find 的 -print0 和
xargs 的 -0 的来历吧.
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; ls -l
total 0
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 1.log
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log' -print0 | hd
0 1 2 3 4 5 6 7 8 9 A B C D E F |0123456789ABCDEF|
--------+--+--+--+--+---+--+--+--+---+--+--+--+---+--+--+--+--+----------------|
00000000: 2e 2f 66 69 6c 65 20 31 2e 6c 6f 67 00 2e 2f 66 |./file 1.log../f|
00000010: 69 6c 65 20 32 2e 6c 6f 67 00 |ile 2.log. |
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log' -print0 | xargs -0 rm
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log'
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; bye
你可能要问了, 为什么要选 '\0' 而不是其他字符做分隔符呢?
这个也容易理解: 一般的编程语言中都用 '\0' 来作为字符串的结束标志,
文件的路径名中不可能包含 '\0' 字符.
不明白其中-print0和 xargs -0的用法。查了一下,转载一篇备忘。
xargs命令的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题
以下内容转自http://blog.163.com/laser_meng@126/blog/static/16972784420117102638257/
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'),
因此我们看到的 find 的输出都是一行一行的:
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; ls -l
total 0
-rw-r--r-- 1 root root 0 2010-08-02 18:09 file1.log
-rw-r--r-- 1 root root 0 2010-08-02 18:09 file2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log'
./file2.log
./file1.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; bye
比如我想把所有的 .log 文件删掉, 可以这样配合 xargs 一起用:
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log'
./file2.log
./file1.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log' | xargs rm
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log'
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; bye
嗯, 不错, find+xargs 真的很强大. 然而:
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; ls -l
total 0
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 1.log
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log'
./file 1.log
./file 2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log' | xargs rm
rm: cannot remove `./file': No such file or directory
rm: cannot remove `1.log': No such file or directory
rm: cannot remove `./file': No such file or directory
rm: cannot remove `2.log': No such file or directory
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; bye
原因其实很简单, xargs 默认是以空白字符 (空格, TAB, 换行符) 来分割记录的, 因此文件名 ./file
1.log 被解释成了两个记录 ./file 和 1.log,
不幸的是 rm 找不到这两个文件.
为了解决此类问题, 聪明的人想出了一个办法, 让 find 在打印出一个文件名之后接着输出一个 NULL 字符 ('\0')
而不是换行符, 然后再告诉 xargs 也用 NULL 字符来作为记录的分隔符. 这就是 find 的 -print0 和
xargs 的 -0 的来历吧.
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; ls -l
total 0
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 1.log
-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log' -print0 | hd
0 1 2 3 4 5 6 7 8 9 A B C D E F |0123456789ABCDEF|
--------+--+--+--+--+---+--+--+--+---+--+--+--+---+--+--+--+--+----------------|
00000000: 2e 2f 66 69 6c 65 20 31 2e 6c 6f 67 00 2e 2f 66 |./file 1.log../f|
00000010: 69 6c 65 20 32 2e 6c 6f 67 00 |ile 2.log. |
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log' -print0 | xargs -0 rm
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; find -name '*.log'
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.5] ; bye
你可能要问了, 为什么要选 '\0' 而不是其他字符做分隔符呢?
这个也容易理解: 一般的编程语言中都用 '\0' 来作为字符串的结束标志,
文件的路径名中不可能包含 '\0' 字符.
相关文章推荐
- Linux socket 初步
- Linux Kernel 4.0 RC5 发布!
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程
- Linux 下无损图片压缩小工具介绍