如何从文件列表中创建不存在的文件列表

How to make a list of files that don't exist from a list of files

我有一个包含文件列表的文本文件 a.txt

photo/a.jpg
photo/b.jpg
photo/c.jpg
etc

我想获取不存在的文件列表。

您可以使用:

xargs -I % bash -c '[[ ! -e  ]] && echo ""' _ % < a.txt > b.txt

xargs 将为 a.txt 中的每一行 运行 bash -c[[ ! -e ]] 将检查每个条目的 non-presence。

不需要涉及 cat,也不需要为文件中的每一行调用单独的 shell;一个简单的 while read 循环就足够了:

while read -r file
do
    [ -e "$file" ] || echo "$file"
done < a.txt

逐行阅读。测试每个文件是否存在,如果不存在则打印其名称。

正如使用 < 将输入传递给循环一样,可以使用 > out.txt.

将循环的输出写入文件