144

I copied a lot of read-only files from a Windows system to my Mac. When viewing the Info for each file using "Get Info", I can see they are Locked. I'm writing a bash script to copy over some files and I'm getting an error that says "Operation not permitted" So, first I need to unlock the files. Since I'll be pulling files from the Windows system often, I want my script to unlock these files.

What is the terminal command to unlock "Locked" files on OSX?

4 Answers 4

206

To unlock files you can use:

chflags -R nouchg /PATH/TO/DIRECTORY/WITH/LOCKED/FILES/
  • chflags = change flags on files/folders such as "locked"
  • -R = recursive or for everything and follow directories within the specified directory
  • nouchg = means the file can be changed
  • /PATH/ = of course is the path to the files you want to change. Something like: ~/Sites/mysite/directory/with/locked/files/ works as well.
6
  • 1
    Looks like I found the solution just as you were responding. I also learned that the -R is for recursive. So, to unlock all files in the current directory use<br/> chflags nochg *<br/> and to change just one file<br/> chflags nouchg onefile.txt See: mehtanirav.com/2009/04/16/recursively-unlock-files-on-mac-os-x Sep 14, 2009 at 2:37
  • 5
    There's another relevant flag schg which is the system immutable flag (see developer.apple.com/library/mac/documentation/Darwin/Reference/…) The command to clear it is similar: sudo chflags noschg PATH/TO/LOCKED/FILE
    – Andy Madge
    Jan 12, 2015 at 20:22
  • 1
    Use /bin/ls -lO /path/to/file to view the macOS flags on files. Notice the full path to the macOS 'ls' command. This is needed if you have GNU Coreutils installed. Nov 7, 2018 at 5:57
  • @AndyMadge - I needed to use that flag, from following directions on this page: askdavetaylor.com/how-to-delete-locked-file-on-mac-macos. But the link you provided is dead. Do you have an updated link?
    – Adam_G
    Mar 16, 2021 at 19:54
  • 1
    @Adam_G seems like Apple have moved the web page, but you can view the man page directly in MacOS terminal with the man chflags command
    – Andy Madge
    Mar 17, 2021 at 12:34
25

This is helpful if you want to search an entire directory and unlock all files.

In the terminal cd to the directory

This command finds and will print a list of them.

$ find . -flags uchg

This command unlocks them.

$ find . -flags uchg -exec chflags nouchg {} \;

You can use the first command to double check that all the files are unlocked after running the second command, voilà !

2
  • 1
    While this works, it can be extremely slow for large numbers of files because it starts a new process for each file. Aug 23, 2014 at 23:52
  • 1
    I find I have to use + : find . -flags +uchg even to find files that only seem to have the one flag, uchg, set. Nov 28, 2019 at 13:05
18

You can also use SetFile -a l, even though it does the same thing as chflags nouchg:

SetFile -a l file.ext

-a l unsets the bit for the locked attribute. You can install SetFile by downloading the Command Line Tools package from Xcode's preferences or from developer.apple.com/downloads.

2
  • This is the method that worked for me on macOS Sierra with a few files copied from a Windows machine. I used : SetFile -a l ~/Documents/Arduino/Samples/*
    – callisto
    May 18, 2017 at 6:59
  • As fyi, /usr/bin/SetFile (setfile) was deprecated with Xcode 6 along as were other tools supporting Carbon development; although SetFile still appears to install as part of Xcode 12 additional command line tools. See man SetFile May 29, 2021 at 3:38
12

There are actually two lock flags that can be set on a file: uchg and schg. A file that has the uchg flag set is immutable by normal users but it is mutable by the system. A file that has the schg flag set is immutable by anyone. Both flags can be set at the same time.

To see which flags are set on a file, use

ls -lO FILE

That is a capital letter o, not zero.

To definitely unlock a file, you would have to execute

chflags nouchg,noschg FILE

as if both flags were set and you remove only one of them, it will still be locked.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .