* 基礎編 [#o4d6b7f4] #contents ** UNIXコマンドの利用 [#k284908d] - ローカル開発環境 - レンタルサーバーのうちSSH接続 - MacOS X ** ログイン、ログアウトをしよう [#yd547b9b] Terminal.appからyuji.devにログインする ssh yuji@yuji.dev password: [yuji@localhost ~]$ - 「[yuji@localhost ~]$」部分はプロンプトと呼ばれる - 「localhost」はサーバーが自分自身を指す用語 - 「~」はホームフォルダの意味 - 「$」は一般ユーザーの意味 - ログアウトする $ exit ** パスワードを変更しよう [#y446f205] $ passwd ** ディレクトリを移動する (1) [#p5bacf96] - 現在のディレクトリを表示 $ pwd - ディレクトリを変更(移動)する $cd - トップディレクトリへ移動する $ cd / - ホームディレクトリへ移動する $ cd ~ ** ディレクトリを移動する (2) [#bce7cd9c] - 絶対指定 -- 「/home/yuji」 - 相対指定(. ..) -- 「.」は自分自身 -- 「..」は一つ上のディレクトリ - 一つ上のディレクトリへ移動 $ cd .. - 二つ上のディレクトリへ移動 $ cd ../.. ** ディレクトリの中身をみよう [#yef7fffa] - ディレクトリの中身をみる $ ls - ディレクトリの中身の詳細をみる $ ls -l - 隠しファイルも表示する ※複数のオプションを組み合わせることも可能 $ ls -a ** ディレクトリの詳細情報を読み解こう [#e7f8036a] $ ls -l drwxr-xr-x 20 root root 4096 Dec 19 00:20 var - 「d」はディレクトリか否か - 「rwxr-xr-x」はパーミッション -- 「rwx」は所有者「r-x」はグループ「r-x」は全ての人のパーミッション -- r : read -- w : write -- x : 実行 - 1つ目の「root」は所有者 - 2つ目の「root」はグループ名 - 「4096」サイズ - 「Dec 19 00:20」は最終更新日 - 「var」は名前 ** パーミッションを理解する [#g29c93b1] - パーミションを「rwxr-xr-x」に変更する $ chmod 755 - 数字は「r」「w」「x」の足し算 -- 4: r : read -- 2: w : write -- 1: x : 実行 ** ディレクトリの作成、削除 [#s265b0c5] - ディレクトリ「test1」を作成 $ mkdir test1 - ディレクトリ「test1」を削除 $ rmdir test1 ** ファイルの作成、削除、コピー、移動 [#t99271d8] - 「test」ディレクトリおよび「test.txt」ファイルを作成 $ mkdir test $ vi test.txt - 「test.txt」のコピー「text2.txt」を作成 $ cp test.txt test2.txt - ディレクトリ「test」配下にコピー「test3.txt」を作成 $ cp test.txt test/test3.txt - 「test3.txt」を削除 $ rm test3.txt - 一つ上の階層の「test2.txt」を削除 $ rm ../test.txt - 「text.txt」を「test」配下へ移動 $ mv test.txt test/ - 「text.txt」の名前を「text2.txt」に変更 $ mv test.txt test2.txt ** ファイルの中身をみよう [#o05d8dbb] - 「test2.txt」ファイルの中身を全て表示する $ cat test2.txt - ファイルの中身を順を追って表示する $ more test2.txt $ less test2.txt - 最初の10行を表示する (オプションで行数を指定可) $ head test2.txt - 最後の10行を表示する (オプションで行数を指定可) $ tail test2.txt ** ワイルドカードについて [#f711069b] - 「*」:任意の文字いくつでも - 「?」:任意の一文字 - 拡張子が「.txt」のファイルを全て削除 $ rm *.txt - 「test6.text」というファイルを削除 $ rm test6.te?t ** findを使ってみよう [#b3d7ef94] - カレントディレクトリから「test_」を含むファイルを検索 $ find . -name "test_*" -type f - カレントディレクトリから「test_」を含むディレクトリを検索 $ find . -name "test_*" -type d ** grepを使ってみよう [#l304d203] - 使い方は「grep -[option] [文字列] [対象ファイル]」 - 「111」という文字列を含むファイルを検索 $ grep 111 * - 「111」という文字列がマッチした回数をカウントする $ grep -c 111 * - 配下のディレクトリも含めて全て検索 $ grep -r 111 * - 「111」且つ「222」という文字列を含むファイルを検索 $ grep 111 * | grep 222 ** コマンドラインの便利な使い方 [#i5ec879b] - [tab]キーを一回押すとコマンドの候補を表示する - [tab]キーを二回押すと候補一覧を表示する - 矢印キーを押すと過去に実行したコマンドを遡ることができる - 過去に実行したコマンド一覧を表示する $ history - (history実行後)コマンド番号「255」を指定して再実行 $ !255 - 前回実行したコマンドを実行 $ !! - 前回実行したgrepコマンドを実行 $ !gr - 前回実行したgrepコマンドを表示 $ !gr:p ** 便利な小技いろいろ [#ub3cdcd7] - manコマンドでgrepのマニュアルを見る ※スペースキーでスクロール「q」で終了 $ man grep - ディレクトリ構造を表示する $ tree - (サーバの)時間を表示する $ date - カレンダーを表示する $ cal - 2030年のカレンダーを表示する $ cal -y 2030 - 「test1.txt」ファイルの行数を表示する $ wc test1.txt $ wc -l test1.txt ** lsコマンドのカラーリング [#x483eaf6] - エイリアスの登録 $ cd $HOME $ vim .bashrc alias ls = 'ls -G' - エイリアスを使用可能に設定 $ cd $HOME $ vim .bash_profile source .bashrc - 変更の反映(下記コマンド実行or再起動) $ source .bash_profile ** コマンド実行履歴検索(reverse-i-search) [#m5d71335] - [Ctrl]+[r] ** 改行コード [#m7c416d1] chr(13) : CR (キャリッジリターン) : MacOS ver.9まで chr(10) : LF (ラインフィード) : UNIX系システム chr(13)||chr(10) : CRLF : Windows系 // 正規表現 \r : CR (キャリッジリターン) \n : LF (ラインフィード) \r\n : CRLF * 応用編 [#k91e1a1f] ** ログファイルgrep検索方法 [#x955cf0b] - 対象:~/log_dir/logfile.201410* - 条件:keyword1かつkeyword2を含みkeyword3またはkeyword4またはkeyword5を含むログを表示 - zgrepは圧縮ファイルも検索 $ zgrep keyword1 ~/log_dir/logfile.201410* grep keyword2 | egrep "(keyword3|keyword4|keyword5)" | less ** 時系列別500系エラー出力数表示 [#of5afe5d] - 対象:/usr/local/apache2/logs/logfile.201410* $ egrep -v '(\.gif|\.png|\.jpg|\nagios-plugins)' /usr/local/apache2/logs/logfile.201410* | awk '{if ($10 ~ /^(500|502|503)/ ) print $0}' | awk '{print $4}'| awk -F: '{print $1,$2}'|sort|uniq -c|sed "s/\[//" ** PostgreSQLスロークエリ(duration)ログのヒット数をカウント [#cfc185a4] - 対象:/var/log/pgsql/postgresql-2014-10-**_*.log - 条件:keyword1かつkeyword2を含む $ grep duration /var/log/pgsql/postgresql-2014-10-**_*.log | grep keyword1 | grep keyword2 | wc -l ** home配下からkeywordを含むファイルをfind検索 [#qaddc2d9] $ find ~/ -type f -print | xargs grep "keyword" ** rsyncによるファイル同期(デプロイ) [#i2b033d4] - 同期元ディレクトリ:/home/src/ - 同期先ホスト名:host - 同期先ディレクトリ:/home/www/ - 「n」オプションは「dry-run」のため実行時には除外 $ rsync -n -vazc /home/src/ host:/home/www/ ** シンボリックリンク [#vaa8783d] - シンボリックリンク作成 $ ln -s [path] - シンボリックリンク一覧表示 $ find . -type l - シンボリックリンクの実体パス表示 $ readlink [symlink]