LoginSignup
13
12

More than 5 years have passed since last update.

【よいお年を】役立ちシェルスクリプトコマンドを年の瀬にまとめるメモ

Posted at

2015年も残り数時間になりました。
みなさん、今年もよい一年でしたか?

きっかけ

  • シェルスクリプトの役立つ記事や、シェル芸人の楽しい記事に触れた
  • 最近知って役に立ったコマンドを、2015年のQiita納めとしてまとめておきたい

tldr

TL;DRは、"Too Long; Didn't Read".「長すぎて読んでないよ」の略だそう。
長いmanコマンドを一から読むよりも、
実例とともにコマンドの使い方を示してくれた方が分かりやすい時もあるよね、
ということで、初心者だったり急いでいる人がすぐにコマンドを使えるように、
有志の方々がまとめてくれた「コマンド使用例一覧」とも言えるもの。
Githubからインストールできます。

Node.js client : npm install -g tldr
Python client : pip install tldr
Python client: pip install tldr.py
Go client: go get github.com/pranavraja/tldr or platform binaries
...
C++ client: brew tap tldr-pages/tldr && brew install tldr
...

Webブラウザ上でも使えるようです。

使用例1

「肥大化したログファイルを.gz形式に変換したいけど、tarコマンドのオプションってどうすれば良かったっけ?」

$ tldr tar

Archiving utility Optional compression with gzip / bzip

create an archive from files
tar cf {{target.tar}} {{file1 file2 file3}}

create a gzipped archive
tar czf {{target.tar.gz}} {{file1 file2 file3}}

extract an archive in a target folder
tar xf {{source.tar}} -C {{folder}}

extract a gzipped archive in the current directory
tar xzf {{source.tar.gz}}

extract a bzipped archive in the current directory
tar xjf {{source.tar.bz2}}

create a compressed archive, using archive suffix to determine the compression program
tar caf {{target.tar.xz}} {{file1 file2 file3}}

list the contents of a tar file
tar tvf {{source.tar}}

「czf」か、なるほど。

使用例2

「awkコマンド使いたいけど、区切り文字の指定はどうするんだっけ?」(初心者っぽい質問w)

$ tldr awk

A versatile programming language for working on files

Print the fifth column in a space separated file
awk '{print $5}' {{filename}}

Print the third column in a comma separated file
awk -F ',' '{print $3}' {{filename}}

Sum the values in the first column and print the total
awk '{s+=$1} END {print s}' {{filename}}

Sum the values in the first column and pretty-print the values and then the total
awk '{s+=$1; print $1} END {print "--------"; print s}' {{filename}}

「-Fで指定できるよね、なるほど。」

:(コロン)

シェルスクリプトの役立つ記事からの引用ですが、

: というコマンド(?)を利用すると、何もせずに終了ステータス0(つまり正常終了)を返します。

こういうコマンド(?)があるみたいです。

使用例

「バックアップを取ったので、ログファイルを空にしよう」

# ファイルを初期化する
: > app.log

ちなみに。

echo "" > app.log

のようにしないのは、ログファイルにログを吐いているアプリサーバのプロセスを奪ってしまわないように
(echoで0バイトにすると、正常にログが出力されなくなり、アプリサーバの再起動が必要になる場合がある)。

おわりに

みなさん、よいお年を!

参考

シェルスクリプトの役立つ記事: 初心者向け、「上手い」シェルスクリプトの書き方メモ
シェル芸人の楽しい記事: 【たのしいな】様々なコマンド達を何も考えずにつないで遊ぶ

13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12