linux刪除含特殊字元的檔案或檔案夾

來源:互聯網
上載者:User

標籤:特殊字元

   linux系統支援使用者建立包含連字號(-)的檔案或檔案夾,一個錯誤的操作有時可能會產生以下檔案或檔案夾:

   

[[email protected] aa]# touch /tmp/aa/{,-,\\}-{0,1,2,3,4,5}[[email protected] aa]# ls-0  --0  \-0  -1  --1  \-1  -2  --2  \-2  -3  --3  \-3  -4  --4  \-4  -5  --5  \-5[[email protected] aa]# lltotal 0-rw-r--r-- 1 root root 0 Apr  2 22:52 -0-rw-r--r-- 1 root root 0 Apr  2 22:52 --0-rw-r--r-- 1 root root 0 Apr  2 22:52 \-0-rw-r--r-- 1 root root 0 Apr  2 22:52 -1-rw-r--r-- 1 root root 0 Apr  2 22:52 --1-rw-r--r-- 1 root root 0 Apr  2 22:52 \-1-rw-r--r-- 1 root root 0 Apr  2 22:52 -2-rw-r--r-- 1 root root 0 Apr  2 22:52 --2-rw-r--r-- 1 root root 0 Apr  2 22:52 \-2-rw-r--r-- 1 root root 0 Apr  2 22:52 -3-rw-r--r-- 1 root root 0 Apr  2 22:52 --3-rw-r--r-- 1 root root 0 Apr  2 22:52 \-3-rw-r--r-- 1 root root 0 Apr  2 22:52 -4-rw-r--r-- 1 root root 0 Apr  2 22:52 --4-rw-r--r-- 1 root root 0 Apr  2 22:52 \-4-rw-r--r-- 1 root root 0 Apr  2 22:52 -5-rw-r--r-- 1 root root 0 Apr  2 22:52 --5-rw-r--r-- 1 root root 0 Apr  2 22:52 \-5

我們試著用普通的rm刪除時會提示:

[[email protected] aa]# rm -f --0rm: unrecognized option `--0‘Try `rm --help‘ for more information.

查看rm man手冊有如下描述:

To remove a file whose name starts with a ‘-’, for example ‘-foo’, use one of these commands:   rm -- -foo   rm ./-foo

使用這個辦法刪除帶\的檔案進應注意進行二次轉義,否則無法刪除。

[roo[email protected] aa]# rm -f -- \-5[[email protected] aa]# ls-0  --0  \-0  -1  --1  \-1  -2  --2  \-2  -3  --3  \-3  -4  --4  \-4  --5  \-5[[email protected] aa]# rm -f ./\-5[[email protected] aa]# ls-0  --0  \-0  -1  --1  \-1  -2  --2  \-2  -3  --3  \-3  -4  --4  \-4  --5  \-5

正確的做法如下:

[[email protected] aa]# rm -f -- \\-5[[email protected] aa]# ls-0  --0  \-0  -1  --1  \-1  -2  --2  \-2  -3  --3  \-3  -4  --4  \-4  --5[[email protected] aa]# rm -f ./\\-4[[email protected] aa]# ls-0  --0  \-0  -1  --1  \-1  -2  --2  \-2  -3  --3  \-3  -4  --4  --5

檔案夾的刪除與此相同。


還可以通過inode號進行刪除:

[[email protected] aa]# ls -li | grep -- -22752777 -rw-r--r-- 1 root root 0 Apr  2 22:52 -22752783 -rw-r--r-- 1 root root 0 Apr  2 22:52 --2[[email protected] aa]# find . -inum 2752783 -exec rm {} \;[[email protected] aa]# ls -li | grep -- -22752777 -rw-r--r-- 1 root root 0 Apr  2 22:52 -2


本文出自 “HUNT” 部落格,請務必保留此出處http://hunt1574.blog.51cto.com/1390776/1629585

linux刪除含特殊字元的檔案或檔案夾

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.