LoginSignup
21
18

More than 5 years have passed since last update.

備忘:UNIXコマンドの小ネタ

Last updated at Posted at 2014-07-18

普段からバリバリ使うわけではないが、覚えておくと便利なコマンドの使い方をメモしていく。

エイリアス

設定は

$ alias

で確認できる。
最初から設定されているものも結構あるので、確認のこと。

ls -l

なんて打ってる人がいたら、aliasの存在を教えてあげよう。(大抵は"ll"で登録してある)

aliasの内容は、~/.bashrcか~/.bash_aliasesで定義しておけばターミナル起動時から有効になる。

ぼくの.bash_aliases

~/.bash_aliases
# Enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# My aliases
alias gs='git svn'
alias gk='gitk --all &'
alias clip='xsel -i -b'


function ud () {
    if [ $# -ne 1 ]; then
        echo "不正な引数の数"
        return 0
    fi

    expr "$1" + 1 >/dev/null 2>&1
    if [ $? -ge 2 ]; then
        echo "不正な引数の型"
        return 0
    fi

    i=0
    until [ $i -ge $1 ]
    do
        cd ../
        i=`expr $i + 1`
    done
}

ちゃっかり自作のコマンド入ってます。
ll 以外に特によく使うのは gk と clip(両方共ソフトをインストールする必要はあるけど)

ディレクトリを監視するのに使える watch コマンド

$ watch -n 1 "ls -l"

1秒毎に"ls -l"を実行して、結果を表示してくれる。エイリアスは使えない。
オプションに-dを入れると、前回との差分をハイライトしてくれるので、それもオススメ。

多才な tail コマンドさん

普段私は -n と -f オプションをそのまま使うくらいだが、--help を読んだら結構面白かったので紹介。

-n +K

K行目から末尾までを読み込む

$ tail -n +2 hoge.xml > piyo.xml

これで hoge.xml のxml宣言以外をpiyo.xmlにコピーできる。

--follow=name

単純に -f オプションを使うとログのローテートなどでファイル名が変更された場合に、追跡が終わってしまう。("tail -f"は"tail --follow=descriptor"と同じ)
--follow=nameにしておくと、ファイルが一旦消えても同名のファイルが再出現した場合に追跡を再開してくれる。ちなみに、

tail: hoge.log: そのようなファイルやディレクトリはありません
tail: 'hoge.log' が現れました。新しいファイルの終端を追跡します

こんなメッセージが出る。

ログ監視の結果を grep に渡す

$ tail -follow=name access.log | grep -v " 200 "

これで HTTP のステータスコードが200以外のログを監視できる。

21
18
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
21
18