How to write protect a file in macOS

Saturday, July 6, 2024 at 10:36 AM | 2 min read

Last modified on Thursday, May 28, 2026 at 9:11 AM

#chflags, #command line, #macOS, #modify file flags, #schg, #system immutable flag, #uchg, #user immutable flag, #write protect files

Immutable records

Photo by Mr Cup / Fabien Barral on unsplash.com

Sometimes you want to make sure that important files are not removed via Command Line when using the rm (remove) command. You want to make sure that these important files are write protected.

I'm going to use one of my shell script files I have created and spoke about in previous posts as an example. In general, I want to make sure that my shell scripts cannot accidentally be removed either!

When a file is "write protected", that means that it is "unalterable". It cannot be changed or deleted by any type of user, including the root (or superuser) in the macOS UNIX operating system.

First I use the chflags (change flags) command followed by the schg (set the system immutable flag) flag. This sets the "set the system immutable flag" type to a file:

chflags schg hello.sh

However, if I run the above command as is, the following is returned in Terminal:

chflags: hello.sh: Operation not permitted

That's because I have to run it as superuser. So I have to add sudo to the beginning of the command like so:

sudo chflags schg hello.sh

And the following is returned:

Password:

I have to input my user password.

Then, if I try and delete the hello.sh file with the command rm hello.sh, the following is returned in Terminal:

override rwx------ mariacam/staff schg for hello.sh?

If I do actually want to delete the file, I type "y" for "yes", and if this was a mistake and I don't want to delete it, I type "n" for "no".

I can also run the following command without sudo (which stands for "superuser do"):

chflags uchg hello.sh

uchg represents the "user immutable flag". With the "system immutable flag" (schg), the superuser can remove whichever file across the whole computer system, no matter who owns which file. With the "user immutable flag", only the owner can delete files which they own. Other users on the system cannot delete another owner/user's files.

So if I run the chflags uchg test.txt command, followed by the rm test.txt command, the following is returned in Terminal:

override rwx------ mariacam/staff uchg for test.txt?

Again, I can either type "y" for "yes" if I want to actually delete the file, or "n" for "no" if I don't. This prompt gives me the opportunity to change my mind before actually deleting the file. However, if I do type "y" to remove the file, I get the following in Terminal:

rm: test.txt: Operation not permitted

This proves that the test.txt file is truly "write protected" or "immutable".

If I decide that I no longer want the test.txt file to be write protected, I can "unprotect" the file and therefore delete it with the rm command. To achieve this, I run the following command in Terminal:

chflags nouchg test.txt

And then if I run the following command:

ls -l test.txt

The following is returned in Terminal:

-rwx------ 1 mariacam staff 0 Jul 6 07:21 test.txt

And then if I run rm test.txt followed by the ls command inside the macos-scripting folder, the following is returned:

concatenate.sh play_album.sh concatenate.txt play_song.sh download_install_firefox.sh wifi_reset.sh hello.sh

The test.txt file is successfully removed. The same can be done with the schg flag. Instead of nouchg I would use noschg. I simply have to put no in front of either the "user immutable flag" (uchg) or the "system immutable flag" (schg) to remove the file's write protection. no clears the flag.

Note: The above commands work for both files and folders alike.

Apple OS X: Write Protect File From Command Line: by Vivek Gite, cyberciti.biz