How do I use file
to differentiate between ELFves and scripts as quickly as possible?
I don't need any further details, just ELF, script (/plaintext), or other/error.
If it's just between ELF and script, you may not need file
at all. With bash
:
IFS= LC_ALL=C read -rn4 -d '' x < file case $x in ($'\x7fELF') echo ELF;; ("#!"*) echo script;; (*) echo other;; esac
(-d ''
(to use NUL character as delimiter) is to work around the fact that bash
's read
otherwise just ignores the NUL bytes in the input).
See also:
Create a custom magic(5) file that contains only the tests you need.
The stock elf
test can be fetched from https://github.com/file/file/blob/master/magic/Magdir/elf , and then cut down even further. text
is built-in, so you don't need to include it.
To avoid the parsing it each time, put it in its own directory and compile it to mgc
:
file -C -m dir
This will output dir.mgc
. (Specifying -m dir/
will instead output .mgc
.) Then it will be used by subsequent runs:
file -m dir
Turns out I can exclude the following tests to speed it up:
encoding tokens cdf compress elf tar
(Tests are excluded with -e $test_name
).
It looks like I need the ascii
and the soft
test.