When I use find
, it often finds multiple results like
find -name pom.xml
./projectA/pom.xml
./projectB/pom.xml
./projectC/pom.xml
I often want to select only a specific result, (e.g edit ./projectB/pom.xml
). Is there a way to enumerate find
output and select a file to pass into another application? like:
find <print line nums?> -name pom.xml
1 ./projectA/pom.xml
2 ./projectB/pom.xml
3 ./projectC/pom.xml
!! | <get 2nd entry> | xargs myEditor
?
[Edit] I've bumped into some perculiar bugs with some of the solutions mentioned. So I'd like to explain steps to reproduce:
git clone http://git.eclipse.org/gitroot/platform/eclipse.platform.swt.git
cd eclipse.platform.swt.git
<now try looking for 'pom.xml' and 'feature.xml' files>
[Edit] Solution 1 So far a combination of 'nl' (enumirate output), head & tail seems to work if I combine them into functions and use $(!!).
i.e:
find -name pom.xml | nl #look for files, enumirate output.
#I then define a function called "nls"
nls () {
head -n $1 | tail -n 1
}
# I then type: (suppose I want to select item #2)
<my command> $(!!s 2)
# I press enter, it expands like: (suppose my command is vim)
vim $(find -name pom.xml |nls 2)
# bang, file #2 opens in vim and Bob's your uncle.
[Edit] Solution 2 Using "select" seems to work quite well as well. e.x:
findexec () {
# Usage: findexec <cmd> <name/pattern>
# ex: findexec vim pom.xml
IFS=$'\n';
select file in $(find -type f -name "$2"); do
#$EDITOR "$file"
"$1" "$file"
break
done;
unset IFS
}
Your find command | head -TheNumberYouWant
fulfilling your requirements? (With your line:!! | head -2 | xargs myEditor
) – ADDB Jul 13 '17 at 15:29