##まとめに至った経緯
- 単純にGUIに頼りすぎていちいちトラックパッドでパタパタやんのめんどい...
- 体系的にlinuxを学習してみて効果を知りたかった
- つか環境構築で爆死したり、何回もvagrant box潰したり、最近docker触ったりとos周りの知識が必要不可欠だと感じたから
- 知ってて当たり前感
###part 2 http://qiita.com/kindai_dai/items/0adf9dbff0ff50bcfad8
第1章
-
コマンドライン操作
file: /usr/share/man, .bash_history
command: bash, echo, env, export, pwd, set, unset, man, history -
ファイル操作
command: cp, find, mkdir, mv, ls, rm, rmdir, touch, tar, cpio, dd, file, gzip, gunzip, vzip2 -
ストリーム、パイプ、リダイレクション
command: tee, xargs -
フィルタによる処理
command: cat, cut, expand, fmt, head, less, od, join, nl, pr, sed, sort, split, tail, tr, unexpand, uniq, wc
コマンドライン操作
###set
現在のシェルで定義されているシェル変数の一覧を表示
※シェル変数:設定されたシェルだけが使用する変数
主なシェル変数
PWD, PATH, HOME, PS1, PS2, HISTFILE, LANG
###env (or printenv)
環境変数を表示
※環境変数:設定されたシェルとシェルで起動したプログラムが使用する変数
###export (or declare -x)
シェル変数を環境設定に設定し、bashから起動するアプリケーションが参照できるようにする
環境変数は、子プロセスとして起動したアプリケーションに引き継がれるので、アプリケーションから利用できる
###declare
シェル内の変数と関数を全て表示する
| option | explane |
|---|---|
| -x | 環境変数としてエクスポートする |
| -f | 定義されている関数を表示する |
| -f シェル変数名 | 関数を格納する変数を定義する |
| -i | 整数を格納する変数を表示する |
| -i シェル変数名=値 | 整数を格納する変数とその値を定義する |
| -p | 全ての変数とその属性を表示する |
###PS1='[\u@\h \w]$ '
[vagrant@localhost ~]$
PS1 -> コマンドプロンプト
PS2 -> 2次プロンプト(コマンドプロンプトが終了せず、継続行であること)
| option | explane |
|---|---|
| \s | シェルの名前 |
| \v | bashのバージョン |
| \u | ユーザ名 |
| \h | ホスト名の内の最初の.まで |
| \w | 現在の作業ディレクトリ |
###cd [dir]
ファイルシステム内の移動
| option | explane |
|---|---|
| / | ルートディレクトリ ファイルシステムの頂点 |
| ~ | ホームディレクトリ |
| . | カレントディレクトリ |
| .. | 親ディレクトリ |
###date [option] [日付] [+表示形式]
現在の日時を設定、変更
[vagrant@localhost ~]$ date
Thu 6 Jul 15:10:51 BST 2017
[vagrant@localhost ~]$ date +%c
Thu 06 Jul 2017 15:11:06 BST
[vagrant@localhost ~]$ date -u
Thu 6 Jul 14:11:51 UTC 2017
| option | explane | example |
|---|---|---|
| %A | 曜日 | Sunday |
| %B | 月 | January |
| %H | 時 | 23 |
| %M | 分 | 10 |
| %c | 日時 | Thu 06 Jul 2017 15:11:06 BST |
| %x | 日付 | 6/07/17 |
###man [option] [章] command or keyword
引数で指定したコマンドのオンラインマニュアルを表示
ディストリビューションに含まれるパッケージをインストールした場合
-> /usr/share/man 配下の該当するセクションのサブディレクトリに置かれる
[vagrant@localhost ~]$ ls /usr/share/man
cs de fr id ja man0p man1p man2 man3 man3x man4x man5x man6x man7x man8x man9x pl ru sv zh_CN
da es hu it ko man1 man1x man2x man3p man4 man5 man6 man7 man8 man9 mann pt_BR sk tr zh_TW
ディストリビューションに含まれないソフトウェアをダウンロードしてインストールした場合
-> /usr/local/share/man にマニュアルが一般的には配置される
[vagrant@localhost ~]$ ls /usr/local/share/man
man1 man1x man2 man2x man3 man3x man4 man4x man5 man5x man6 man6x man7 man7x man8 man8x man9 man9x mann
###history [option] [履歴数]
シェルから実行したコマンドの実行履歴を表示する
| option | explane |
|---|---|
| -c | 実行履歴の消去 |
| -d [履歴番号] | 指定した履歴番号を履歴から削除 |
[vagrant@localhost ~]$ history 3
59 ls /usr/local/share/man
60 ls /usr/local/share/man/man1
61 history 3
コマンド実行履歴を保存するファイル名を指定するシェル変数: HISTFILE
default => ~/.bash_history
[vagrant@localhost ~]$ echo $HISTFILE
/home/vagrant/.bash_history
###su [option] [変更先ユーザ名]
一時的に他のユーザの権限に切り替える
環境変数に設定した値などはsu実行前に利用していたユーザ環境の値が継承される
| option | explane |
|---|---|
| - (ハイフン) | ユーザのログイン環境になる |
###sudo
管理者から許可されている管理者用コマンドを実行する
##ファイル操作
###mkdir [option] ディレクトリ名..n
ディレクトリを作成する。複数指定可
| option | explane |
|---|---|
| -m[アクセス権] | 明示的にアクセス権を指定してディレクトリを作成する |
| -p | 中間ディレクトリを作成する |
###touch ファイル名
空のファイルを新規に作成する。
###mv [option] 移動元ファイル名..n ディレクトリ名
###mv [option] 移動元ディレクトリ名..n ディレクトリ名
ファイルやディレクトリの移動
| option | explane |
|---|---|
| -i | 移動先に同名のファイルが存在する場合、上書きするか確認する |
| -f | 移動先に同名のファイルが存在しても、強制的に上書きする |
ディレクトリの移動だけではなく、中身全ても移動する場合は「*」をつける
mv -f /tmp/A/* /usr/local
ワイルドカード一覧
| option | explane |
|---|---|
| ? | 任意の一文字 |
| * | 0文字以上の文字列 |
| [...] | []内に含まれる任意の一文字 |
| [!...] | []内に含まれない任意の一文字 |
| $ | 変数の置換や、コマンドの置換を行う |
###cp [option] コピー元のファイル名 コピー先ファイル名
###cp [option] コピー元のファイル名...n コピー先ディレクトリ名
| option | explane |
|---|---|
| -i | コピー先に同名ファイルが存在する場合、上書きするか確認する |
| -f | コピー先に同名ファイルが存在しても、強制的に上書きする |
| -p | コピー元の所有者、タイムスタンプ、アクセス権などの情報を保持したままコピーする |
| -R(-r) | コピー元のディレクトリ階層をそのままコピーする |
###lsattr ファイル名
ext2ファイルシステム上にあるファイルの属性を表示する
[vagrant@localhost ~]$ lsattr fuga.txt
----i---------e- fuga.txt
###ls [option] [ディレクトリ名]...n
###ls [option] [ファイル名]...n
ディレクトリやファイルの情報を表示
| option | explane |
|---|---|
| -F | ファイルタイプを表す記号の表示 /はディレクトリ、*は実行可能ファイル、@はシンボリックリンク |
| -a | 隠しファイル |
| -l | 詳細な情報を表示 |
| -d | ディレクトリ自身の情報を表示 |
###rm [option] ファイル名...n
ファイル削除
| option | explane |
|---|---|
| -i | ファイルを削除する前にユーザに確認する |
| -f | ユーザへの確認なしに削除する |
| -R(-r) | 指定されたディレクトリ内にファイル、ディレクトリが存在していても全て削除する |
###tar [option] ファイル名orディレクトリ名...n
オプションによって指定したファイルをアーカイブしたり、アーカイブファイルからファイルの情報を表示したり、取り出したりする。
| option | explane |
|---|---|
| -c | アーカイブファイルを作成する |
| -t | アーカイブファイルの内容を表示する |
| -x | アーカイブファイルを展開する |
| -f | アーカイブファイルを指定する |
| -v | 詳細情報を表示する |
| -j | bzip2を経由してアーカイブをフィルタする |
| -z | gzipを経由してアーカイブをフィルタする |
###圧縮:gzip [option] ファイル名
###解答:gunzip [option] ファイル名
###圧縮:bzip2 [option] ファイル名
###解答:bunzip2 [option] ファイル名
###cpio
複数のファイルやディレクトリを一つにまとめる
###dd
ファイルシステムやディスク単位でバックアップ
dd if=image.iso of=/dev/sdb
if(input file)
of(output file)
##フィルタによる処理
###more ファイル名
[Space]キーを押すと次ページ表示。最後のページまで閲覧すると同時に終了する
###less ファイル名
一画面に収まらないファイルを表示する
###cat [option] [ファイル名]...n
ファイルの内容を標準出力する
-n オプションで行番号表示(空行含む)
###nl [option] ファイル名
ファイルの内容に行番号をつけて表示
空行を除いた行に行番号を表示
###find [option] ファイル名|ディレクトリ名
| option | explane |
|---|---|
| -i | MIMEタイプで表示する |
[vagrant@localhost ~]$ file fuga.txt
fuga.txt: ASCII text
[vagrant@localhost ~]$ file -i fuga.txt
fuga.txt: text/plain; charset=us-ascii
###リダイレクト
コマンドの後1>&2
標準出力を標準エラー出力にリダイレクトする
#####ファイル記述子
0 -> 標準入力
1 -> 標準出力
2 -> 標準エラー出力
これらはプロセスが生成された時に用意される
[vagrant@localhost ~]$ ls fuga.txt test.txt
ls: cannot access test.txt: No such file or directory ->標準エラー
fuga.txt -> 標準出力
標準出力はディスプレイに、標準エラー出力はファイルへ出力するように切り替えたい場合にリダイレクトを使用する
[vagrant@localhost ~]$ ls fuga.txt test.txt 2> error.txt
fuga.txt
[vagrant@localhost ~]$ ls
error.txt fuga.txt
[vagrant@localhost ~]$ cat error.txt
ls: cannot access test.txt: No such file or directory
###標準出力、標準エラー出力のリダイレクト例
-
カレントディレクトリのファイルリストをfile1に格納する
$ ls > file1 -
1>を使用してファイルリストをfile2に格納する(1.と同意)
$ ls 1> file2 -
file1に、/binのファイルリストを追記して保存する
$ ls /bin >> file1 -
lsコマンドを実行してエラーが出力された場合のみfile3ファイルに格納する
$ ls not_exsit_file exist_file 2> file3 -
標準出力、標準エラー出力の両方をbothファイルに格納する
$ ls not_exsit_file exist_file &> both -
コマンド1を実行した結果の標準出力、標準エラー出力の両方をbothファイルに格納する
$ コマンド1 &> both
###標準入力のリダイレクト例
-
file1の内容を標準入力からコマンド1に取り込む
$ コマンド1 < file1 -
file1の内容を標準入力からコマンド1に取り込み、コマンド1の標準出力をコマンド2の標準入力に渡す
$ コマンド1 < file1 | コマンド2
パイプを使用することで、コマンドの処理結果(標準出力)を次のコマンドの標準入力に渡してさらにデータ加工することができる。
###/dev/null
このファイルに書き込まれたものは全て消去される。
このファイルを読み込むと、常にEOF(end of file)が返る
$ コマンド > /dev/null 2>&1
標準出力先を/dev/nullにする
標準エラーの出力先は、現在の標準出力先(/dev/null)とする
###tac ファイル名...n
ファイルの内容を最後の行から表示する。
[vagrant@localhost ~]$ cat file1.txt
aaa
bbb
ccc
[vagrant@localhost ~]$ cat file2.txt
ddd
eee
fff
[vagrant@localhost ~]$ tac file1.txt file2.txt
ccc
bbb
aaa
fff
eee
ddd
ストリーム、パイプ、リダイレクション
###tee [option] ファイル名
標準入力から読み込んだデータを標準出力とファイルの両方に出力する
| option | explane |
|---|---|
| -a | ファイルに上書きせずに、追記する |
[vagrant@localhost ~]$ nl /etc/passwd | tee myfile.txt | head -3
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
[vagrant@localhost ~]$ ls
error.txt file1.txt file2.txt fuga.txt myfile.txt
[vagrant@localhost ~]$ cat myfile.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
...
###コマンドの連続実行「;」「&&」「||」
- コマンド1, コマンド2を順々に実行する
$ コマンド1 ; コマンド2
コマンド1の実行ステータスに関わらずコマンド2を実行する
- コマンド1が実行できれば、次のコマンド2を実行する
$ コマンド1 && コマンド2
コマンド1の実行ステータスが0ならば、コマンド2を実行する
- コマンド1が実行できなければ、コマンド2を実行する
$ コマンド1 || コマンド2
[vagrant@localhost ~]$ ls file1.txt
file1.txt
[vagrant@localhost ~]$ echo $?
0
[vagrant@localhost ~]$ ls test
ls: cannot access test: No such file or directory
[vagrant@localhost ~]$ echo $?
2
###grep [option] 検索する文字列パターン [ファイル名]...n
テキストデータ内で文字列検索を行う
| option | explane |
|---|---|
| -v | パターンに一致しない行を検索する |
| -n | 行番号を表示する |
| -l | パターンと一致するファイル名を表示 |
| -i | 大文字と小文字を区別しないで検索を行う |
[vagrant@localhost ~]$ cat file1.txt
aaa
bbb
ccc
[vagrant@localhost ~]$ cat file1.txt | grep -i aaa
aaa
[vagrant@localhost ~]$ grep -n bbb file1.txt
2:bbb
[vagrant@localhost ~]$ grep -v 'b' file1.txt
aaa
ccc
[vagrant@localhost ~]$ ps -ef | grep chrome
vagrant 3448 3391 0 02:05 pts/0 00:00:00 grep --color=auto chrome
###head [option] [ファイル名]...n
テキストファイルの先頭部分を表示
デフォルトは10行
###tail [option] [ファイル名]...n
テキストファイルの末尾部分を表示
デフォルトは10行
| option | explane |
|---|---|
| -n | 行数を数字で指定 |
| -f | 常にファイルの最終部分を読み続ける |
###tr [option] [文字群1 [文字群2]]
標準入力(キーボード)から入力した文字を指定したフォーマットに変換して、標準出力(画面)に表示
| option | explane |
|---|---|
| -d | 文字群1で合致した文字を削除する |
| -s | 文字群1で合致した文字の繰り返しを1文字に置き換える |
[vagrant@localhost ~]$ tr 'a-z' 'A-Z'
hello ->キーボードからの入力
HELLO ->
[vagrant@localhost ~]$ tr -d 'my'
My name is taro
M nae is taro
文字群の指定にはあらかじめ定義されている文字クラスを利用することも可能
| option | explane |
|---|---|
| [:alnum:] | 英文字と数字 |
| [:alpha:] | 英文字 |
| [:digit:] | 数字 |
| [:lower:] | 英小文字 |
| [:space:] | 水平及び垂直方向の空白 |
| [:upper:] | 英大文字 |
###改行コード
| os | 改行コード | エスケープ文字での表示 |
|---|---|---|
| Linux | LF | \n |
| Windows | CR+LF | \r\n |
###sed [option] [編集コマンド] [ファイル名]
単語単位の変換や削除
入力ストリーム(ファイルorパイプラインからの入力)に対してテキスト変換を行うために使われる
| option | explane |
|---|---|
| -i | 編集結果を直接ファイルに書き込む |
| command | explane |
|---|---|
| s/パターン/置換文字列/ | 各行を対象に、最初にパターンに合致する文字列を置換文字列に変換 |
| s/パターン/置換文字列/g | ファイル内全体を対象に、パターンに合致する文字いれつを置換文字列に変換 |
| d | パターンに合致する行を削除 |
| p | パターンに合致する行を表示 |
[vagrant@localhost ~]$ cat file1.txt
aaa
bbb
ccc
[vagrant@localhost ~]$ sed 's/aaa/nnn/' file1.txt
nnn
bbb
ccc
- file5の1行目を削除
$ sed '1d' file5
- file5の2行目から5行目を削除
$ sed '2,5d' file5
- file5の空白行を削除
$ sed '/^$/d' file5
- file5の行末にtestを追加
$ sed 's/ $/ test/' file5
- file5のuser01が含まれる行だけ表示する
$ sed -n '/user01/p' file5
###cut [option] ファイル名
ファイル内の行中の特定部分のみ取り出すにはcutコマンドを使用する
| option | explane |
|---|---|
| -c | 指定された位置の各文字だけを表示する |
| -b | 指定された位置の各バイトだけを表示する |
| -d | -fと一緒に用い、フィールドの区切り文字を指定。デフォルトはtab |
| -f | 指定された各フィールドだけを表示する |
| -s | -fと一緒に用い、フィールドの区切り文字を含まない行を表示しない |
- file5のフィールドの区切り文字を空白として、2番目のフィールドを取り出す
$ cut -d '' -f 2 file5
- file5のフィールドの区切り文字として、1,3番目のフィールドを取り出す
$ cut -d '' -f 1,3 file5
- file5の1文字目から3文字目までを取り出す
$ cut -c 1-3 file5
- 「ps ax」の表示行の先頭5文字(プロセスID)を取り出す
$ ps ax | cut -c 1-5
###wc [option] [ファイル名]
ファイル内のバイト数、単語数、行数を表示するにはwcコマンドを使用する
| option | explane |
|---|---|
| -c | バイト数だけを出力する |
| -l | 行数だけを出力する |
| -w | 単語数だけを出力する |
[vagrant@localhost ~]$ cat file1.txt
aaa
bbb
ccc
[vagrant@localhost ~]$ wc file1.txt
3 3 12 file1.txt
###cat, grep, wcを組み合わせる
cat /var/log/maillog | grep 'reject' | wc -l
###od [option] [ファイル名]
od(Octal Dump)はファイルの内容を8進数で表示するコマンド
バイナリファイルの内容を表示する場合や、テキストファイルに含まれる非印字コードを調べる時などに使用すると便利
| option | explane |
|---|---|
| -d | Decimal(10進数)で表示する |
| -x | Hexa Decimal(16進数)で表示する |
| -c | ASCII文字またはバックスラッシュ付きのエスケープ文字として表示する |
| -A 基数 | 表示されるオフセットの基数を選択する。基数として指定できるのは以下の通りである d:10進数 o:8進数(デフォルト) x:16進数 n:なし(オフセットを表示しない) |
###expand [option] [ファイル名]
引数で指定されたファイル内にあるタブをスペースに変換する
| option | explane |
|---|---|
| -i | 行頭のタブのみスペースへ変換する |
| -t | 書き換える行数を指定する |
###unexpand
expandの逆。スペースからタブに変換する
###pr [option] [ファイル名]
印刷用にファイルのページ付、段組を行う
###split [option] [入力元ファイル[プレフィックス]]
ファイルを決まった大きさに分割する
デフォルトでは、入力元ファイルを1,000行ずつ出力ファイルに分割する