Ran into the same problem tonight as this question from SO that wasn't fully answered.

I'm trying to uninstall an old installation of MacPorts, and run into SIP:

mybox:~ me$ sudo rm -rf /opt/local
rm: /opt/local/var/macports/home/Library/Preferences: Operation not permitted
rm: /opt/local/var/macports/home/Library: Operation not permitted
rm: /opt/local/var/macports/home: Operation not permitted
rm: /opt/local/var/macports: Directory not empty
rm: /opt/local/var: Directory not empty
rm: /opt/local: Directory not empty

According to the original question, the answer seems to involve SIP… but why has SIP decided to protect this (empty) directory?

EDIT: Here's the results of ls -a in the directory:

./ ../

And here's the results of ls -leOd /opt/local/var/macports/home/Library/Preferences:

drwxrwxrwx  2 root  wheel  - 64 Nov  8 19:45 /opt/local/var/macports/home/Library/Preferences/

.

EDIT 2017-11-12:

Here's the results of ls -laeO@ /opt/local/var/macports/home

drwxr-xr-x  3 root  wheel  - 96 Nov  8 19:42 ./
drwxr-xr-x@ 3 root  wheel  - 96 Nov  8 19:45 ../
    com.apple.FinderInfo    32 
drwxr-xr-x  3 root  wheel  - 96 Nov  8 19:42 Library/
New contributor
James Jensen is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • are you sure it is empty or does it has hidden files – Buscar웃 Nov 9 at 2:15
  • There are a number of possible causes for this error. Check the directories' access control list (ACL) and flags with e.g. ls -leOd /opt/local/var/macports/home/Library/Preferences. If that lists a "restricted" flag, the directory of protected by SIP. There are a number of other flags that might forbid deletion; see man chflags for a partial list of them. I don't think an ACL could have this effect, but it doesn't hurt to check that as well. You can add the results to your question by editing it (note: please use code format, as you did for the original errors). – Gordon Davisson Nov 9 at 2:30
  • @Buscar웃 I checked. ls -a returns ./ ../ – James Jensen Nov 9 at 3:54
  • command+Shift+. will show hidden files – Buscar웃 Nov 9 at 3:57
  • 1
    @Wowfunhappy I'm actually more interested in learning what's going on than in solving the problem. – James Jensen Nov 9 at 5:04

Try one of the following

o You are not the owner of the directory

ls -ld /path/to/directory

o You do not have permissions to remove the directory name from the parent directory

o The directory or it's parent has an ACL that is preventing you from deleting the directory

ls -ldeO@ /path/to/directory

ls -ldeO@ /path/to/directory/..

chmod to mess with ACLs

o The directory or its parent has 'uchg' or 'schg' flag

ls -ldeO@ /path/to/directory

ls -ldeO@ /path/to/directory/..

chflags to mess with flags.

and finally this sudo chflags -R nouchg <parent>

  • Op ran the command under sudo so does not matter who is the owner. – Mark Nov 9 at 8:11

Your Answer

James Jensen is a new contributor. Be nice, and check out our Code of Conduct.
 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.