Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Query

I use Bash. When I'm looking for files, often I'll do the following:

find -name stackexchange.hs

And often the results will look like:

/youre/the/man/now/dog/stackexchange.hs
/you/are/no/longer/the/dog/dog/stackexchange.hs
/this/is/the/file/i/want/stackexchange.hs

Then I'll want to do one of the following:

  • Option 1: Open the last item in the list of results in vim.
  • Option 2: Open the Nth item in the list of results in vim.

Currently, I cut-and-paste with the mouse. Which brings me to my question:

  1. Is there an easy, one-liner to accomplish options 1&2? Note that this is occurring after the find command.
  2. Is there a way to capture N-lines from stdout in some kind of bash vector/array?

Ideal usage

$ find -name am_i_really_all_alone.txt
./borges/library/you_are_not_alone.txt
./borges/library/am_i_really_all_alone.txt
$ vim (N)

(syntax and semantics may differ, but you get the point)

Similaria

There seem to be several similar questions. Here are my perceived differences (I am open to enlightenment):

Thank you for your help! Having used *nix/BSD when I was a teenager in the 90s and getting scared away by calling my burnout, acid-head neighbour to help me install drivers for my plug-and-play sound card, I'm relieved to discuss command-line minutiae with (perceivably) less frightening individuals. It feels good to be back.

share|improve this question
    
I think, that if you know it before that you want to open the last result, you could use something like vim $(command |tail -n1). –  varesa Jan 3 '13 at 21:26
add comment

3 Answers

up vote 5 down vote accepted

Here is a potential solution to your issue that should be reasonably (but not perfectly) safe in the presence of funky file names (doesn't handle filenames with linefeeds in them - probably fixable, but there might be other problems lurking).

Two functions, the first one runs find with the parameters you pass it, saves the output in an array, and displays them. The second is just a helper to access that array.

myfind() {
  IFS=$'\n' __last_find_result=($(find "$@"));
  printf "%s\n" "${__last_find_result[@]}";
}
myget() {
  echo "${__last_find_result[$1]}";
}

Use case:

$ myfind . -name "c*"
./a b/c d
./.git/config
./.git/hooks/commit-msg.sample
$ vim "$(myget 0)"
# This opens the "./a b/c d" file.
$ vim "$(myget 2)"
# This opens ".git/hooks/commit-msg.sample"

Quotes not required around the $(myget index) if you don't have whitespace or other troublesome characters in your filenames.
Pushes the whole output of find to your environment, which might be limited. (Using a temporary file rather than that array would solve that, but has other issues - notably concurrent use from multiple shells.)

share|improve this answer
    
Ah, this is fantastic. Thanks!! –  weirdcanada Dec 28 '12 at 18:00
    
I can't upvote you because I don't have enough of a reputation, so here is a, uh, verbal one: "upvote" –  weirdcanada Dec 28 '12 at 18:00
add comment

I've got this in my .screenrc:

bind -c pasteline 1 eval copy 'stuff "-Y"' 'paste .'
bind -c pasteline 2 eval copy 'stuff "2-Y"' 'paste .'
bind -c pasteline 3 eval copy 'stuff "3-Y"' 'paste .'
bind -c pasteline 4 eval copy 'stuff "4-Y"' 'paste .'
bind -c pasteline 5 eval copy 'stuff "5-Y"' 'paste .'
bind -c pasteline 6 eval copy 'stuff "6-Y"' 'paste .'
bind -c pasteline 7 eval copy 'stuff "7-Y"' 'paste .'
bind -c pasteline 8 eval copy 'stuff "8-Y"' 'paste .'
bind -c pasteline 9 eval copy 'stuff "9-Y"' 'paste .'
bindkey ¬ command -c pasteline

Basically, in screen, ¬1, pastes the line above the cursor, ¬2, pastes the second line above the cursor... and so on. You may want to add more for lines 10 and above, but I find that after about 7 already, I'd rather use the mouse or screen's copy mode than counting the number of lines to get the one I want.

share|improve this answer
    
Hah. Nice! Thanks. –  weirdcanada Dec 28 '12 at 20:42
add comment

another solution is: you can write an interactive script that will automatically ask for your choice. here is the code for interactive script:

#!/bin/bash

echo "enter your choice : z for last argument or a number for that file"
read choice

case "$choice" in
z) eval vim \$$#;;
*)eval  vim \$$choice;;
esac

save this script with any name say "autofind" and invoke the script with your "find command" as argument here is the code to invoke script:

./autofind `your find command`

But before using the script check your "find command" whether it is giving result or not.If it is showing some result then only use the script

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.