Linux

刪除文本文件中列出的文件

  • April 10, 2018

我有一個文件導出了一堆需要刪除的文件名。我需要知道如何刪除每個文件,而不必在命令行中一次發出一個。

我考慮過將它放在一個for循環中,這可能會起作用,但想知道是否有更簡單甚至更好的解決方案來做到這一點。

謝謝。

rm -rf `cat /path/to/filename`

`` 字元可以替換為 $()

來自 bash 手冊頁:

  Command Substitution
      Command substitution allows the output of a command to replace the command
      name.  There are two forms:

             $(command)
      or
             `command`

      Bash performs the expansion by executing command and replacing the command
      substitution  with  the  standard output of the command, with any trailing
      newlines deleted.  Embedded newlines are not  deleted,  but  they  may  be
      removed  during  word splitting.  The command substitution $(cat file) can
      be replaced by the equivalent but faster $(< file).

      When the old-style backquote  form  of  substitution  is  used,  backslash
      retains its literal meaning except when followed by $, `, or \.  The first
      backquote not preceded by a backslash terminates the command substitution.
      When  using  the  $(command)  form, all characters between the parentheses
      make up the command; none are treated specially.

      Command substitutions may be nested.  To nest when  using  the  backquoted
      form, escape the inner backquotes with backslashes.

      If the substitution appears within double quotes, word splitting and path‐
      name expansion are not performed on the results.

引用自:https://serverfault.com/questions/175348