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
Here's an answer to question Y (ignoring question X), inspired by the OP's attempt:
The above contains a few bashisms. The following version seems to be POSIX-compliant:
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 have tested this code (but not thoroughly), and it seems to work,
except for the fact that it doesn't recognize
Of course, you should not parse the output of
To convert from the symbolic to octal notation, I once came up with:
That returns the octal number from the output of
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.
On Mac OS X (10.6.8) you have to use
To just translate a symbolic permission string produced by
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.
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
If your goal is to take permissions from one file and give them to another as well, GNU