Linux中 print用法,linux之find中的-print0和xargs中-0用法

本文详细解释了在Linux命令行中,如何使用find和xargs命令配合删除文件,特别是当文件名包含空格时遇到的问题。通过使用-print0和-xargs -0选项,以NULL字符作为记录分隔符,解决了文件名被错误解析的问题。此外,还提供了其他find和xargs的实用示例,如删除指定后缀的旧文件,以及结合sed和grep进行文件内容操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

默認情況下, find 每輸出一個文件名, 后面都會接着輸出一個換行符 ('\n'), 因此我們看到的 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

[bash-4.1.5] ; find -name '*.log'

./file2.log

./file1.log

比如我想把所有的 .log 文件刪掉, 可以這樣配合 xargs 一起用:

[bash-4.1.5] ; find -name '*.log'

./file2.log

./file1.log

[bash-4.1.5] ; find -name '*.log' | xargs rm

[bash-4.1.5] ; find -name '*.log'

嗯, 不錯, find+xargs 真的很強大. 然而:

[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

[bash-4.1.5] ; find -name '*.log'

./file 1.log

./file 2.log

[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

原因其實很簡單, xargs 默認是以空白字符 (空格, TAB, 換行符) 來分割記錄的, 因此文件名 ./file 1.log 被解釋成了兩個記錄 ./file 和 1.log, 不幸的是 rm 找不到這兩個文件.

為了解決此類問題, 聰明的人想出了一個辦法, 讓 find 在打印出一個文件名之后接着輸出一個 NULL 字符 ('\0') 而不是換行符, 然后再告訴 xargs 也用 NULL 字符來作為記錄的分隔符. 這就是 find 的 -print0 和 xargs 的 -0 的來歷吧.

[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

[bash-4.1.5] ; find -name '*.log' -print0 | hd

0123456789ABCDEF|0123456789ABCDEF|

--------+--+--+--+--+---+--+--+--+---+--+--+--+---+--+--+--+--+----------------|

00000000: 2e 2f 66 696c 65 20 312e 6c 6f 6700 2e 2f 66|./file 1.log../f|

00000010: 69 6c 65 2032 2e 6c 6f67 00|ile 2.log.|

[bash-4.1.5] ; find -name '*.log' -print0 | xargs -0 rm

[bash-4.1.5] ; find -name '*.log'

你可能要問了, 為什么要選 '\0' 而不是其他字符做分隔符呢? 這個也容易理解: 一般的編程語言中都用 '\0' 來作為字符串的結束標志, 文件的路徑名中不可能包含 '\0' 字符.

其他我收集的find、xargs實例:

刪除以html結尾的10天前的文件,包括帶空格的文件:

find /usr/local/backups -name "*.html" -mtime +10 -print0 |xargs -0 rm -rfv

find /usr/local/backups -mtime +10 -name "*.html" -exec rm -rf {} \;

find -print 和 -print0的區別:

-print 在每一個輸出后會添加一個回車換行符,而-print0則不會。

當前目錄下文件從大到小排序(包括隱藏文件),文件名不為".":

find . -maxdepth 1 ! -name "." -print0 | xargs -0 du -b | sort -nr | head -10 | nl

nl:可以為輸出列加上編號,與cat -n相似,但空行不編號

以下功能同上,但不包括隱藏文件:

for file in *; do du -b "$file"; done|sort -nr|head -10|nl

xargs結合sed替換:

find . -name "*.txt" -print0 | xargs -0 sed -i 's/aaa/bbb/g'

xargs結合grep:

find . -name '*.txt' -type f -print0 |xargs -0 grep -n 'aaa'#“-n”輸出行號

轉載:http://blog.sina.com.cn/s/blog_5611597901019nye.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值