bashtcsh

Check if a file is executable


I am wondering what's the easiest way to check if a program is executable with bash, without executing it ? It should at least check whether the file has execute rights, and is of the same architecture (for example, not a windows executable or another unsupported architecture, not 64 bits if the system is 32 bits, ...) as the current system.


Solution

  • Take a look at the various test operators (this is for the test command itself, but the built-in BASH and TCSH tests are more or less the same).

    You'll notice that -x FILE says FILE exists and execute (or search) permission is granted.

    BASH, Bourne, Ksh, Zsh Script

    if [[ -x "$file" ]]
    then
        echo "File '$file' is executable"
    else
        echo "File '$file' is not executable or found"
    fi
    

    TCSH or CSH Script:

    if ( -x "$file" ) then
        echo "File '$file' is executable"
    else
        echo "File '$file' is not executable or found"
    endif
    

    To determine the type of file it is, try the file command. You can parse the output to see exactly what type of file it is. Word 'o Warning: Sometimes file will return more than one line. Here's what happens on my Mac:

    $ file /bin/ls    
    /bin/ls: Mach-O universal binary with 2 architectures
    /bin/ls (for architecture x86_64):  Mach-O 64-bit executable x86_64
    /bin/ls (for architecture i386):    Mach-O executable i386
    

    The file command returns different output depending upon the OS. However, the word executable will be in executable programs, and usually the architecture will appear too.

    Compare the above to what I get on my Linux box:

    $ file /bin/ls
    /bin/ls: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), stripped
    

    And a Solaris box:

    $ file /bin/ls
    /bin/ls:        ELF 32-bit MSB executable SPARC Version 1, dynamically linked, stripped
    

    In all three, you'll see the word executable and the architecture (x86-64, i386, or SPARC with 32-bit).


    Addendum

    Thank you very much, that seems the way to go. Before I mark this as my answer, can you please guide me as to what kind of script shell check I would have to perform (ie, what kind of parsing) on 'file' in order to check whether I can execute a program ? If such a test is too difficult to make on a general basis, I would at least like to check whether it's a linux executable or osX (Mach-O)

    Off the top of my head, you could do something like this in BASH:

    if [ -x "$file" ] && file "$file" | grep -q "Mach-O"
    then
        echo "This is an executable Mac file"
    elif [ -x "$file" ] && file "$file" | grep -q "GNU/Linux"
    then
        echo "This is an executable Linux File"
    elif [ -x "$file" ] && file "$file" | grep q "shell script"
    then
        echo "This is an executable Shell Script"
    elif [ -x "$file" ]
    then
        echo "This file is merely marked executable, but what type is a mystery"
    else
        echo "This file isn't even marked as being executable"
    fi
    

    Basically, I'm running the test, then if that is successful, I do a grep on the output of the file command. The grep -q means don't print any output, but use the exit code of grep to see if I found the string. If your system doesn't take grep -q, you can try grep "regex" > /dev/null 2>&1.

    Again, the output of the file command may vary from system to system, so you'll have to verify that these will work on your system. Also, I'm checking the executable bit. If a file is a binary executable, but the executable bit isn't on, I'll say it's not executable. This may not be what you want.