10

These two files are artefacts of Office 2011 that are preventing removal the 'Contents' directory. How do I delete these?

sh-3.2# ls -l -a
total 21496
drwx------+ 15 Beef  staff      480 Jan 14 23:32 .
drwxr-xr-x+ 19 Beef  staff      608 Jan 14 23:40 ..
-rw-r--r--@  1 Beef  staff    10244 Jan 10 10:36 .DS_Store
-rw-r--r--   1 Beef  staff        0 Dec 25 23:13 .localized
drwxrwxrwx  47 Beef  staff     1504 Jan  2 00:01 CSC119 - 1228
drwxrwxrwx   7 Beef  staff      224 Jan  1 22:50 Chris 1228
drwxrwxr-x@  3 Beef  staff       96 Aug 25  2010 Contents
drwxrwxrwx@  5 Beef  staff      1
sh-3.2# cd Contents
sh-3.2# ls -l -a
total 0
drwxrwxr-x@  3 Beef  staff   96 Aug 25  2010 .
drwx------+ 15 Beef  staff  480 Jan 14 23:32 ..

Specifically the these to above '.' and '..' files in the Contents dir. I am using a bash shell on my Mac terminal. I am in root.

Thanks!

sh-3.2# ls -a -l
total 21496
drwx------+ 15 Beef  staff      480 Jan 14 23:32 .
drwxr-xr-x+ 19 Beef  staff      608 Jan 15 00:19 ..
-rw-r--r--@  1 Beef  staff    10244 Jan 10 10:36 .DS_Store
-rw-r--r--   1 Beef  staff        0 Dec 25 23:13 .localized
drwxrwxrwx  47 Beef  staff     1504 Jan  2 00:01 CSC119 - 1228
drwxrwxrwx   7 Beef  staff      224 Jan  1 22:50 Chris 1228
drwxrwxr-x@  3 Beef  staff       96 Aug 25  2010 Contents
drwxrwxrwx@  5 Beef  staff      160 Dec 28 12:09 Lily 1228
drwxrwxrwx  29 Beef  staff      928 Jan  3 21:48 Old Dell Laptop
lrwxr-xr-x   1 Beef  staff       29 Dec 26 02:51 Relocated Items -> /Users/Shared/Relocated Items
-rwxrwxrwx@  1 Beef  staff  1554944 Jan  8  2019 SanDisk Flashback.pdf
drwxrwxrwx   4 Beef  staff      128 Jan  8  2019 SanDiskSecureAccess
-rwxrwxrwx   1 Beef  staff  8600360 Nov  4  2016 SanDiskSecureAccessV3.01_win.exe
drwxr-xr-x@  9 Beef  staff      288 Jan 10 11:41 Scanned Docs
drwxrwxrwx   4 Beef  staff      128 Dec 19 10:04 google
sh-3.2# rmdir Contents
rmdir: Contents: Directory not empty
sh-3.2# rmdir -p Contents
rmdir: Contents: Directory not empty

How do I get rid of this dir?

sh-3.2# ls -a -l
total 21496
drwx------+ 15 Beef  staff      480 Jan 14 23:32 .
drwxr-xr-x+ 19 Beef  staff      608 Jan 15 00:19 ..
-rw-r--r--@  1 Beef  staff    10244 Jan 10 10:36 .DS_Store
-rw-r--r--   1 Beef  staff        0 Dec 25 23:13 .localized
drwxrwxrwx  47 Beef  staff     1504 Jan  2 00:01 CSC119 - 1228
drwxrwxrwx   7 Beef  staff      224 Jan  1 22:50 Chris 1228
drwxrwxr-x@  3 Beef  staff       96 Aug 25  2010 Contents
drwxrwxrwx@  5 Beef  staff      160 Dec 28 12:09 Lily 1228
drwxrwxrwx  29 Beef  staff      928 Jan  3 21:48 Old Dell Laptop
lrwxr-xr-x   1 Beef  staff       29 Dec 26 02:51 Relocated Items -> /Users/Shared/Relocated Items
-rwxrwxrwx@  1 Beef  staff  1554944 Jan  8  2019 SanDisk Flashback.pdf
drwxrwxrwx   4 Beef  staff      128 Jan  8  2019 SanDiskSecureAccess
-rwxrwxrwx   1 Beef  staff  8600360 Nov  4  2016 SanDiskSecureAccessV3.01_win.exe
drwxr-xr-x@  9 Beef  staff      288 Jan 10 11:41 Scanned Docs
drwxrwxrwx   4 Beef  staff      128 Dec 19 10:04 google
sh-3.2# sudo rm -r -f Contents
rm: Contents: Directory not empty

