I have some directories that have the UNIX executable permission some does not. What tests could I write in Mathematica to test it?
1 Answers
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 :
RunProcesssends an external command to the operating system command language interpreter$SystemShell(try to evaluate that variable in a cell in Mathematica).- The
-coption (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 myRunProcesscode. - The unix command sent in the string here is :
test -x, which actually tests if the givenfilenameis 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 theRunProcesscommand.
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. – user13253 Jan 14 '15 at 16:54
-
-
just
Run[" test -x dirname" ]works as well, for those with older versions.. – george2079 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 alsoReadList, andImport. I also planned to replace[ ]withtestwhich is a little bit less "scary" ;) I'll post that very soon. – SquareOne Jan 17 '15 at 00:30 -
@george2079 Please see my edit with added alternative ways to run the same external
testcommand.Runand others are now included. – SquareOne Jan 17 '15 at 02:36
ReadList["!ls -la ~/somedie", String]lists the-rwx--x--xon all files. I assume that's what you mean by "UNIX executable permission"? – Öskå Jan 09 '15 at 15:47FileNames, but any file access on any file ( eg.FileDate) should fail. ( sorry my linux box is down so I can't test ). Of course if you don't have read permission either than its kind of moot. – george2079 Jan 09 '15 at 18:19FileNames[__,dir]just returns an empty list if dir is not executable (even thoughlswill show the directory contents ), so you cant use this. – george2079 Jan 16 '15 at 23:51