マシンで(名前を忘れたときに)検索したいコマンドを見つけるために、.bashrcでエイリアスを定義して、すべての「bin」ディレクトリにアクセスし、おそらく正規表現を使用する簡単なアイデアはありますか?
例えば.
ls /bin /sbin /usr/bin | grep program
すべてのbinディレクトリを検索するために正規表現を使用する方法を知っていますか?
whereis
を使用して、検索している名前に一致するすべてのバイナリを検索できます
> whereis -b ls
ls: /bin/ls
-b
オプションはバイナリ専用であり、それがないと、ソースやマニュアルページファイルも見つかります。
> whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
これは外部依存関係なしで動作し(シェルビルトインのみ)、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、...}とともに一覧表示します。
これは、次の構文を受け入れることができます。
たまにWord全体にのみ一致させ、grepと入力したときにegrep、fgrep、またはbugreportを報告しないようにしたい場合は、アスタリスクを削除して、コマンドラインで必要な場所にのみ追加し直すことができます。
find_bin(){
for x in ${PATH//://${1} }${1}; do
[ -f "$x" ] && echo $x
done
}
このため、最初と最後を一致させるために追加の構文が必要です
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
which
はまさにそれを行います:
which commandname
それを.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)を表します。
あなたがmlocateパッケージを持っていると仮定します:
locate programname
bash
で、コマンドの最初の1文字または2文字を覚えている場合は、<character><character><TAB><TAB>
と入力して、名前が<character><character>
で始まるすべてのコマンドのリストを表示します。
Linuxでは、「locate string」は、名前の一部に「string」が含まれるすべてのファイルとディレクトリを一覧表示します。 (最初にupdatedb
を使用してDBを構築する必要があります。CentOSでは、cron.daily
ジョブがupdatedb
を実行します。)これでは、どれが実行可能かはわかりません。 ;これは、より多くの情報を見つけるためのもう1つの方法です。
apropos program/library/related/anything really
正規表現のサポートを含めて、マニュアルページで検索ワードを検索します。 ncmpcppのつづりを思い出せないので、
apropos mpd
ncmpcppを含むすべてのmpdを見つけることができます
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はコマンド用ではありません。もちろん、悲しいことに、一部のコマンドにはマニュアルページがないことを考慮する必要があります。