![删除文本文件中列出的文件](https://linux22.com/image/522943/%E5%88%A0%E9%99%A4%E6%96%87%E6%9C%AC%E6%96%87%E4%BB%B6%E4%B8%AD%E5%88%97%E5%87%BA%E7%9A%84%E6%96%87%E4%BB%B6.png)
我有一个文件,其中导出了一堆需要删除的文件名。我需要知道如何删除每个文件,而不必在命令行上逐个执行。
我曾考虑过将其放在for
循环内,这可能会起作用,但我想知道是否有更简单甚至更好的解决方案来做到这一点。
谢谢。
答案1
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.
答案2
不需要cat
或循环:
xargs -d '\n' -a file.list rm
答案3
$ cat file.list | xargs rm
答案4
perl -lne 'unlink' files_to_remove.txt
如果您需要删除大量文件,这比 xargs + rm 快几倍,比 shell 循环快很多倍。