關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • Linux: 操作包含特殊字元的檔案

    Posted on February 22nd, 2010 Sam Tang No comments

    在 Unix 或 Linux 下很多特殊字元也是指令的一部份,例如空格 (” “)、問號 (“?”) 及橫線 (“-”) 等。所以如果檔案名稱包含了一些特殊字元,在操作上便會出現錯誤,例如:

    # touch -foo.txt
    touch: invalid option — ‘o’
    Try `touch –help’ for more information.
    # touch –foo.txt
    touch: unrecognized option ‘–foo.txt’
    Try `touch –help’ for more information.

    上面輸入了兩行指令,分別用 touch 建立檔案 “-foo.txt” 及 “–foo.txt”,但同樣發生錯誤。要解決這個問題,只要在指令後面加上兩個橫線 “–” 即可,例如:

    # touch — –foo.txt
    # touch — -foo.txt

    而這個方法同樣可以在其他檔案操作指令使用,例如 cp, mv, rm, rmdir。

    以下是 cp 及 mv 的用法:

    cp – - ‘-foo.txt’ /path/to/dest
    mv – - ‘-foo.txt’ /path/to/dest


    Leave a reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word