Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

migrated from Jun 18 '13 at 7:27

This question came from our site for professional and enthusiast programmers.

up vote 3 down vote accepted

How about:

cd $(find -name "filename.txt" -type f -exec dirname {} \; | sed 1q)
share|improve this answer
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 – Jeff Widman Jul 18 '13 at 22:58

Like this:

$ cd `find ~ -name filename.txt -exec dirname {} \;`
share|improve this answer
Stalker!!!! :-) – jaypal singh Jun 18 '13 at 5:48
@JS: heh - well someone's got to answer all these find questions... ;-) – Paul R Jun 18 '13 at 5:58
Yes Sir!! Now we just got to wait for mods to move it to SuperUser. Sigh! – jaypal singh Jun 18 '13 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) – Jeff Widman Jul 2 '13 at 18:22
What happens if you just run $ find ~ -name filename.txt -exec dirname {} \; ? – Paul R Jul 2 '13 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.

share|improve this answer

You must log in to answer this question.

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