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.

  • 34
    . and .. are present in all directories in unix filesystems (they are links to the directory itself, and its parent, respectively). They inherently cannot be deleted themselves, and will not prevent deleting the directory. – Gordon Davisson Jan 15 at 7:21
  • 8
    Quite possibly related: Strange case of truly invisible file (?) and undeletable directory on macOS (on the AskDifferent site) – Kusalananda Jan 15 at 7:38
  • 5
    what does xattr -lr Contents say? – mosvy Jan 15 at 7:52
  • 4
    As @mosvy stated: checking the attributes would be a good start. 'ls -l@ Contents' and 'ls -ll Contents' could also help. – gerhard d. Jan 15 at 9:01
  • 1
    Note that you can combine options for ls, so you can use ls -la instead of ls -l -a. – JPhi1618 Jan 15 at 20:06
31

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.

  • 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 at 9: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 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.

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.

0

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.

  • 2
    Does it really works like that in MacOS? Would a command like mkdir dir && exec 7>dir/file && rm dir/file && rmdir dir FAIL the rmdir on MacOS? – mosvy Jan 15 at 17:59
  • @mosvy That series of commands does not fail on macOS (rmdir exits with a zero exit status). – Kusalananda Jan 15 at 19:32
  • 1
    That's not how it works. – OrangeDog Jan 16 at 11:13
-3

If you are sure you want to remove the Contents directory, you can use:

sudo rm -r -f Contents

This will pass the recursive parameter (which removes directories, removing the contents recursively beforehand) and will use the forced parameter (which ignores non-existent files and overrides any confirmation prompts).

Note that this can not be undone.

(for more info use rm --help or see http://www.gnu.org/software/coreutils/manual/html_node/rm-invocation.html#rm-invocation)

  • 5
    Note that rm --help requires GNU rm and that the manual that you link to is also about GNU rm. macOS does not use GNU tools by default. Also note that the link count for the empty directory is 3 rather than 2, which indicates that there's a file in the directory (assuming the default macOS APFS file system type). Since ls -a does not show this file, this implies a filesystem error. – Kusalananda Jan 15 at 7:42
  • 1
    Even that doesn't work. – nightanimal Jan 15 at 7:47
  • 1
    -f just ignores errors. This doesn't mean the error didn't happen (still preventing deletion) – A.B Jan 15 at 13:05
  • 2
    I think it's a pretty big assumption that a completely invisible file in a directory they want to delete is something anybody actually desires. – davolfman Jan 15 at 17:13
  • No, no, the explanation is more likely to be that a file was deleted while a running process keeps it open (see my answer). In UNIXes (OSX being one such) rm only removes the directory entry, the file only disappears when all hard-links have been removed AND the file is closed. – j4nd3r53n Jan 16 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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