# How to determine if directories with executable permission and not

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

• possible duplicate of How to trigger UNIX command-line command from Mathematica notebook?
– Öskå
Jan 9 '15 at 15:46
• ReadList["!ls -la ~/somedie", String] lists the -rwx--x--x on all files. I assume that's what you mean by "UNIX executable permission"?
– Öskå
Jan 9 '15 at 15:47
• 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? Jan 9 '15 at 17:24
• Do you mean that you want to execute a file and see if it's executable or not?
– Öskå
Jan 9 '15 at 17:28
• the linux executable bit for directories does not affect executability of files within the directory. Nice explanation here: unix.stackexchange.com/questions/21251/… Jan 9 '15 at 18:08

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


or

execQ[filename_] := RunProcess[{$SystemShell, "-c", "test -x " <> filename}, "ExitCode"] == 0  or execQ[filename_] := ReadList["!test -x " <> filename <> " && echo 0 || echo 1"] == {0};  or 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. ### Test 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.

• What is "-c" and "-x" for respectively? What's documentation for those? The answer is incomplete without any mentioning of such. Jan 14 '15 at 16:54
• @qazwsx Please, see my edit. Jan 14 '15 at 23:32
• just Run[" test -x dirname" ] works as well, for those with older versions.. Jan 16 '15 at 23:55
• @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. Jan 17 '15 at 0:30
• @george2079 Please see my edit with added alternative ways to run the same external test command. Run and others are now included. Jan 17 '15 at 2:36