Why ‘ls’ Outputs Only ‘.’ in Root Directory of External Drive

lsosxusb-drive

In the root directory of my USB flash drive, sometimes when I run ls, the output is normal and it lists the files. At other times, the output is simply one line:

$ ls
.

If I try ls -la at one of those times, I get this:

$ ls -la
ls: .: Invalid argument

If I run ls back to back multiple times, it seems to return either the normal output or the abnormal one basically at random.

ls appears to work normally in other directories. ls $drivename even appears to work fine from the parent directory, and ls .. seems to work fine from a child directory. (Though I can't be 100% sure of the ones that "work normally" since the behavior is indeterminate to begin with.) I tried two other external USB drives and got the same behavior.

What's going on here? I'm on Mac OS X 10.11.3.

Edit: Nice idea, but I don't seem to be using an alias, and /bin/ls gives the same result.

Best Answer

It may be a bug in the filesystem driver for FAT32 on recent versions of OSX. This also only appears to occur when the working directory is at the root of the mounted drive. If it's in a subdirectory or anywhere else on the system things seem to work.

There's some interesting discussion in this thread including system traces. https://github.com/robbyrussell/oh-my-zsh/issues/4161

Related Question