Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This seems like an obvious question to me but I can't find the answer... Presume I want to find a certain txt-file in my project and in Terminal I type:

find -name test.txt

As result I might get something like:

./directory1/directory2/directory3/directory4/test.txt

How do I jump to directory4 without having to type the complete path?

share|improve this question
add comment

2 Answers 2

I'd use bash's history support to do this. After any command, you can reference it again with !!:

[jk@pecola tmp]$ find . -name test.text
./directory1/directory2/directory3/test.text

To perform a ls -l on the output of the last command:

[jk@pecola tmp]$ ls -l $(!!)
ls -l $(find . -name test.text)
-rw-rw-r-- 1 jk jk 0 Jan 23 20:49 ./directory1/directory2/directory3/directory4/test.text

In this example, we use the $(...) syntax to use the output of the command as an argument to another command.

Because !! references the last command run, we now need to be more specific if we want to re-run the find command, since we've run ls since. We can use the !<match> syntax to re-run the last command that started with <match>

[jk@pecola tmp]$ dirname $(!find)
dirname $(find . -name test.text)
./directory1/directory2/directory3

[jk@pecola tmp]$ cd $(dirname $(!find))
cd $(dirname $(find . -name test.text))

[jk@pecola directory4]$ ls
test.text

I also find this handy for re-doing commands which you need sudo for:

[jk@pecola tmp]$ adduser newuser
adduser: Only root may add a user or group to the system.

[jk@pecola tmp]$ sudo !!
sudo adduser newuser
share|improve this answer
    
Thanks, I didn't know about !! and $(...). They’re surely handy! Although I'm a little surprised that's such an "easy task" is quite a hassle, I think this solution will do! –  Ludder Jan 23 '13 at 19:36
add comment

If the terminal session is within a GUI you can select text with your mouse (left button) and paste the whole text at the position of the text cursor by clicking the middle mouse button.

share|improve this answer
    
Thanks, I didn't know about the middle click (I use shift+insert instead). I prefer a keyboard only solution though. –  Ludder Jan 23 '13 at 19:31
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.