Locking and Unlocking Files
For years, the Macintosh operating system has allowed you to lock a file or folder to protect against accidental deletion or modification. In OS X, you have that same ability, either through the Finder or the shell.
If there’s one thing that friends and family know, it’s how to find that one file you’ve been spending an inordinate amount of time on and then delete it in a fit of gleeful innocence. For many years in Mac OS 9 and earlier, the first line of defense for this behavior has been locking a file, making sure that it can’t be changed or deleted unless it is specifically unlocked.
This ability remains in Mac OS X and applies even to the root user (locked files can’t be deleted without being unlocked first, period). Longtime Mac users are familiar with the following process for locking a file or directory in the Finder:
Select the file or directory you want to lock.
Choose Get Info from the File menu or press

-I.
Place a check in the Locked checkbox.
Either of the following two shell commands do the same thing under OS X:
%chflags uchg filename.txt%/Developer/Tools/SetFile -a L filename.txt
The chflags
utility is part of a default OS X
install and changes the
uchg
flag of a file, representing the immutable
bit (literally, “this file is not subject to
change”). chflags can be
performed only by a superuser or the file’s owner.
SetFile
is a utility ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access