It still won't delete.

muru
  • 72,889

4 Answers4

32

Your directory looks empty, but the ls output indicates that there is a file in there since the link count for the directory is 3 rather than 2 (an empty directory on an APFS filesystem should have a link count of 2).

This implies that your filesystem has managed to get itself into an inconsistent state and that you should probably run fsck on it. On macOS, this is done by running the disk first aid via the "Disk Utility" app. If the issue is with your main boot disk (often called "Macintosh HD", at least by default), then you should do this from recovery mode.

If this is the same issue that I encountered, then it will most likely be solved by first making sure that you are on a recent version of macOS (somewhere past March 2019) before running the first aid on the disk in recovery mode.

You get into recovery mode by pressing Cmd+R as soon as you reboot the machine. Once in recovery mode, you will be presented with the opportunity to run "Disk Utility" from a menu.

Kusalananda
  • 333,661
  • 1
    If you were right, I would not use that filesystem and switch to a reliable filesystem. Ihowever suspect that this is a result of the implementation of anattribute directory when using extended attribute files as indicated by @. – schily Jan 15 '20 at 09:00
  • 3
    @schily This seems to have been a bug in APFS, or in fsck_apfs (which did not repair such error earlier), that was fixed after March 2019. The alternative filesystem on macOS would probably be HFS+, but since Apple, with one of the Mojave updates, automatically converted HFS+ volumes to APFS volumes, I'm not sure they want to support HFS+ as boot volume in upcoming versions of the operating system. They don't to my knowledge currently support any other filesystem for boot volumes, so choosing something else (for macOS) would be more hassle than going with Apple's suggestion. – Kusalananda Jan 15 '20 at 15:17
9

The directory entries . and .. are an artefact from the early 1970s, when UNIX did run on tiny machines and code needed to be tiny as well.

So the people created real hardlinks to directories named . and ... This bad idea causes the hard link count of an empty directory to be 2.

Today, this is nonsense and the POSIX definition since 1988 is that these entries do not need to exist. Their behavior just has to be emulated. That means that if . or .. appear in a path name, the filesystem just needs to behave the right way.

Modern filesystems do not have . or .. entries in directories and do not have a hard link count of 2 for an empty directory.

ZFS is implemented this way, but pretends the historic behavior to avoid badly written programs to fail. So ZFS delivers . and .. with readdir() even though these entries do not exist.

Regardless of whether you are on a modern filesystem or a historic filesystem, you cannot remove these entries.

Now to your link count 2....

On a historic filesystem, the OS typically believes that a directory with a link count > 2 Has sub-directories and thus cannot be deleted.

If the link count in your directory is not the result of a hidden "attribute directory" for the listed extended attribute files, this must be a result of a bug in the filesystem.

If your OS does not allow to remove that directory using the unlink program instead of rm, while being root, you need to run fsck.

schily
  • 19,173
5

My first guess would be that you have files that have been deleted, but are kept open by a running process. Use sudo lsof | grep Contents - this should give you all the PIDs that have files open with names containing the word 'Contents'. Here's the first couple of lines of the output to show you the headers:

$ sudo lsof | head
COMMAND    PID  TID             USER   FD      TYPE             DEVICE  SIZE/OFF       NODE NAME
systemd      1                  root  cwd       DIR              253,1      4096          2 /
systemd      1                  root  rtd       DIR              253,1      4096          2 /
...

When you get rid of the process that keeps something open in your directory, you will be able to delete it.

j4nd3r53n
  • 715
1

Try rebooting. It may be that you have a process which still has a deleted file open in Contents directory. A reboot will release the file and you will be able to delete the directory.

Paulo Tomé
  • 3,782
SimonP
  • 27