0

I know Linux has a built-in binary executable formats such as ELF, a.out, Shebang etc. I also know that the user is able to add his own supported formats to the system in '/proc/sys/fs/binfmt_misc'.

Now my goal is to cover all the possibilities here, so I could automatically (with a script) determine if a program is executable or not (and not just by the runable permission bit). I heard of the command file that can identify the file's format, and that it will print out the word 'executable' if it is an executable however - when I tested it with a jar file (which is listed on the binfmt_misc directory as an executable) it didn't print out the word 'executable'.

So I am asking for the best way to determine if a file is executable on a Linux system. If there is a way to run execve() just to get the return code (if the file isn't a supported executable - execve returns a matching error) then I could just intercept it and use it. The thing is that I need to do it without running the file on my system (or at least not let it change it in any way...).

Thanks!

user3016694
  • 307
  • 2
  • 4
  • 11
  • 2
    A `jar` file isn't executable, which is why `file` didn't say it was. It needs a java interpreter to execute it. Can you be more clear on what you mean by "executable"? You could, for example, have a Python script file which would execute via, `python myfile.py` but would fail to execute if you did, `sh myfile.py`. Is that considered executable? – lurker Apr 26 '14 at 15:53
  • 1
    broad question.. -x in linux is the answer. But what you would call executable for linux.. binary or a text file, which can be interpreted by an interpretr!! I guess both but even a file which doesnt hv executable permission can be executed as 'interpreter filename'.. since there are diff ways to execute you hv to consider all, and prepare a list of all!!! – PradyJord Apr 26 '14 at 16:11
  • I am not at all sure that your question really makes sense. What (and how) a file is executable is very system specific.... – Basile Starynkevitch Apr 26 '14 at 18:33
  • Possible duplicate of [Check if a file is executable](https://stackoverflow.com/q/10319652/608639) – jww Dec 06 '19 at 07:10

0 Answers0