web-dev-qa-db-ja.com

Linuxでプログラムを検索するコマンド

マシンで(名前を忘れたときに)検索したいコマンドを見つけるために、.bashrcでエイリアスを定義して、すべての「bin」ディレクトリにアクセスし、おそらく正規表現を使用する簡単なアイデアはありますか?

例えば.

ls /bin /sbin /usr/bin | grep program

すべてのbinディレクトリを検索するために正規表現を使用する方法を知っていますか?

3
user1162226

whereis を使用して、検索している名前に一致するすべてのバイナリを検索できます

> whereis -b ls
ls: /bin/ls

-bオプションはバイナリ専用であり、それがないと、ソースやマニュアルページファイルも見つかります。

> whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
4
Alex

これは外部依存関係なしで動作し(シェルビルトインのみ)、busyboxashでも動作します。

正確に綴る方法を思い出せないこともありますが、厳密に一致するものを見つける必要がある人のために、bashrcに追加する次の関数があります。

find_bin(){
for x in ${PATH//://*${1}* }*${1}*; do
    [ -f "$x" ] && echo $x
done
}

find_bin grepは、$ PATH内のすべてのバイナリを、名前にgrepとともに、パス{grep、egrep、fgrep、bugreport、...}とともに一覧表示します。

これは、次の構文を受け入れることができます。

  • a * z --with az
  • a?z --with a <1文字> z
  • a [0-9] z-with az
  • a [a-z] z --with az
  • a [A-Z] z --with az
  • a * {a、z} * z --with a .... a [az] zと同じ

たまにWord全体にのみ一致させ、grepと入力したときにegrep、fgrep、またはbugreportを報告しないようにしたい場合は、アスタリスクを削除して、コマンドラインで必要な場所にのみ追加し直すことができます。

find_bin(){
for x in ${PATH//://${1} }${1}; do
    [ -f "$x" ] && echo $x
done
}

このため、最初と最後を一致させるために追加の構文が必要です

  • * grep-greで終わるコマンドと一致します(grep、egrep、fgrepは検出されますが、バグレポートは検出されません)
  • grep-正確な文字列grepにのみ一致します
  • grep * -grepで始まるコマンドと一致します
  • 上記のすべてのパターンも適用されます
3
technosaurus

Bash関数typeを使用します。 whereisよりも優れています。これは、bashとエイリアスに組み込まれている関数、およびバイナリ実行可能ファイルを識別するためです。 whereisは、パターンマッチングを行うため、より優れています-typeは、指定したものだけを検索します。

使用 -aパス内の実行可能ファイルのすべてのオカレンスを表示するオプション。

~$ type python
python is /usr/local/bin/python
~$ type -a python
python is /usr/local/bin/python
python is /usr/bin/python
python is /usr/local/bin/python
~$ whereis -b python
python: /usr/bin/python /usr/bin/python2.4 /usr/lib/python2.4 /usr/local/bin/python /usr/local/bin/python2.6 /usr/local/bin/python2.5 /usr/local/bin/python2.7 /usr/local/bin/python2.7-config /usr/local/bin/python2.5-config /usr/local/bin/python2.6-config /usr/local/lib/python2.6 /usr/local/lib/python2.5 /usr/local/lib/python2.7 /usr/include/python2.4

~$ whereis -b time
time: /usr/bin/time /usr/include/time.h
~$ type -a time
time is a Shell keyword
time is /usr/bin/time
3
Doug Harris

whichはまさにそれを行います:

which commandname
1
Eduardo I.

それを.bashrcに入れるには、エイリアスはパラメータを受け取らないので役に立ちませんが、関数は次のことを行います。

pfind () {
  for d in ${PATH//:/ }
  do
    find $d -regex $d/"$1"
  done
}

pfind '.*[ou]b.*h'

find ... -regesはパス全体の正規表現を検索するため、$ dを繰り返す必要があります。

pfindは、program-find(またはPATH find)を表します。

0
user unknown

あなたがmlocateパッケージを持っていると仮定します:

locate programname
0
Armand

bashで、コマンドの最初の1文字または2文字を覚えている場合は、<character><character><TAB><TAB>と入力して、名前が<character><character>で始まるすべてのコマンドのリストを表示します。

Linuxでは、「locate string」は、名前の一部に「string」が含まれるすべてのファイルとディレクトリを一覧表示します。 (最初にupdatedbを使用してDBを構築する必要があります。CentOSでは、cron.dailyジョブがupdatedbを実行します。)これでは、どれが実行可能かはわかりません。 ;これは、より多くの情報を見つけるためのもう1つの方法です。

0
aqn
apropos program/library/related/anything really

正規表現のサポートを含めて、マニュアルページで検索ワードを検索します。 ncmpcppのつづりを思い出せないので、

apropos mpd

ncmpcppを含むすべてのmpdを見つけることができます

0
averagejoey2000

Man -kを使用して、各コマンドのマンページ内を検索できます。

man -k packet


PF_PACKET (7)        - packet interface on device level.
gpgsplit (1)         - Split an OpenPGP message into packets
ip6tables (8)        - IPv6 packet filter administration
iptables (8)         - administration tool for IPv4 packet filtering and NAT
lft (1)              - print the route packets trace to network Host
lft.db (1)           - print the route packets trace to network Host
packet (7)           - packet interface on device level.
pcap-filter (7)      - packet filter syntax
tc-bfifo (8)         - Packet limited First In, First Out queue
tc-pfifo (8)         - Packet limited First In, First Out queue
tcptraceroute (1)    - A traceroute implementation using TCP packets
tcptraceroute.db (8) - print the route packets trace to network Host
tcptraceroute.mt (1) - A traceroute implementation using TCP packets
traceproto (1)       - print the route packets trace to network Host
traceproto.db (1)    - print the route packets trace to network Host
traceroute (1)       - print the route packets trace to network Host
traceroute-nanog (1) - print the route packets trace to network Host
traceroute.db (1)    - print the route packets trace to network Host
traceroute6 (1)      - print the route packets trace to network Host
traceroute6.db (1)   - print the route packets trace to network Host


man -k "packet limited"
tc-bfifo (8)         - Packet limited First In, First Out queue
tc-pfifo (8)         - Packet limited First In, First Out queue

括弧内の数字は、コマンドのマニュアルページのセクションを表します。たとえば、セクション3と5はコマンド用ではありません。もちろん、悲しいことに、一部のコマンドにはマニュアルページがないことを考慮する必要があります。

0
user122729