15

我有一个 .txt 文件(比如 list.txt)中的文件列表。我想删除该列表中的文件。我以前没有写过脚本。有些人可以提供我可以使用的shell脚本/命令。我有 bash 外壳。

4

7 回答 7

45
while read -r filename; do
  rm "$filename"
done <list.txt

是缓慢的。

rm $(<list.txt)

如果参数太多,将失败。

我认为它应该工作:

xargs -a list.txt -d'\n' rm
于 2012-04-14T03:08:13.177 回答
10

试试这个命令:

rm -f $(<file)
于 2012-04-13T22:45:49.423 回答
5

如果文件名中有空格,则其他答案都不起作用;他们会将每个单词视为单独的文件名。假设文件列表在 中list.txt,这将始终有效:

while read name; do
  rm "$name"
done < list.txt
于 2012-04-13T23:10:29.380 回答
2

为了在无法使用xargs自定义分隔符的macOS 上快速执行:d

<list.txt tr "\n" "\0" | xargs -0 rm
于 2018-03-17T10:34:49.273 回答
1

以下应该可以工作,并在您循环时为您留出做其他事情的空间。

编辑:不要这样做,见这里: http: //porkmail.org/era/unix/award.html

对于 $(cat list.txt) 中的文件;做rm $文件;完毕

于 2012-04-13T22:44:38.233 回答
1

我今天只是在寻找解决方案,最终使用了一些答案和我拥有的一些实用程序功能的修改后的解决方案。

// This is in my .bash_profile

# Find
ffe () { /usr/bin/find . -name '*'"$@" ; } # ffe: Find file whose name ends with a given string

# Delete Gradle Logs
function delete_gradle_logs() {
   (cd ~/.gradle; ffe .out.log | xargs -I@ rm@)
}

于 2021-02-11T13:57:26.967 回答
0

在linux上,您可以尝试:

printf "%s\n" $(<list.txt) | xargs -I@ rm @

就我而言,我的 .txt 文件包含此类项目的列表*.ext并且运行良好。

于 2019-08-18T18:01:26.933 回答