Say I have the following output from
drwxr-xr-x 2 root root 4096 Apr 7 17:21 foo
How can I automatically convert this to the format used by
$ echo drwxr-xr-x | chmod-format 755
I'm using OS X 10.8.3.
Some systems have commands to display the permissions of a file as a number, but unfortunately, nothing portable.
If you want the permissions expressed in octal, you need:
BSDs (including Apple OS/X) have a
GNU find (from as far back as 1990 and probably before) can print the permissions as octal:
Later (2001, long after
Long before those, IRIX already had a
Again, when there's no standard command, the best bet for portability is to use
You can ask GNU
So in your case:
Or you can even automate it by formatting
The above solution will also work for multiple files if using a wildcard:
Will work correctly with file names containing whitespace characters, but will fail on file names containing single quotes.
I once came up with:
(more as an exercise in
This command on Mac under sh
if you only want the numeric permission, use %Lp only.
The 700 is the numeric permission which can be used in chmod, and Desktop is the filename.
If your goal is to take permissions from one file and give them to another as well, GNU
Here's an answer to question Y (ignoring question X), inspired by the OP's attempt:
And, yes, I know it's better not to use
Also, I just found the following code, by cas, under How to restore default group/user ownership of all files under /var?:
I haven't tested it, but it looks like it should work. Note, though,
that while this answer is superior in terms of simplicity and clarity,
mine is actually shorter (counting only the code inside the loop;
the code that handles a single
Of course, you should not parse the output of
An alternative, if you want to save the permissions away, to restore them later on, or on a different file is to use
(on Solaris, use
However, though they are available on some BSDs, they are not on Apple OS/X where the ACLs are manipulated with
On Mac OS X (10.6.8) you have to use
To just translate a symbolic permission string produced by