シェルコマンド「man」の使い方
自分がターミナルに触れる機会が増えるにつき、man
コマンドが便利だと思いましたのでその紹介をしたいと思います。
使い方がうる覚えなコマンドをターミナル上でぱぱっと検索したい時に使っています。
なお以降の例は次の環境で試したものです
$ cat /etc/os-release NAME="Ubuntu" VERSION="20.04.2 LTS (Focal Fossa)" ... $ uname -r 5.4.0-1038-raspi
manコマンドとは
「指定したコマンドのマニュアルを表示させる」コマンドであり、マニュアルの略からmanとなっています。
その基本的な使い方は
# 調べ方 $ man コマンド # iでカーソル下、kでカーソル上 # qで終了
です
とういことは、manコマンドの使い方を調べるにはmanのmanをしてみたら良いであろうと考えられます
$ man man MAN(1) Manual pager utils NAME man - an interface to the system reference manuals SYNOPSIS man [man options] [[section] page ...] ... man -k [apropos options] regexp ... man -K [man options] [section] term ... man -f [whatis options] page ... man -l [man options] file ... man -w|-W [man options] page ... DESCRIPTION man is the system's manual pager. .... The table below shows the section numbers of the manual followed by the types of pages they contain. 1 Executable programs or shell commands 2 System calls (functions provided by the kernel) 3 Library calls (functions within program libraries) 4 Special files (usually found in /dev) 5 File formats and conventions, e.g. /etc/passwd 6 Games 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) 8 System administration commands (usually only for root) 9 Kernel routines [Non standard] A manual page consists of several sections. ...
基本的な内容は
- 名前の簡単な説明
- 簡単な使い方
- オプション
などが表示されます。その正確な内容はbashやzshなどのバージョン(カーネルのバージョン)に依存しています
man(1)の(1)とは?
manコマンドや、そのほかの色々な説明を読んでいると各種コマンドの右側に(数字)
となっているところがあると思います。例えばman(1)、ls(1)といった形です。
これは上のman man
の結果の中の、DESCRIPTION
に記述されており、マニュアルページのセクション番号に対応するようです。
よく出てくるのは1,重要なのは1,2,3,7ぐらいかと思われます。
必要なオプションを見つけ出す
基本的にはman + grep
の組み合わせで検索を行うことが多いです。
以下は、「並び替えをsortで行えることは覚えていたけど、数字順で並び替えるにはどのオプションを使えば良いかわからなかった」時に使ったコマンドです。
$ man sort | grep num ... -n, --numeric-sort compare according to string numerical value ...
よって、-n
オプションをつけることにより数字でソートできることがわかります。
なお、grep
コマンドのオプションで「マッチした文字列を含む行の前後N列を表示させる(-C3など)」、とかもありよく使っています。
1回は読んでみるべきコマンド
man
,bash(zsh)
,type
コマンドや、他にもよく使うものなんかは1回調べてみると良いと思います
おわりに
せっかくターミナル上で作業しているのに、わからないコマンドが出てくるたびにブラウザに検索に戻るなんて時間の無駄ですよね(?)
みなさんがman
コマンドで効率的な検索を行ない、快適な生活が送れるようになったら幸いです