I want to open terminal, find a file or folder, and quickly navigate to that folder or the parent directory of the file.

I can quickly get the path using

find ~ -name 'filename.txt' 

but I can't figure out how to copy that path result into the "cd" command

Is there a way to basically say "cd [path from find command]" or "open [path from find command]"

I'm just using the default terminal in OSX Mountain Lion

3 Answers 3


How about:

cd $(find -name "filename.txt" -type f -exec dirname {} \; | sed 1q)
  • it's a workable solution, but not clean like I was hoping... oh well, I guess there isn't a simple clean solution like I was hoping Jul 18, 2013 at 22:58

Like this:

$ cd `find ~ -name filename.txt -exec dirname {} \;`
  • Stalker!!!! :-) Jun 18, 2013 at 5:48
  • @JS: heh - well someone's got to answer all these find questions... ;-)
    – Paul R
    Jun 18, 2013 at 5:58
  • 1
    Yes Sir!! Now we just got to wait for mods to move it to SuperUser. Sigh! Jun 18, 2013 at 6:01
  • I just tried this and got a "No such file or directory error".$ cd find ~ -name filename.txt -exec dirname {} \;`` results in an error, but the find command by itself works perfectly:$ find ~ -name 'filename.txt' works perfectly. (I also can't figure out how to use markdown to delineate the code above because the code contains backticks so markdown stops the code formatting part early) Jul 2, 2013 at 18:22
  • What happens if you just run $ find ~ -name filename.txt -exec dirname {} \; ?
    – Paul R
    Jul 2, 2013 at 20:30

If have added this to ~/.inputrc to make \eo rerun the previous command and insert its output:

"\eo": "$(!!)\e\C-e"

Or if you use bash 4.0 or later, you can enable globstar and press \eg to expand patterns like this:

cd **/filename.txt

You can delete the filename part in both cases with option-delete.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .