Wednesday, January 13, 2010

How to check whether it is binary or ascii file in Linux

With file command you can determine the file type in unix. Thus with file command you can check whether a file is binary file or ascii file. Before going into much about file command let us check some simple example.
# file .bash_history
.bash_history: ASCII text
# file .dns
.dns: ASCII text, with no line terminators
# file .cvsignore
.cvsignore: empty
# file .ftpquota
.ftpquota: character Computer Graphics Metafile
# file nextlevelrealty.nextlevelre.com
nextlevelrealty.nextlevelre.com: ASCII text, with very long lines
# file memd*
memd: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped
# file pppd
pppd: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, stripped

Note that by invoking file command it is shown the file type.
File type can be come as,

1) Text: The file will be text file if they contain one of the words text (the file contains only printing characters and a few common control characters and is probably safe to read on an ASCII terminal)

2) Executable: After invoking file command if you get text executable then the file contains the result of compiling a program in a form understandable to some UNIX kernel or another, which means binary file.

3) Data: If after invoking file command there comes data keyword then it means data is in 'binary' or non-printable format.

So we see if after invoking file command there appears word "text" or "ascii" then it is text or ascii file otherwise it is binary.

Following shell script will tell you whether a file is binary file or text file. This script takes an argument and then prints its type.

file "$1" | grep executable
if [ $? -eq 0 ];then
echo "File is binary"
else
file "$1" |egrep "ascii|text"
if [ $? -eq 0 ];then
echo "File is ascii"
fi
fi

Related Documents
Dot (.) in linux and shell script
Semicolon (;) and double semicolon (;;) in shell script
Hash sign (#) in shell script
Single quote, double quote and comma in shell script
Forward slash and backslash in shell script and linux
http://arjudba.blogspot.com/2009/04/what-is-sharp-bang-appear-at-first.html
Colon (:) character in shell script and Linux
Exclamation mark in shell script and linux
Asterisk (*) in shell script and linux
Question mark (?) in linux and shell script
Dollar sign ($) in shell script
Paranthesis () in shell script
Brace {} in shell script
Control Characters in Linux and shell script
List of comparison made by test operator in shell script

No comments:

Post a Comment