シェルスクリプトでやってみた系


シェルスクリプトで関数


シェルスクリプトで2次元配列


同じ構成のディレクトリ間でdiffをとる

#!/usr/bin/bash
dst_path=$1
for diff_file in $(cat tmp/diff_list.txt); do
       diff -cw ${diff_file} ${dst_path}/${diff_file} > diff_tmp.txt
       if [ $? -eq 1 ]; then
           echo "[*] ${diff_file}"
           cat diff_tmp.txt
       fi
done

"find -exec grep" と "find + xargs grep" の速さの違い

大量に"*.c"のファイルがある場所で、以下のコマンドを実行

$ time find . -type f -name '*.c' -exec grep -iHn 'foo' {} \;
...
...
...

real    0m11.588s
user    0m5.996s
sys     0m0.588s

$ time find . -type f -name '*.c' | xargs grep -iHn 'foo'
...
...
...

real    0m8.699s
user    0m8.489s
sys     0m0.096s

カレントディレクトリ以下のディレクトリサイズの総合計を得る

#!/bin/bash
IFS="
"
dirsize=0
for dir in $(find . -type d); do
       current_dirsize=$(ls -l "$dir" | sed -n '1p' | awk '{print $2}')
       dirsize=$(($dirsize + $current_dirsize))
done
echo "$dirsize"

duコマンドで得られるのはブロックサイズなので、つくったもの。
「ls -l」でそのディレクトリ直下のファイルサイズ合計が得られるので、
それを全てのディレクトリに対して行い、和をとっている。


しかし、遅いので使うべきでないです。
http://sonic64.com/2004-05-26.html
こっちの記事のやり方の方がいい。

他に参考:
最終更新:2014年06月04日 07:45