
這是我的ls
命令,它顯示當前目錄中的文件列表:
rper: ls -l
$commandoutput[0]
file.txt
我嘗試$commandoutput[0]
使用 刪除行rm -rf $commandoutput[0]
,但它顯示以下錯誤。我怎麼刪除它?
rper: rm -rd $commandoutput[0]
commandoutput: Undefined variable.
答案1
除了@RomanPerekhrest 的答案,這也可以工作:
rm '$commandoutput[0]'
因為單引號將避免變數擴展。
另一種方法是開始打字rm $
,然後點擊Tab; shell 將自動完成檔案名,並根據需要轉義字元。
答案2
使用反斜線轉義$
(最好是[
, ]
)您的檔案名稱\
:
rm -rf \$commandoutput\[0\]
答案3
雖然其他人已經回答了這個問題,但另一種方法是尋找檔案的 inode 並使用 find 和 exec delete 來刪除檔案。
ls -il
範例輸出:
781956 drwx------ 3 viv viv 4096 2017-07-7 15:05 $commandoutput[0]
第一列應該是 inode,在上面的範例中,運行:
find . -inum 781956 -exec rm -i {} \;
答案4
另一種簡單的方法是使用通配符和 rm -i:
rm -i *command*
對於所有包含單字「command」的檔案名,這將詢問您是否要刪除該檔案。然後您可以在刪除之前進行檢查。