Linux之刪除帶有空格的文件(而不是目錄)


大家平時工作中對不帶空格的文件接觸較多。這樣一來刪除操作也是比較簡單的。但是有時我們會接觸帶有空格的文件。對於這種文件我們應該如何刪除呢?

首先我們演示一下find命令結合xargs命令刪除不帶空格的文件

[root@ELK-chaofeng test]# touch 1.txt 2.txt
[root@ELK-chaofeng test]# ls
1.txt  2.txt
[root@ELK-chaofeng test]# find . -type f | xargs
./1.txt ./2.txt
[root@ELK-chaofeng test]# find . -type f | xargs rm -rf
[root@ELK-chaofeng test]# ls
[root@ELK-chaofeng test]#

接下來我們演示刪除帶有空格的文件

[root@ELK-chaofeng test]# touch 1.txt 2.txt '1 2.txt'
[root@ELK-chaofeng test]# ls
1 2.txt  1.txt  2.txt
[root@ELK-chaofeng test]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 14 12:24 1 2.txt
-rw-r--r-- 1 root root 0 Feb 14 12:24 1.txt
-rw-r--r-- 1 root root 0 Feb 14 12:24 2.txt
[root@ELK-chaofeng test]# find . -type f -print0 | xargs -0 rm -rf
[root@ELK-chaofeng test]# ls

上面的參數-print0,於默認的-print相比,輸出的序列不是以空格分隔,而是以null字符分隔。而xargs也有一個參數-0,可以接受以null而非空格間隔的輸入流。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM