How to check if the file is a binary file and read all the files which are not?

后端 未结 13 893
走了就别回头了
走了就别回头了 2020-12-05 16:58

How can I know if a file is a binary file?

For example, compiled c file.

I want to read all files from some directory, but I want ignore binary files.

13条回答
  •  [愿得一人]
    2020-12-05 17:26

    It's kind of brute force to exclude binary files with tr -d "[[:print:]\n\t]" < file | wc -c, but it is no heuristic guesswork either.

    find . -type f -maxdepth 1 -exec /bin/sh -c '
       for file in "$@"; do
          if [ $(LC_ALL=C LANG=C tr -d "[[:print:]\n\t]" < "$file" | wc -c) -gt 0 ]; then
             echo "${file} is no ASCII text file (UNIX)"
          else
             echo "${file} is ASCII text file (UNIX)"
          fi
       done
    ' _ '{}' +
    

    The following brute-force approach using grep -a -m 1 $'[^[:print:]\t]' file seems quite a bit faster, though.

    find . -type f -maxdepth 1 -exec /bin/sh -c '
       tab="$(printf "\t")"
       for file in "$@"; do
          if LC_ALL=C LANG=C grep -a -m 1 "[^[:print:]${tab}]" "$file" 1>/dev/null 2>&1; then
             echo "${file} is no ASCII text file (UNIX)"
          else
             echo "${file} is ASCII text file (UNIX)"
          fi
       done
    ' _ '{}' + 
    

提交回复
热议问题