find と sed で tree コマンド

昔作ったものを思い出したよ。

#!/bin/sh

COMMAND="${0##*/}"
usage() {
    echo "${COMMAND} <find command options>"
    exit
}
[ "$1" = '-h' -o "$1" = '-help' ] && usage

FIND=`which find`
SED=`which sed`

[ -d "$1" ] && DIR=$1 && shift || DIR=.
(cd ${DIR}; pwd)
${FIND} "${DIR}" "$@" | \
${SED} -e "s,^${DIR},," \
       -e '/^$/d' \
       -e 's,[^/]*/\([^/]*\)$,\+--\1,' \
       -e 's,[^/]*/,   ,g' \
       -e 's,\(^\+--\)\|\(^   \),,'

もう、どこで拾ったのか忘れてしまったけど、sedの使い方に感動してシェル化したもの。

$ tree
/home/teramako/develop/javascript/hatenaEditor
chrome
+--content
   +--about.xul
   +--autolink.js
   +--editor.js
   +--hatena.js
   +--hatenaEditor.xul
   +--heFileUtil.js
   +--heRDF.js
   +--hesidebar.xul
   +--overlay.xul
   +--sample.css
   +--sample.js
   +--sample.xul
   +--sampleEditor.xul
   +--sample_tree.xul
+--locale
   +--hatenaeditor.dtd
   +--ja-JP
      +--hatenaeditor.dtd
      +--sample.dtd
   +--sample.dtd
+--skin
   +--classic
      +--1.0
         +--sample.css
   +--sample.css
chrome.manifest
install.rdf

findコマンドのオプションを受け付けるので

$ tree -type d
/home/teramako/develop/javascript/hatenaEditor
chrome
+--content
+--locale
   +--ja-JP
+--skin
   +--classic
      +--1.0

とか

$ tree -name "c*"
/home/teramako/develop/javascript/hatenaEditor
chrome
+--content
   +--classic
chrome.manifest

面倒だけど

$ tree -type d -printf "%p\t%TY-%Tm-%Td %TH:%TM\n"
/home/teramako/develop/javascript/hatenaEditor
        2007-11-08 05:21
chrome  2006-11-04 23:28
content 2006-11-12 19:56
locale  2006-11-07 22:12
+--ja-JP        2006-11-05 22:01
skin    2006-11-05 03:22
+--classic      2006-11-05 03:23
   +--1.0       2006-11-05 03:23

なんて事もできる。