Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Locked files and Mac OS X System
There's an annoying side effect of locking file when it comes to Mac OS X -- there's no fast way to override the state, like option-Empty Trash in OS 9.

Not only does this not exist, but the Finder will report that you do not have "sufficient privileges" to delete the file (which may be correct in a way, but rather misleading since we now have permissions to worry about).

Does anyone know of a quick way to remove file locks from the Terminal or another app? I've tried xFiles 1.0b1, but it doesn't seem to recognize that the files are locked at all.
    •    
  • Currently 2.79 / 5
  You rated: 1 / 5 (14 votes cast)
 
[210,223 views]  

Locked files and Mac OS X | 31 comments | Create New Account
Click here to return to the 'Locked files and Mac OS X' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
this can be done form the Terminal
Authored by: Anonymous on Mar 28, '01 04:09:21PM
I've run into this same problem, where I couldn't delete a file because osX thought it was in use. If the file hasn't yet been moved to the trash, try to log into the terminal as root and use the rm command to delete it. Be careful that you're deleting the correct file, as by default rm does not ask for confirmation. You can use tab-completion to get osX to insert the filename for you. ie to delete a file called My File Is Locked, you could do this:

rm "My (then hit the tab key) " (put an endquote in there also)


Make any sense? also, if your file is already in the trash, you can search for it with sherlock, and then drag the icon to the Terminal - it's full path wil pop up, allowing for easy deletion.

Be careful with rm!



