更改 / 複製 / 刪除 "-" 開頭的檔案名稱

檔案名稱是 -(dash) 開頭, 要如何 複製 / 刪除 / 改名? (砍 減字號 開頭的檔案)

範例

  1. echo 'xxx' > "-X-02.txt"
  2. ls

    -X-02.txt

  3. mv *02* 02.txt # 改名字

     mv: 不適用的選項 -- X
     請嘗試執行‘mv --help’來獲取更多資訊。

  4. mv -- *02* 02.txt # 解法, 使用 --, 更名完成

更改 / 複製 / 刪除 減號開頭的檔名

測試
  • echo 'xxx' > "-xx" # 產生一個 減號開頭的檔案
解法
  • 使用 --./ 即可解決此問題.
更名
  • mv -- -xx oo
  • mv ./-xx oo
複製
  • cp ./-xx oo
  • cp -- -xx oo
刪除
  • rm ./-xx
  • rm -- -xx

相關說明

下述轉載自: http://cha.homeip.net/blog/archives/lccnotes.txt

如何建立/刪除減號 ( - ) 開頭的檔案, 目錄:
1) mkdir -- -p (建立 -p 資料夾)
2) touch ./-a (建立 -a 檔案)
3) rmdir ./-p (刪除 -p 資料夾)
4) rm -f -- -a (刪除 -a 檔案)
ps. 若資料夾中含有 - 開頭的檔案, 則無法以 -rf 刪除, 必須先以上述方式
手動刪除 - 開頭檔案後, 才能以 -rf 強制全數刪除其他資料

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

在〈更改 / 複製 / 刪除 "-" 開頭的檔案名稱〉中有 3 則留言

  1. 我也遇到這個問題過...
    man page 其實有寫到
    NOTES
    The rm command uses getopt(3) to parse its arguments, which allows it to
    accept the `--' option which will cause it to stop processing flag
    options at that point. This will allow the removal of file names that
    begin with a dash (`-'). For example:
    rm -- -filename
    The same behavior can be obtained by using an absolute or relative path
    reference. For example:
    rm /home/user/-filename
    rm ./-filename
    幫tsung 補完一下^^

Tsung 發表迴響取消回覆

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料