What's are the technical differences between "chmod -w file" and "chflags uchg file"

BSD/MacOS question.

chmod -w file will remove all the write permissions from file

chflags uchg file will set the user-immutable bit (aka "Locked" bit) on file

Beyond the obvious statement of "setting permissions vs setting a flag", what are the technical differences and behavior differences of these two commands? When is one preferred over the other?


Three differences come to mind off the top of my head:

  • The uchg flag locks the contents of the file and its directory entry, while the file permissions only apply to the file's contents. This means that a file with all write access removed can still be freely moved, renamed, or even deleted by any user with write access to the directory the file's in.
  • The root user ignores file permissions, but is subject to a uchg lock. But the root user can unlock the file, so this isn't that deep a difference.
  • At least on OS X, removing all write access in the POSIX permissions can be overridden by an access control list on the file. chmod -w will not remove ACL entries granting write access, so some users and/or groups may still have write permissions.