[ Reply to This | # ]
re: this can be done form the Terminal
Authored by: Anonymous on Mar 28, '01 07:23:02PM

that's not actually the case with locked files.

if the file is locked, then when you try to rm, even as root, it prompts
"override <permissions> for <filename>?"
hitting y for yes returns
"rm: <filename>: Operation not permitted"
and the file stays there.



[ Reply to This | # ]
re: this can be done form the Terminal
Authored by: alanw on Apr 14, '01 01:11:16AM

It is true that even as root via the Terminal you cannot use the usual UNIX "rm" command to delete locked files. This is disturbing (especially unsettling to one who has done Mac troubleshooting for years and is well acquainted with Murphy's Law--if something can go wrong, it will). Suppose that by whatever cause, malicious or otherwise, you find your disks filled with hundreds or thousands of locked files. There should be some backdoor method (certainly, I agree, not one to be invoked casually) to unlock them in batch fashion (apart from rebooting into Mac OS 9.1; perhaps someday that won't be an option).
Well, after puzzling over this for some time, I finally discovered that backdoor method. You need to have the Developer Tools installed. Open a Terminal window and type (without the quotes)
"/Developer/Tools/SetFile a -l <names of files to unlock>" You can use wildcards. "/Developer/Tools/SetFile a -l *" for example will unlock all files in the current directory. If some are already unlocked, their lock attribute remains unchanged. Experienced UNIX hackers will no doubt be able to use shell scripts or more complicated command combinations to unlock in batch fashion any files anywhere.



[ Reply to This | # ]
re: this can be done form the Terminal
Authored by: alanw on Apr 14, '01 01:19:34AM

Correction: That should have been "/Developer/Tools/SetFile -a l <names of files>" and
"/Developer/Tools/SetFile -a l *"
I knew I shouldn't have stayed up this late. :-)



[ Reply to This | # ]
re: this can be done form the Terminal
Authored by: sauy7 on Oct 30, '04 06:39:48AM
Here's a simple bash script to run in a Terminal window

#! /bin/bash
#
# Simple file unlocking utility for Mac OS X
#
ARGS=1
E_BADARGS=65

function recursiveUnlock() {
    pushd $1
    for rdir in `ls -A`; do
        if [ -d "${rdir}" ]; then
            recurse ${rdir}
        else
            /Developer/Tools/SetFile -a l ${rdir}
        fi
    done
    popd
}

if [ $# -ne "$ARGS" ]
then
    echo "Usage: `basename $0` starting-directory"
    exit $E_BADARGS
fi

recursiveUnlock $1
Usage (code saved as an excutable file recursiveUnlock.sh):

$ ./recursiveUnlock.sh /path/to/top-level/dir
unlocks all files in dir, including those in dir's sub-directories

---
sauy7

[ Reply to This | # ]

re: this can be done form the Terminal
Authored by: gwik on Mar 01, '06 09:55:25AM
$ find *the_root_dir* -type d -exec /Developer/Tools/SetFile -a l {} \;

[ Reply to This | # ]
this can be done form the Terminal
Authored by: rodrat on Dec 09, '02 05:39:58PM

To trash a locked file,I found the problem is hidden files. control
click on the folders that won't open and click on reveal to open folder.
Then unlock hidden file with GETINFO!



[ Reply to This | # ]
this can be done form the Terminal
Authored by: rodrat on Dec 09, '02 05:42:59PM

To trash a locked file,I found the problem is hidden files. control
click on the folders that won't open and click on reveal to open folder.
Then unlock hidden file with GETINFO!



[ Reply to This | # ]
this can be done form the Terminal
Authored by: tolba on Apr 10, '06 07:32:19PM

I have tried almost every suggestion I have seen to delete a particular file. This file is on an external hard drive, it is locked and no matter what I do it will not go away. It is not a virus it is a fractal software program. I will take all the help I can get. Please be gentle I am new at this terminal window thing.

thanks
tolba



[ Reply to This | # ]
Do it the old way
Authored by: Anonymous on Mar 31, '01 01:10:26AM

hit cmnd-I to launch the inspector and un-check the "locked" checkbox for the offending file. I know this is slow but that's the purpose of locking a file...to prevent it from being casually deleted. With OS X we can prevent casual deletion with permissions so this is less of an now and locked files are a now a PITA. I agree, Apple should enable the option-empty trash combo for OS X.

Y

ps. do you know about shift-cmnd-delete then return shortcut in the finder to empty your trash? Better then nothing, but no option overrides are available that I know of



[ Reply to This | # ]
Do it the old way
Authored by: mchoya on Mar 21, '06 02:23:54PM

Thanks! This worked like a charm. I have had this locked file on the desktop for months.



[ Reply to This | # ]
Unlocking files using Terminal
Authored by: vxbush on May 29, '01 05:33:04PM
I don't mind unlocking files on an OS 9 volume using "Get Info", but I find it really aggrivating to do this for a folder with more than five files. I frequently have thirty or more files that that are all locked, and it gets really annoying.


I used Terminal and did an ls -la, and saw the following:



-rwxrwxrwx 1 vbush notes - 1082304 May 1 12:58 file1*
-rwxrwxrwx 1 vbush notes uchg 105698 Apr 19 14:17 file2*

The first file is not locked; the second file is. Notice the "uchg" entry. How can I go through and remove this from all the files? I can't tell what kind of attribute this is.

--vb

[ Reply to This | # ]
Re: Unlocking files using Terminal
Authored by: blb on May 29, '01 11:41:38PM
uchg is the 'user immutable' flag (ie, you can't delete it, as you've found). I know of two ways to clear this flag, either use (from a Terminal) the chflags command (see man chflags for info); the other, grab ChangeFileFolderProps to do this with a GUI.

[ Reply to This | # ]
Unlocking files using Terminal
Authored by: ankh on Dec 09, '02 08:53:01PM

I noticed I can select All of a group of files, get information, and it'll let you unlock everything selected. Had no idea it would do that.

Hope it does it tomorrow. Never can seem to predict what X will do, yet.



[ Reply to This | # ]
Unlocking many files
Authored by: absorber on Dec 13, '01 09:42:40PM

I recently had the bad luck of running NAV 7 and it locked about 70 per cent of the files on my hard drive. This creates a real problem when the locked files are preferences or identities and such. I considered trying to write a script to unlock them but have selectively been unlocking them using the Get Info (CMD I). The trick to doing it fast is to sort the files in List View by Kind. Then select ALL the individual files in the list, deselecting any folders you accidentally select. Now press Command I and the Get Info Window will appear. If none of the files are locked the check box in the window will be empty. If all the files are locked the check box will be checked. If there is a mixture of locked and unlocked, a dash will be in the box. Click on the box to change them all to unlocked. (I don't know if there is a limit to the number of files you can open at once, but I have done a bunch.) Open the next folder and repeat...

This is the fastest method I have found. If anyone knows of an easier method or has written a script to go through a whole drive and unlock all the files, I would love to try it. Meanwhile no more Norton for me.



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: stefan724 on Jan 07, '05 04:00:02PM

I found a little freeware program called locker that will handel multiple files. Works good for me.

http://www.vojousoftware.com/products.html



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: tipstuff on Jan 15, '05 08:07:56AM

There is also another way I discovered.
you can take any amount of files and in the stuffit pref just select delete original. then simply make and archive and delete it.
Hope it will work for you too.



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: kcoates55 on Mar 29, '06 09:52:05AM

This is a great little program. I have OS 9 on a separate hard drive so booting into OS 9 didn't work and I'm terrified of the terminal. Thanks for your contribution!



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: renderhead on Jan 21, '05 04:35:18PM
Using other comments in this thread for reference, I managed to put together the following procedure for unlocking every file in a directory and all of its sub-directories. When a file is "locked" by OS X, it is applying what *nix calls a "flag" to the file. In this case, the flag is "uchg", which means that no user, even root, can alter the file in any way until the flag is removed. You can use the command chflags to remove the flag.

The following example should unlock every file in your home directory:

sudo chflags -R nouchg ~/
Here's a breakdown of the command:

sudo will run the chflags command as root (you may be prompted for your root password).

-R will cause the command to affect every file in the directory and all of its subdirectories. Yes, capitalization matters.

nouchg is the syntax for removing the uchg flag. Any flag can be removed by putting "no" in front of it.

You can of course substitute the tilde (~) with whatever directory path you want to affect. To change an individual file, leave out the -R and type the file path instead of a directory path. Normal operators apply, such as $ and *.

[ Reply to This | # ]

For the Trash...
Authored by: renderhead on Jan 21, '05 04:48:57PM
To apply my above hint to items already in the trash, use:
sudo chflags -R nouchg ~/.Trash
To empty the trash entirely, add a simple
rm -f ~/.Trash/*
I'll bet someone who knew their applescript could write up a simple "Force Empty Trash" menu item based on those two commands.

[ Reply to This | # ]
Locked files and Mac OS X
Authored by: johnsawyercjs on Jan 22, '05 02:35:38AM

The utility "BatCHmod" will let you drag any file, folder, or volume to it, and then let you unlock all items inside that item.



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: tribe on Apr 05, '07 08:46:39AM
Hi,

you can easily unlock multiple files and folders using the terminal. Just follow the instructions :

http://docs.info.apple.com/article.html?artnum=106272

cheers

[ Reply to This | # ]
Locked files and Mac OS X
Authored by: jakecollins on Jan 12, '08 04:30:31PM

Guys, guys, guys.. There is a much easier way using the GUI in 10.4. Read on.

I don't mind using terminal sometimes, but let's face it why not use the GUI if possible?

Normally if you select multiple files and then to command I you'll get a ton of windows coming up and you'll have to individually unlock each file. Here's the magic. Hold down alt and command when you press 'I'. You now only get one window showing multiple file info, meaning one or two clicks to unlock all the files in one go. Another great thing is the mulitple file info window does not even need to be closed. Once you've finished in one folder simply move to the next and select more files.

I think this is what Absorber was trying to explain in his post. He missed out the vital step of using alt and command together though. You also don't need to go to list view, it works in any finder view.



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: jemacd on May 04, '08 10:48:35AM

How I do it is as (after going through all the terminal scripts and other 'solutions')

If you cannot un-check the 'locked' check box when 'get info' is invoked (or if you can but you STILL have un-deletable files), then open trash and follow the path inside the folder(s) to the end file/folder that has a tiny padlock on the bottom left.

Then, 'get info' for that specific one and un-check the locked box.

Do this for each individual file/folder (yep, its a pain) and you will magically be able to empty the trash.

I think the terminal script way (published in several places, including the max site) didn't work because the route path wasn't specific to the individual lock.

Anyway - hope this helps peeps.

JMac



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: inspired_tmu on Jan 20, '09 01:51:48PM

This was exactly what I was looking for! Thanks!

The command I ended up using:

sudo chflags nouchg <filename>



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: alekksa on May 02, '09 09:12:29PM

I am using software called FileBuddy. It also have function to search all Locked files and you can remove Locked status from all the files (or just from selected) with a click.

FileBuddy is excelent software and I can't imagine using Mac without it.



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: acampbell on May 04, '09 01:49:09PM

Files get locked that I never locked, so it has been frustrating. The easiest why I have found (at least with photos, not sure if it applies to all files) is to view it through Adobe Bridge. Select all and right click. If just one of the selected files is locked you'll get an option to "unlock all", if no files are locked that option will not be available.

Hope this helps. If anyone knows how to prevent these files (usually happens with files being shared through a shared folder) from locking please advise.

thanks



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: barfalot on Dec 09, '09 09:17:25PM

Just run the following command from within the root of the directory of files and sub-directorys whenever you have a lot of locked files. It will unlock every one recursively without writing a shell script. Mac OS X and up.


$sudo find . -print -exec Setfile -a l {} \;



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: TvE on Apr 19, '10 11:42:55AM
So if I use my SHELL to run your SCRIPT - then I don't understand what you mean with

…It will unlock every one recursively without writing a shell script…
;-)

[ Reply to This | # ]
Locked files and Mac OS X
Authored by: elephantmanmusic on May 16, '10 05:51:12PM

The easiest way to do it is to open the folder that all of the locked folders are in. Select the folder. Single click the folder and type command I (as in the letter "I"). At the bottom right of the info screen click the lock. Type in your administrator password which will unlock the permissions. Change the folder from "Read only" to "Read & Write". Now for the cool part. At the very bottom just to the right of the zoom icon, click once and a drop down menu opens. Select "apply to enclosed items" and every folder and program within that folder will be unlocked. You can do the same thing for batch locking programs.



[ Reply to This | # ]
Locked files and Mac OS X
Authored by: magnusalpha on Aug 13, '11 11:54:52AM

There's a whole bunch of complicated stuff being suggested, but all you really need to do is hold 'command-shift' when emptying your trash.

I had a folder in my trash that wouldn't delete when I emptied. Mac OS would ask whether I wanted to delete the locked the file or not. Clicking on "yes please" didn't actually delete it. Going into Inspector showed that it wasn't locked, and making sure all the permissions were 'read & write' didn't help any.

I still don't know why it insisting on saying it was locked, but the Shift-Command while emptying got rid of it for good.



[ Reply to This | # ]