osx - mdfind -onlyin not working in hidden directories recursvily? How to use it properly?

07
2014-07
  • andi

    I am trying to find all files named doc.py in one of my virtualevns (called data_science), which are all in hidden directory .virtualenvs under /Users/adni and so I go:

    mdfind -onlyin data_science  -name doc.py 
    

    it returns NOTHING.

    One hint is thtat is not working recursivly or has some problems with hidden dirs. If it matters I run it in zsh shell.

  • Answers
  • ؘؘؘ

    I think Spotlight doesn't index files or directories whose name starts with a period, or at least they are not shown by mdfind.

    I couldn't find any documentation about it, but for example mdfind kMDItemFSName=.bash_history doesn't find ~/.bash_history either. If you run sudo opensnoop in one shell and mkdir .a;touch .a/{1..1000};mdimport .a in another shell, you'll see that files in .a are not opened, but if you run mkdir a;touch a/{1..1000};mdimport a, files in a are opened.


  • Related Question

    osx - Pipe to grep not working in zsh
  • Yar

    I am doing something I always do in Bash:

    set | grep -i path 
    

    and the output is

    Binary file (standard input) matches
    

    What's wrong? grep --help works, and set | more works too.


  • Related Answers
  • Doug Harris

    Do this:

    set > /tmp/zshset
    

    Then open /tmp/zshset in your favorite editor. Look for IFS.

    The default value for this, per the zshparam man page, is default space, tab, newline and NUL. This last one is causing the trouble. grep sees the NUL (ascii 0, displayed often as ^@) and thinks that this is a binary file.

    Possible solutions:

    • Set IFS to some other value. This might cause problems if something else you do expects that NUL is a valid delimiter.
    • Use grep -a as suggested by KeithB (or its equivalent --binary-files=text)
    • Create a new alias for yourself which uses grep -a to save yourself a little bit of typing
    • Adapt to looking at your PATH through other means (e.g. env | grep -i path, echo $PATH) -- I think the other solutions are simpler
  • KeithB

    I'm not sure what is going on, but you can pass the -a flag to grep to force it to treat its input as text, regardless of what it thinks that it is.

  • Adrian Frühwirth

    Another workaround if your grep doesn't implement -a is to do:

    $ set | cat -v | grep foo