I have some directories that have the UNIX executable permission some does not. What tests could I write in Mathematica to test it?

  • 1
    $\begingroup$ possible duplicate of How to trigger UNIX command-line command from Mathematica notebook? $\endgroup$
    – Öskå
    Jan 9, 2015 at 15:46
  • 1
    $\begingroup$ ReadList["!ls -la ~/somedie", String] lists the -rwx--x--x on all files. I assume that's what you mean by "UNIX executable permission"? $\endgroup$
    – Öskå
    Jan 9, 2015 at 15:47
  • 1
    $\begingroup$ That relies on seeing UNIX permission values directly. Any "indirect" way? I want to see if I can "execute" anything in the directory. But how to test this "execute" in Mathematica? $\endgroup$
    – user13253
    Jan 9, 2015 at 17:24
  • $\begingroup$ Do you mean that you want to execute a file and see if it's executable or not? $\endgroup$
    – Öskå
    Jan 9, 2015 at 17:28
  • 2
    $\begingroup$ the linux executable bit for directories does not affect executability of files within the directory. Nice explanation here: unix.stackexchange.com/questions/21251/… $\endgroup$
    – george2079
    Jan 9, 2015 at 18:08

1 Answer 1


I don't know if I understand well, but if you want to check, with Mathematica on Unix, if a given file/directory is executable, you'll have to run a specific external unix command line and retrieve the result.

Unix useful commands

The following concerns UNIX systems but there are probably some equivalent commands for Windows. (Feel free to edit the post if you know which ones).

There are several ways ... such as the approach given by @Oska in the comments above (-> analyse the file explicit permissions output by ls -la).

Here I will use the UNIX test command which in particular can be applied directly to a file to check many of its properties. Indeed, to check if a file is executable, just type in a terminal the command line :
test -x filename or [ -x filename ] ([the square bracket is a shorthand of testbut the you have to add ] at the end of the test.) The -x option is actually telling test to check if the file is indeed executable (many other options exists, just see the manual pages of test).

Run the external (unix) command with Mathematica :Run, RunProcess, ReadList and Import

There are also different ways to ask Mathematica to run an external command AND to get back the result (into Mathematica). I will show below how to run our unix test command with all the Mathematica functions given in this post.

execQ[filename_] := Run["test -x " <> filename] == 0


execQ[filename_] := RunProcess[{$SystemShell, "-c", "test -x " <> filename}, "ExitCode"] == 0


execQ[filename_] := ReadList["!test -x " <> filename <> " && echo 0 || echo 1"] == {0};


execQ[filename_] := Import["!test -x " <> filename <> " && echo 0 || echo 1", "Text"] == "0";

This indeed will test only if the filename corresponds to a file which is executable. It works for regular files and directories. I could have included here some additional unix code to test if the file is also a directory (test -d will do) but I leave it to Mathematica which knows how to do that also with FileType. (See the tests below).

You'll notice some additional unix command && echo 0 || echo 1 for ReadListand Import. This will print on a unix terminal 0 or 1, depending whether the test was successful or not. This is required here as those mathematica functions cannot retrieve (contrary to Runand RunProcess) the equivalent "exit codes" returned automatically by the unix.


Let's apply the function execQ to your problem :

1/ let's for example list automatically all the directories in the current working directory (Directory[]) :

mydirs = Select[FileNames[], FileType@# == Directory &]

2/ then, this should tell you which ones are executable :

Select[mydirs, execQ]

Explanation concerning the RunProcess code

As requested by the OP in the comments, here are some explanations concerning the RunProcess syntax inside execQ. "-c" and "-x" are actually options for two different UNIX commands used here.
What happens :

  • RunProcess sends an external command to the operating system command language interpreter $SystemShell (try to evaluate that variable in a cell in Mathematica).
  • The -c option (which is specific to the Unix shell), tells that the command sent to the interpreter is contained in a string which is actually the third argument in the list inside my RunProcess code.
  • The unix command sent in the string here is : test -x, which actually tests if the given filename is executable (-x). If it is, the command will return the exit code 0, or 1 if it is not.
  • This exit code is then directly retrieved by Mathematica thanks to the parameter "ExitCode" given in the RunProcess command.

For more info about the unix commands, you can just type in a unix console/terminal man sh and man test or just google it.

  • $\begingroup$ What is "-c" and "-x" for respectively? What's documentation for those? The answer is incomplete without any mentioning of such. $\endgroup$
    – user13253
    Jan 14, 2015 at 16:54
  • $\begingroup$ @qazwsx Please, see my edit. $\endgroup$
    – SquareOne
    Jan 14, 2015 at 23:32
  • $\begingroup$ just Run[" test -x dirname" ] works as well, for those with older versions.. $\endgroup$
    – george2079
    Jan 16, 2015 at 23:55
  • $\begingroup$ @george2079 yes ;) actually I have prepared yesterday a small edit to my answer to show all the alternative ways to do the same thing, including Run (as you exactly did), but also ReadList, and Import. I also planned to replace [ ]with test which is a little bit less "scary" ;) I'll post that very soon. $\endgroup$
    – SquareOne
    Jan 17, 2015 at 0:30
  • $\begingroup$ @george2079 Please see my edit with added alternative ways to run the same external test command. Run and others are now included. $\endgroup$
    – SquareOne
    Jan 17, 2015 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.