CNET también está disponible en español.

Ir a español

Don't show this again

HolidayBuyer's Guide
Computers

Managing locked files in OS X

If you can't edit a file in any application, including the Terminal, then it may have a lock flag associated with it.

In OS X there are several ways to prevent access to files, including putting them in locked disk images or changing their permissions setting so only select accounts on the system are permitted to read them.

In addition to these options, the OS X Finder supports another method of locking a file. When you open the information window for the file, you can check the Locked check box to prevent the file from being edited, even by people who otherwise have access to it. When this lock is enabled, the file's icon in the finder will have a small lock appended to its lower-left corner.

Finder file lock settings
The Finder's information window can be used to set the file lock. Screenshot by Topher Kessler/CNET

This option sets a lock flag for the file that is recognized system-wide and not just by the Finder or by applications that users generally interact with. So not only will applications like TextEdit and Pages not be able to edit the locked file, but command-line tools and other tasks should also be prevented from modifying it.

Not only will the lock prevent most tools from editing the file, but it also supersedes file permissions and extended attribute settings, so not even commands and programs run under the root account will be able to edit it with the lock enabled. This is significant for power users who make use of the Terminal, since if they cannot edit a file even when they try using "sudo," then the file may be locked. As an extra hurdle for those who use the Terminal, there is no obvious indicator such as the Finder icon that will show you the file is locked--it just will not be editable.

In addition to potential frustrations with locks when managing files in the Terminal, sometimes the Finder itself will provide obstacles when managing locked files. While you should be able to use the Finder's information window to set and remove file locks, sometimes the Finder will not allow you to do this. In most cases, the Finder will merely prevent the lock from being set in the first place; however, at times the file's lock setting may be set but will be grayed out so you cannot unlock it.

Locked file icon
When a file is locked, its icon will have a small lock appended to it. Screenshot by Topher Kessler/CNET

Luckily, if you do run into problems with a file that cannot be edited, either in the Terminal or in the Finder, you can unlock it by performing the following steps using the Terminal:

  1. Open the Terminal.
  2. Type the following command, followed by a single space (do not press enter yet).

    sudo chflags nouchg

  3. Drag the locked file from the Finder to the Terminal window to complete its full folder path.
  4. Press Enter to run the command, supplying your password when prompted.

When run, this command will undo the lock flag for the file, regardless of whether you own the file. While an enabled lock will prevent the file from being edited, ultimately it is designed to be a temporary hurdle that can be undone by any user who has access to the file.



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.