基礎編

UNIXコマンドの利用

ログイン、ログアウトをしよう

Terminal.appからyuji.devにログインする

ssh yuji@yuji.dev
password:
[yuji@localhost ~]$
$ exit

パスワードを変更しよう

$ passwd

ディレクトリを移動する (1)

$ pwd
$cd
$ cd /
$ cd ~

ディレクトリを移動する (2)

$ cd ..
$ cd ../..

ディレクトリの中身をみよう

$ ls
$ ls -l
$ ls -a

ディレクトリの詳細情報を読み解こう

$ ls -l
drwxr-xr-x  20 root root  4096 Dec 19 00:20 var

パーミッションを理解する

$ chmod 755

ディレクトリの作成、削除

$ mkdir test1
$ rmdir test1

ファイルの作成、削除、コピー、移動

$ mkdir test
$ vi test.txt
$ cp test.txt test2.txt
$ cp test.txt test/test3.txt
$ rm test3.txt
$ rm ../test.txt
$ mv test.txt test/
$ mv test.txt test2.txt

ファイルの中身をみよう

$ cat test2.txt
$ more test2.txt
$ less test2.txt
$ head test2.txt
$ tail test2.txt

ワイルドカードについて

$ rm *.txt
$ rm test6.te?t

findを使ってみよう

$ find . -name "test_*" -type f
$ find . -name "test_*" -type d

grepを使ってみよう

$ grep 111 *
$ grep -c 111 *
$ grep -r 111 *
$ grep 111 * | grep 222

コマンドラインの便利な使い方

$ history
$ !255
$ !!
$ !gr
$ !gr:p

便利な小技いろいろ

$ man grep
$ tree
$ date
$ cal
$ cal -y 2030
$ wc test1.txt
$ wc -l test1.txt

lsコマンドのカラーリング

$ cd $HOME
$ vim .bashrc
alias ls = 'ls -G'
$ cd $HOME
$ vim .bash_profile
source .bashrc
$ source .bash_profile

コマンド実行履歴検索(reverse-i-search)

改行コード

chr(13) : CR (キャリッジリターン) : MacOS ver.9まで
chr(10) : LF (ラインフィード) : UNIX系システム
chr(13)||chr(10) : CRLF : Windows系
// 正規表現
\r : CR (キャリッジリターン)
\n : LF (ラインフィード)
\r\n : CRLF

ディスク使用量を表示

$ df -am
Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/vda3          98529  6672     86853   8% /
proc                   0     0         0    - /proc
sysfs                  0     0         0    - /sys
devpts                 0     0         0    - /dev/pts
tmpfs                499     0       499   0% /dev/shm
/dev/vda1            243    86       145  38% /boot
none                   0     0         0    - /proc/sys/fs/binfmt_misc
-a ... すべてのファイルシステムのディスクの使用状況を出力
-k ... 使用領域や空き領域などのサイズを、キロバイト(KB)単位で表示
-m ... 使用領域や空き領域などのサイズを、メガバイト(MB)単位で表示
-T ... ファイルシステムタイプも表示
-l ... ローカルホストに接続されたファイルシステムのディスクの使用状況だけを表示
$ du -ak /var/www/html/docs/
324	/var/www/html/docs/WRTG393_WA1_TechnicalDescription.pdf
108	/var/www/html/docs/WRTG393_Proposal.pdf
32	/var/www/html/docs/WRTG393_WA1_393_technicaldescription.pdf
144	/var/www/html/docs/WRTG393_WA2_393_instructions_or_procedures.pdf
3100	/var/www/html/docs/WRTG393_WA2_Instructions.pdf
3712	/var/www/html/docs/
-a ... ディレクトリの使用量だけでなく、その中に含まれるすべてのファイルシステムのディスクの使用状況をそれぞれ出力
-k ... ディスク使用量をキロバイト(KB)単位
-m ... ディスク使用量をメガバイト(MB)単位
-s ... 引数DIRに指定したディレクトリの中に含まれるサブディレクトリやファイルのディスク使用量を計算しない

応用編

ログファイルgrep検索方法

$ zgrep keyword1 ~/log_dir/logfile.201410* grep keyword2 | egrep "(keyword3|keyword4|keyword5)" | less

時系列別500系エラー出力数表示

$ 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)ログのヒット数をカウント

$ grep duration /var/log/pgsql/postgresql-2014-10-**_*.log | grep keyword1 | grep keyword2 | wc -l

home配下からkeywordを含むファイルをfind検索

$ find ~/ -type f -print | xargs grep "keyword"

rsyncによるファイル同期(デプロイ)

$ rsync -n -vazc /home/src/ host:/home/www/

シンボリックリンク

$ ln -s [path]
$ find . -type l
$ readlink [symlink]

複数ファイルに対して複数条件の文字列で一括置換

s/検索文字列1/置換文字列1/g
s/検索文字列2/置換文字列2/g
s/検索文字列3/置換文字列3/g
s/検索文字列4/置換文字列4/g
s/検索文字列5/置換文字列5/g
p
d
$ find ./dir/ -type f | xargs sed -i -f replace.sed

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS