How to tell binary from text files in linux

后端 未结 8 1033
[愿得一人]
[愿得一人] 2021-02-07 13:52

The linux file command does a very good job in recognising file types and gives very fine-grained results. The diff tool is able to tell binary files f

8条回答
  •  北荒
    北荒 (楼主)
    2021-02-07 14:36

    This approach uses same criteria as grep in determining whether a file is binary or text:

    is_text_file() { 
      grep -qI '.' "$1"
    }
    

    grep options used:

    • -q Quiet; Exit immediately with zero status if any match is found
    • -I Process a binary file as if it did not contain matching data

    grep pattern used:

    • '.' match any single character. All files (except an empty file) will match this pattern.

    Notes

    • An empty file is not considered a text file according to this test.
    • Symbolic links are followed.

提交回复
热议问题