我一直在寻找一种从需要存在的文件列表中列出不存在的文件的方法。这些文件可以存在于多个位置。我现在拥有的:
#!/bin/bash
fileslist="$1"
while read fn
do
if [ ! -f `find . -type f -name $fn ` ];
then
echo $fn
fi
done < $fileslist
如果文件不存在,find命令将不会打印任何内容,并且测试不起作用。删除not并创建if then else条件不能解决问题。
如何打印文件名列表中找不到的文件名?
新脚本:
#!/bin/bash
fileslist="$1"
foundfiles="~/tmp/tmp`date +%Y%m%d%H%M%S`.txt"
touch $foundfiles
while read fn
do
`find . -type f -name $fn | sed 's:./.*/::' >> $foundfiles`
done < $fileslist
cat $fileslist $foundfiles | sort | uniq -u
rm $foundfiles
答案 0 :(得分:1)
尝试用[[ -z "$(find . -type f -name $fn)" ]] && echo $fn
替换身体。 (请注意,此代码必然会出现包含空格的文件名问题。)
更有效的基础:
diff <(sort $fileslist|uniq) <(find . -type f -printf %f\\n|sort|uniq)
我认为你可以处理差异输出。
答案 1 :(得分:1)
这是test.bash:
#!/bin/bash
fn=test.bash
exists=`find . -type f -name $fn`
if [ -n "$exists" ]
then
echo Found it
fi
将$ exists =设置为find的结果。 if -n检查结果是否为空。
答案 2 :(得分:1)
#!/bin/bash
fileslist="$1"
while read fn
do
FPATH=`find . -type f -name $fn`
if [ "$FPATH." = "." ]
then
echo $fn
fi
done < $fileslist
你很亲密!
答案 3 :(得分:1)
尝试一下:
find -type f -print0 | grep -Fzxvf - requiredfiles.txt
-print0
和-z
可以防范包含换行符的文件名。如果您的实用程序没有这些选项,并且您的文件名不包含换行符,那么您应该没问题。
答案 4 :(得分:0)
一次过滤一个文件的重复find
非常昂贵。如果您的文件列表与find
的输出直接兼容,请运行一个find
并从列表中删除所有匹配项:
find . -type f |
fgrep -vxf - "$1"
如果没有,也许您可以在find
之前按下管道中fgrep
的输出,使其与文件中的格式相匹配;或者,相反,按下文件中的数据为find
- 兼容。
答案 5 :(得分:0)
我使用这个脚本,它适用于我
#!/bin/bash
fileslist="$1"
found="Found:"
notfound="Not found:"
len=`cat $1 | wc -l`
n=0;
while read fn
do
# don't worry about this, i use it to display the file list progress
n=$((n + 1))
echo -en "\rLooking $(echo "scale=0; $n * 100 / $len" | bc)% "
if [ $(find / -name $fn | wc -l) -gt 0 ]
then
found=$(printf "$found\n\t$fn")
else
notfound=$(printf "$notfound\n\t$fn")
fi
done < $fileslist
printf "\n$found\n$notfound\n"
该行计算行数,如果大于0,则查找成功。这会搜索硬盘上的所有内容。你可以替换/。仅适用于当前目录。
$(find / -name $fn | wc -l) -gt 0
然后我只是运行它,文件列表中的文件被换行符分隔
./search.sh files.list