如何制作文件列表中不存在的文件列表

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

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

我想获得一个不存在的文件列表。

您可以使用:

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

xargs将为a.txt每行运行bash -c[[ ! -e $1 ]] [[ ! -e $1 ]]将检查每个条目是否不存在。

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

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

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

就像使用<将输入传递给循环一样,可以使用> out.txt将循环的输出写入文件。