0

I'm learning how to change the permissions on files. On my macOS system, I want to copy the permissions from one file to another using the chmod --reference=reference-file file command, however it keeps returning

chmod: illegal option -- -

Any help would be greatly appreciated.

0
1

The chmod on macOS is not GNU chmod (which is usually found on Linux systems), and does not therefore support the special non-standard GNU "long options".

On macOS, you may still get access to GNU chmod via the Homebrew package manager. The utility is available as gchmod once you install the coreutils package with

brew install coreutils
0

If your version of chmod doesn't support --reference and you don't want to install additional software, you can use stat to get the reference file's permissions, and use that. Unfortunately, stat itself is hopelessly inconsistent between different OSes/versions. On macOS (and NetBSD and probably other BSDs), you'd use:

chmod "$(stat -f "%Mp%Lp" referencefile)" file

While if you had the GNU version of stat (and for some reason didn't have the GNU version of chmod), it'd be:

chmod "$(stat -c "%a" referencefile)" file

On other OSes... check man 1 stat and see what the local dialect is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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