[英]How to detect a file type on busybox
Busybox does not include the traditional linux "file" commad. Busybox 不包含传统的 linux“文件”逗号。 Is there any alternative way to detect whether a file is binary or ascii?有没有其他方法可以检测文件是二进制文件还是 ascii 文件? Thanks谢谢
You can try using grep.您可以尝试使用 grep。 This thread contains some variants of using grep command: https://stackoverflow.com/a/30133802/3694234 .此线程包含使用 grep 命令的一些变体: https://stackoverflow.com/a/30133802/3694234 。 For me, this command works fine.对我来说,这个命令工作正常。
$ grep -P "[^\x00-\x7F]" filename && echo Binary || echo Text
You can use this in a bash script您可以在 bash 脚本中使用它
#!/bin/bash
type=$(grep -P "[^\x00-\x7F]" $1 > /dev/null && echo Binary || echo Text)
echo "File type: ${type}"
Sample output样品 output
$ ./getFileType.sh test.c
File type: Text
$ ./getFileType.sh test
File type: Binary
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.