3

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

How about:

cd $(find -name "filename.txt" -type f -exec dirname {} \; | sed 1q)
1
  • 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
0

Like this:

$ cd `find ~ -name filename.txt -exec dirname {} \;`
5
  • 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
  • 1
    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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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