Created
September 27, 2013 02:35
-
-
Save frah/6723461 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
########################## ツリー表示スクリプト ########################### | |
# 第一引数に与えられたディレクトリがルートのディレクトリツリーを表示する。# | |
# また、ディレクトリの所有ユーザ名、グループ名、パーミッションを表示する。# | |
# シンボリックリンク先も辿る。 # | |
# # | |
# 引数1: ツリー表示したいディレクトリパス # | |
# 引数2: 走査する深度(オプション) # | |
# # | |
# 2013/09/20: 引数処理バグ修正 # | |
# 2013/09/20: 各階層ごとにソートするよう変更 # | |
# 2013/09/27: ファイル一覧取得オプション追加 # | |
########################################################################### | |
MAXDEPTH=3 | |
FINDARG="-type d -o -type l" | |
#{{{function dir_tree() | |
function dir_tree() | |
{ | |
local dir=$1 | |
local indent="$2 " | |
local depth=$3 | |
local head | |
local next | |
local list="$(find -L ${dir} -maxdepth 1 ${FINDARG} | sed 1d | sort)" | |
local num=$(echo "$list" | wc -w) | |
local count=0 | |
local name | |
local stats | |
((depth++)) | |
[ $depth -gt $MAXDEPTH ] && return | |
for file in $list | |
do | |
((count++)) | |
if [ $count -eq $num ] | |
then | |
head="$indent\`--" | |
next="$indent " | |
else | |
head="$indent|--" | |
next="$indent|" | |
fi | |
name=$(basename $file) | |
printf "%-50s" "$head $name" | |
#stat -c ' (%U:%G:%a)' $file | |
stats=$(stat -c '%U:%G:%a' $file) | |
IFS=':' read -ra stat <<< "$stats" | |
printf " (%-10s:%-10s:%3s)\n" ${stat[0]} ${stat[1]} ${stat[2]} | |
[ -d "$dir/$name" ] && dir_tree $dir/$name "$next" $depth | |
done | |
} | |
#}}} | |
while getopts "fd:" flag; do | |
case $flag in | |
\?) OPT_ERROR=1; break;; | |
f) FINDARG="${FINDARG} -o -type f";; | |
d) | |
if expr "$OPTARG" : '[0-9]*' > /dev/null ; then | |
MAXDEPTH="$OPTARG" | |
else | |
OPT_ERROR=1; break; | |
fi;; | |
esac | |
done | |
shift $(( $OPTIND - 1 )) | |
if [ $OPT_ERROR ]; then # option error | |
echo >&2 "usage: $0 [-f] [-d MaxDepth] TargetPath" | |
exit 1 | |
fi | |
top=$1 | |
echo $(basename $top) | |
dir_tree $top "" 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment