Check if the file is executable

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

+55
bash tcsh
Apr 25 '12 at 16:16
source share
4 answers

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

You will notice that -x FILE says that the FILE exists, and permission to execute (or search) 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, try file . You can parse the output to see what type of file it has. Word 'o Warning : Sometimes file returns 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 on the OS. However, the word executable will be in executable programs, and usually the architecture will also appear.

Compare the above with what I get in 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 the Solaris window:

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

In all three cases, you will see the word executable and architecture ( x86-64 , i386 or SPARC using 32-bit ).




Adding

Thank you very much, this seems like a way. Before marking this as your answer, can you tell me what kind of shell script check I need to perform (for example, what kind of parsing) on โ€‹โ€‹the โ€œfileโ€ to check if I can execute the program? If such a test is too difficult to do on a general basis, I would at least want to check whether it is linux or osX executable (Mach-O)

At the top of my head, you can 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 run the test, and then if it succeeds, I do grep on the output of the file command. grep -q means no print is output, but use the grep exit code to find out if I found a string. If your system does not accept grep -q , you can try grep "regex" > /dev/null 2>&1 .

Again, the output of the file command may differ from system to system, so you will need to make sure that they will work on your system. In addition, I check the executable bit. If the file is a binary executable, but the executable bit is not included, I will say that it is not executable. Perhaps this is not what you want.

+87
Apr 25 '12 at 16:27
source share

No one seems to notice that the -x operator is no different from a directory file.

To check the executable file accurately, you can use [[ -f SomeFile && -x SomeFile ]]

+9
Sep 14 '16 at 11:17
source share

It also seems that no one has noticed the -x operator on symbolic links. A symbolic link (chain) to a regular file (not classified as an executable file) does not pass the test.

+3
Dec 03 '16 at 6:04
source share

Testing files, directories, and symbolic links

The solutions given here do not work on directories or symbolic links (or both). On Linux, you can test files, directories, and symbolic links with:

 if [[ -f "$file" && -x $(realpath "$file") ]]; then .... fi 

On OS X, you should be able to install coreutils with homebrew and use grealpath .

isexec function isexec

You can define a function for convenience:

 isexec() { if [[ -f "$1" && -x $(realpath "$1") ]]; then true; else false; fi; } 

Or simply

 isexec() { [[ -f "$1" && -x $(realpath "$1") ]]; } 

Then you can check with:

 if 'isexec "$file"'; then ... fi 
+1
Apr 07 '18 at 13:20
source share



All Articles