🔖

コマンド紹介シリーズ:tldr

に公開

コマンド紹介シリーズ第2回は、tldrというコマンドを紹介します。名前からして何かしらの情報をまとめて表示してくれる系のコマンドと予想できるかと思いますが、実際はどんなコマンドなのかみていきましょう。

tldrとは?

一言で言ってしまうと、

コマンドラインツールのヘルプページで、man pagesをよりシンプルにしたもの

と思ってもらえればと思います。公式GitHubは以下にありますのでぜひ参照ください。

https://github.com/tldr-pages/tldr

そもそもman pagesとは何かという話ですが、コマンドラインツールで使い方がわからない時に利用されるman hogehogeがまさにman pagesそのものです。manを使うとそのコマンドのヘルプページが表示され、オプションの内容などがまとめられています。試しにman lsを実行してみると以下のように表示されました。

LS(1)                                                                            General Commands Manual                                                                           LS(1)

NAME
     ls – list directory contents

SYNOPSIS
     ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]

DESCRIPTION
     For each operand that names a file of a type other than directory, ls displays its name as well as any requested, associated information.  For each operand that names a file of
     type directory, ls displays the names of files contained within that directory, as well as any requested, associated information.

     If no operands are given, the contents of the current directory are displayed.  If more than one operand is given, non-directory operands are displayed first; directory and non-
     directory operands are sorted separately and in lexicographical order.

     The following options are available:

     -@      Display extended attribute keys and sizes in long (-l) output.

     -A      Include directory entries whose names begin with a dot (‘.’) except for . and ...  Automatically set for the super-user unless -I is specified.

     -B      Force printing of non-printable characters (as defined by ctype(3) and current locale settings) in file names as \xxx, where xxx is the numeric value of the character in
             octal.  This option is not defined in IEEE Std 1003.1-2008 (“POSIX.1”).

     -C      Force multi-column output; this is the default when output is to a terminal.

     -D format
             When printing in the long (-l) format, use format to format the date and time output.  The argument format is a string used by strftime(3).  Depending on the choice of
             format string, this may result in a different number of columns in the output.  This option overrides the -T option.  This option is not defined in IEEE Std 1003.1-2008
             (“POSIX.1”).

... 以下省略

manで表示される内容はまさにコマンドラインツールの使い方そのものであり、使い方を調べるには必要な情報が載っています。ただし、上記の実行例を見てもらっても分かる通り、情報量が多すぎて実際にどう使えばいいか分かりづらいことが多いです。そのような場合に、簡単な利用例を示してくれるのがtldrになります。

使ってみる

インストール

今回は以下のコマンドでインストールしました。

brew install tldr

使い方

使い方は簡単で、tldr hogehogeのように利用例を調べたいコマンドを続けて入力するだけになります。例えば先ほどのlsコマンドを実行すると以下のようになります。

> tldr ls

ls

List directory contents.
More information: <https://www.gnu.org/software/coreutils/manual/html_node/ls-invocation.html>.

- List files one per line:
    ls -1

- List all files, including hidden files:
    ls [-a|--all]

- List files with a trailing symbol to indicate file type (directory/, symbolic_link@, executable*, ...):
    ls [-F|--classify]

- List all files in [l]ong format (permissions, ownership, size, and modification date):
    ls [-la|-l --all]

- List files in [l]ong format with size displayed using human-readable units (KiB, MiB, GiB):
    ls [-lh|-l --human-readable]

- List files in [l]ong format, sorted by [S]ize (descending) recursively:
    ls [-lSR|-lS --recursive]

- List files in [l]ong format, sorted by [t]ime the file was modified and in reverse order (oldest first):
    ls [-ltr|-lt --reverse]

- Only list directories:
    ls [-d|--directory] */

先ほどのmanとは異なり、ユースケースとそのコマンドの例を表示してくれます。manと比較して全ての機能を網羅できているわけではありませんが、一般的に利用される頻度の高いユースケースは網羅していると思います。

メンテナンス

tldrはコミュニティによりメンテナンスされています。登録済みのコマンドについて更新したり、新しいコマンドを登録することもできます。コミュニティにより活発にメンテナンスされているので、tldrコマンドは実行されると定期的に辞書の更新が実行されます。実際、この記事を書く時に実行すると辞書の更新がされ、以下のようなログが表示されました。

> tldr ls

Local database is older than two weeks, attempting to update it...
To prevent automatic updates, set the environment variable TLDR_AUTO_UPDATE_DISABLED.
Successfully updated local database

メンテナンスは言語によって分けられており、試しに日本語のlsのページを見に行くと以下のリンクのように日本語で作成されています。

https://github.com/tldr-pages/tldr/blob/main/pages.ja/common/ls.md

まとめ

今回はコマンドラインツールの利用方法について教えてくれるtldrについて紹介しました。このコマンドは日々更新されており、我々もそのメンテナンスに参加できます。もし自分が普段使っていて登録されてないコマンドがあったりメンテナンスに協力したい場合はレポジトリを見に行ってもらえたらと思います。

Discussion