Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file is executable

Tags:

bash

tcsh

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.

like image 739
bob Avatar asked Apr 25 '12 16:04

bob


People also ask

How do you check if a file is an executable?

[ -w file ] tests if a file is writeable. [ -x file ] tests if a file is executable. The recommended tool to determine a file type is file .

How can I tell if a file is executable in Linux?

In Linux systems, we should look up the x bit of the file. out may be executed by its owner, joe, the members of the joe group, and others. So, the test's x flag proves if the file exists and is executable.

Is a file executable?

An executable file (EXE file) is a computer file that contains an encoded sequence of instructions that the system can execute directly when the user clicks the file icon. Executable files commonly have an EXE file extension, but there are hundreds of other executable file formats.

How do I know if a file is executable in Python?

To check if an existing file is executable, use os. access(path, mode), with the os. X_OK mode. Value to include in the mode parameter of access() to determine if path can be executed.


1 Answers

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.

like image 53
David W. Avatar answered Oct 24 '22 08:10

David W.