MyDocs

# コマンドいろいろ

Developer Roadmaps にあった DevOps コマンドをまとめておく。

文字列操作

awk

列を抽出する。

$ echo 1 2 3 4|awk '{print $1}'
1
$ echo 1 2 3 4|awk '{print $2}'
2

$0 だとすべて。

$ echo 1 2 3 4|awk '{print $0}'
1 2 3 4

区切り文字の指定もできる。

$ echo 1:2:3:4|awk -F'[:]' '{print $1}'
1

sed

文字列置換。

$ cat test.txt
1 2 3 4 5
$ sed 's/3/333/g' test.txt
1 2 333 4 5

grep

文字列検索。

$ cat test.txt
1
2
3
4
5

$ cat test.txt | grep 3
3

-v は一致しないものを検索する。

sort

ソートする。

$ cat test.txt
1
12
3
23
55
22

$ sort test.txt
1
12
22
23
3
55

-n は数値として並べる。

$ sort -n test.txt
1
3
12
22
23
55

-r は逆順として並べる。

$ sort -n -r test.txt
55
23
22
12
3
1

uniq

重複した行を削除。

$ cat test.txt
0123456789
abcdefghij
abcdefghij
ABCDEFGHIJ
9876543210

$ uniq test.txt
0123456789
abcdefghij
ABCDEFGHIJ
9876543210

-i は大文字小文字を無視する。

$ uniq -i test.txt
0123456789
abcdefghij
9876543210

cat

ファイル出力。

$ cat test.txt
0123456789
abcdefghij
abcdefghij
ABCDEFGHIJ
9876543210

cut

awk で良さそう。

$ cat test.txt
1:2:3
4:5:6
7:8:9

$ cut -f 2 -d ":" test.txt
2
5
8

awk でやる場合。

$ cat test.txt |awk -F ':' '{print $2}'
2
5
8

echo

文字列を表示する。

$ echo 'aaa'
aaa

fmt

整形。

$ cat test.txt
1 2 3
4 5 6
7 8 9

$ fmt test.txt
1 2 3 4 5 6 7 8 9

tr

文字の変換、削除する。

$ cat test.txt
12345
abcde
ABCDE

$ cat test.txt | tr abc XYZ
12345
XYZde
ABCDE

$ cat test.txt | tr -d 123
45
abcde
ABCDE

nl

行数表示できる。

$ nl test.txt
     1 12345
     2 abcde
     3 ABCDE

cat でやるなら -b を使う。

$ cat -b test.txt
     1 12345
     2 abcde
     3 ABCDE

egrep

grep -E と同じ意味。

拡張正規表現ってのが使える.普通の正規表現と何が違うかはよくわからない。

$ cat test.txt
12345
bar
foo

$ grep -E '^(bar|foo)' test.txt
bar
foo

fgrep

grep -F と同じ意味。

正規表現文字をそのまま検索できる。

$ cat test.txt
12345
bar.foo

$ grep -F 'bar.foo' test.txt
bar.foo

wc

行数、単語数を数える。

$ cat test.txt
Hello World!

-m は文字数を数える。

$ wc -m test.txt
      13 test.txt

-w は単語数を数える。

$ wc -w test.txt
       2 test.txt

プロセス管理

ps

全プロセスを表示する。

$ ps aux

top

プロセス、CPU、メモリを表示。

デフォルトだと CPU 使用率順にソート。

-a でメモリ使用順にソート。

htop

top より見やすい。

Shift + P で CPU 使用率、Shift + M でメモリ使用率順。

atop

top コマンドの代替。

htop のほうが良さそう。

lsof

特定のポート番号を待ち受けているか、もしくはファイルを読み込んでいるかを調べるとき。

80 番ポートで実行中のプロセスを調べる。

$ lsof -i:80 -P

特定のファイルを開いているプロセスを調べる。

$ lsof <FILE>

コマンドを指定する。

$ lsof -c <COMMAND>

ネットワーク

nmap

サーバの開いているポートを調べる。

$ nmap <SERVER>

tcpdump

パケットをキャプチャする。

ホストを指定してキャプチャする。

$ tcpdump host <HOST>

port を指定する。

$ tcpdump host <HOST> and port 22
$ tcpdump host <HOST> or port 22

ping

パケットを送信して通信を確認。

traceroute

経路と応答を調べる。

<TARGET> までの経路と応答を調べる。

$ traceroute <TARGET>

mtr

経路と応答を調べる. traceroute とだいたい同じ。

dig

ドメイン名に対する IP を調べる。

$ dig www.google.co.jp

netstat

ネットワーク関連の情報を表示。

-r でルーティングテーブルを表示する。

システムパフォーマンス

nmon

OS のベンチマークをとったりできる。

iostat

CPU 使用率と I/O デバイスの使用状況を調べる。

sar

メモリ、プロセス、スワップ、 CPU 等の統計情報を調べる。

vmstat

仮想メモリや CPU 、ディスク I/O の統計情報を調べる。

その他

strace

実行コマンドをトレースする。

$ strace ls

uname

カーネル名を表示。

-a で全ての情報を表示する。

$ uname -a

df

ディスクの空き容量を調べる。

-h で読みやすく表示。

$ df -h

history

コマンドの履歴を表示。

-d で指定した番号の履歴を削除する。

$ history -d 10