Search code examples
macosshellterminalzshmdls

Parse error in globbing filenames containing dashes in zsh


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.


Solution

  • 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