当我使用时find,它经常会发现多个结果,例如
find -name pom.xml
./projectA/pom.xml
./projectB/pom.xml
./projectC/pom.xml我通常只想选择一个特定的结果(例如edit ./projectB/pom.xml)。有没有办法枚举find输出并选择要传递到另一个应用程序的文件?喜欢:
find <print line nums?> -name pom.xml
1 ./projectA/pom.xml
2 ./projectB/pom.xml
3 ./projectC/pom.xml
!! | <get 2nd entry> | xargs myEditor?
[编辑]通过提到的一些解决方案,我遇到了一些重大错误。因此,我想说明重现步骤:
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>[编辑]解决方案1 到目前为止,如果我将'nl'(足够的输出),头和尾结合起来使用函数$(!!),头和尾似乎可以正常工作。
即:
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.[编辑]解决方案2 使用“选择”似乎也很好。例如:
  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满足您的要求?(与您的专线:!! | head -2 | xargs myEditor)