Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

migrated from stackoverflow.com Jun 18 '13 at 7:27

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

3 Answers

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
add comment

Like this:

$ cd `find ~ -name filename.txt -exec dirname {} \;`
share|improve this answer
    
Stalker!!!! :-) –  JS웃 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! –  JS웃 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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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