删除在文本文件中列出的文件

dre*_*ard 6 linux files remove batch-processing

我有一个文件,它导出了一堆需要删除的文件名。我需要知道如何删除每个文件,而不必在命令行中一次发出一个。

我曾想过将它放在一个for循环中,这可能会奏效,但想知道是否有更简单甚至更好的解决方案来做到这一点。

谢谢。

Den*_*son 13

不需要cat或循环:

xargs -d '\n' -a file.list rm
Run Code Online (Sandbox Code Playgroud)


Ign*_*ams 5

while read filename ; do rm "$filename" ; done < files.lst
Run Code Online (Sandbox Code Playgroud)


小智 5

rm -rf `cat /path/to/filename`
Run Code Online (Sandbox Code Playgroud)

`` 字符可以用 $() 替换

从 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.
Run Code Online (Sandbox Code Playgroud)