118

Simple scenario: I'm looking for a wsdl file lost in the middle of a project.

$ find -name '*.wsdl'
./some/very/very/long/way/to/some/lost/directory/filename.wsdl

Now that I know where it is, I want to do something with this file, say edit it. Instead of copy/pasting the path behind my command, is it possible to use the path returned by find earlier ? Just like it's possible to access the last argument you've typed with !$ or you last command with !!.
I've read that it was possible with $?, but it only returns me an error: 0: command not found

$ echo $?
0: command not found
Anto
  • 2,781

5 Answers5

106

There is no special bash variable for that.

$? contains the exit code of the last command (0 = success, >0 = error-code)

You can use the output of find with the -exec flag, like this:

 find -name  '*.wsdl' -exec emacs {} \;

The {} is replaced with the file name found by find. This would execute the command for every found file. If you want to execute a command with all found files as arguments use a + at teh end like this:

  find -name '*.wsdl' -exec emacs {} +

This would open one emacs instance with all found .wsdl files opened in it.

A more general solution is to store the output in a variable:

result=$(find -name '*.wsdl')
emacs $result

This works with all commands, not just find. Though you might also use xargs:

  find -name '*.wsdl' | xargs emacs {}
geirha
  • 47,279
ahilsend
  • 1,540
66

Here's a quick hack that should do what you want with minimal keystrokes, if you don't mind that the last command is executed twice.

Use backtick, ala:

`!!`

e.g.

$ find . -name HardToFind.txt
some/crazy/path/to/HardToFind.txt
$ vim `!!`

*edit: I see the above linked "possibly duped" question also contains this answer. still relevant directly to this one, so leaving it, but sorry for the dupe.

mit
  • 2,141
Rob
  • 771
45

Run the command in the command substitution:

output=$(find -name '*.wsdl')

The output is now stored in the output variable which you can use as many times as you like, with the following command:

echo "$output"
choroba
  • 10,313
0

`!!` is a great solution, but if you want to be even quicker you could use aliases.

Unfortunately it won't work:

~$ alias test='echo `!!`'
~$ test
zsh: command not found: !!

So instead use `fc -e -` Example aliases I use:

copy output:

alias co='echo `fc -e -` | xclip -in -selection clipboard'

open with vim; if output has many lines, opens all of them in tabs:

alias vo='vim -p `fc -e -`'
0

You can simply do `echo !!` and it will return last executed command and its output, although this executes the command again.

$ pwd 
/home/xoein

and the output will be

$ `echo !!` 
`echo pwd`
/home/xoein
NotTheDr01ds
  • 22,082