0

On Linux find -type d works to list all sub directories, ignoring files.

However when I run this on a Mac (High Sierra) I get the error: find: illegal option -- t.

On delving into the googles, I hadn't found any obvious alternative for a command line equivalent except for answers suggesting that I use ls and parse the output via grep, or have solutions for GUI apps or for the non command line users (via finder, etc.).

The usecase would be to pipe this output to a fuzzy finder which expects a newline separated list of items. For example I can accomplish this with files and ripgrep with: rg --files -g "" | fzy. Ripgrep doesn't seem to support a --folders option or the like from my cursory browse on the github issue tracker.

On Linux find -type d | fzy "just works". Up to installing other packages, but I really hoped for something that just comes preinstalled.

I can get away from this with some scripting, but I'd love to hear about a best practice here.

4

I think your find does understand -type d because this is required by POSIX. However the syntax you used:

find -type d

is not POSIX-compliant, thus not portable. The proper portable syntax is:

find path -type d

Linux versions of find will assume ./ if you omit path. On Mac find expects something like this:

find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]

You want -type to be a part of expression but your find needs path or -f path in its command line arguments. Before it gets one, it tries to interpret other arguments as options, so your -type is in fact -t -y -p -e; there is no -t option defined, thus illegal option -- t.

(Compare this answer).

The solution is simple: specify a path explicitly. Mac equivalent of Linux find -type d is:

find ./ -type d

Note this works in Linux as well.

  • I've always used the dot without the slash and it works fine (I never realised it even worked without the dot in Linux). I'm just curious if this is a bad habit in general? – paradroid May 13 '18 at 22:24
  • 2
    @paradroid Probably not a bad habit. I just tend to add trailing slash to indicate the object is a directory. In some circumstances this can make a difference though. – Kamil Maciorowski May 13 '18 at 22:38

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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