为何grep -I/--binary-files=without-match仍匹配特定二进制文件?
GNU grep二进制排除选项对特定压缩文件失效的问题
我有一个16位压缩格式的二进制文件:
# file /tmp/43174744S.0601.Z /tmp/43174744S.0601.Z: compress'd data 16 bits
grep可正常识别它为二进制文件:
# grep "" /tmp/43174744S.0601.Z Binary file /tmp/43174744S.0601.Z matches
但尝试用-I或--binary-files=without-match选项排除二进制文件时,该文件仍会被匹配:
# grep -I "" /tmp/43174744S.0601.Z Binary file /tmp/43174744S.0601.Z matches # grep --binary-files=without-match "" /tmp/43174744S.0601.Z Binary file /tmp/43174744S.0601.Z matches # grep -Il "" /tmp/43174744S.0601.Z /tmp/43174744S.0601.Z
这是否属于bug?因为这些选项对其他二进制文件(如.so库)有效:
# grep -I "" /usr/lib/*.so # grep "" /usr/lib/*.so Binary file /usr/lib/klibc-abS-oVB3xeRN8SFypUWbQvR33nc.so matches Binary file /usr/lib/libdmmp.so matches Binary file /usr/lib/libmpathcmd.so matches Binary file /usr/lib/libmpathpersist.so matches Binary file /usr/lib/libmultipath.so matches
测试涉及的grep版本为grep (GNU grep) 2.16和grep (GNU grep) 3.4。
更新1
提取该文件前2字节生成新文件后,问题依旧存在:
# head -c2 /tmp/43174744S.0601.Z > /tmp/43174744S.0601.C2.Z # cat /tmp/43174744S.0601.C2.Z | od -a 0000000 us gs 0000002 # grep -I "" /tmp/43174744S.0601.C2.Z Binary file /tmp/43174744S.0601.C2.Z matches
更新2
新建内容看似相同的文件后,grep表现正常:
# echo -n $'\37'$'\35' > /tmp/test # cat /tmp/test | od -a 0000000 us gs 0000002 # cat /tmp/43174744S.0601.C2.Z | od -a 0000000 us gs 0000002 # grep "" /tmp/43174744S.0601.C2.Z Binary file /tmp/43174744S.0601.C2.Z matches # grep "" /tmp/test #
但执行cp /tmp/43174744S.0601.C2.Z /tmp/test后,问题再次出现,说明问题与原文件本身相关。
更新3
提供两个文件的十六进制dump输出:
# hexdump -C /tmp/43174744S.0601.C2.Z 00000000 1f 9d |..| 00000002 # hexdump -C /tmp/test 00000000 1f 1d |..| 00000002
内容的提问来源于stack exchange,提问作者mgutt
相关产品推荐
相关产品推荐

