2

When I run mdls -name kMDItemFSName -name kMDItemDateAdded -raw * in zsh in a folder containing the directory - Java Code, I get the following error:

mdls: unrecognized option `- Java Code'

How can I fix this? I tried using "*", but it didn't glob at all.

HappyFace
  • 3,439
  • 2
  • 24
  • 43

1 Answers1

3

The problem is because of the - option in one of the directories. Usually the command line flags are given following a -. A folder name as -Java Code is being treated as one such flags to mdls command which it doesn't like. Usually we provide an end of command line flags separator by providing a -- upon which the shell realizes that no more command line flags are expected.

mdls -name kMDItemFSName -name kMDItemDateAdded -raw -- *

This way since we signaled that the command line flags are done, a folder name containing a leading - should be parsed properly.

The problem is a frequently occurring one and can be simply reproduced by a simple mv command and creating a file containing a - and when trying to move the file, the command would fail. It needs to be fixed by doing mv --

Most of the shell built-in commands and GNU tools support this option. Also POSIX recommends this too. See it from one of their guidelines. See POSIX Utility Conventions - Guideline 10

Inian
  • 80,270
  • 14
  • 142
  • 161
  • Oh, you're right. ‍♀️ I had forgotten `--`. What can one do if the utility in question doesn't support `--`? I mean, why isn't there something akin to "*"? Or is there? – HappyFace Aug 03 '18 at 14:48
  • An interesting note is that there is no `-Java Code`, but `- Java Code`, which is obviously not an option ... – HappyFace Aug 03 '18 at 14:49
  • @HappyFace: Most of the commands should support it. POSIX defines it too – Inian Aug 03 '18 at 14:52
  • 2
    @HappyFace The results of a glob expansion aren't subject to word-splitting, so `mdls` is getting a single argument `- Java Code`, not three arguments `-`, `Java`, and `Code`. Any argument starting with `-` is treated as an option, whether or not the `-` is followed by a space. – chepner Aug 06 '18 at 14:04