Pages

Tuesday, August 30, 2016

Directory Tree

Display folders hierarchy in tree style: 

ex: dt  -- all folders from current
ex: dt 2 -- 2 levels from current
ex: dt xxx - all folders from xxx, which is a 1st level subfolder from current
ex: dt xxx 3 - 3 levels from xxx, which is a 1st level subfolder from current

to look at only current folder , no sub-folder:

  • ls -d XXXX *.*
  • find XXXX -maxdepth 1 -type f 



OS: GNU/Linux
---------------------------------------------------------------------------------------------
dt(){
    if [[ -z $1 ]]; then
        find . -type d 2> /dev/null | sort | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
    else
        if [[ -z $2 ]]; then
            if [ -d "$1" ]; then
               find $1 -type d 2> /dev/null | sort | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
            else
               if [[ $1 =~ ^[0-9]+$ ]]; then
                  find . -maxdepth $1 -type d 2> /dev/null | sort | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
               fi
            fi
        else
            find $1 -maxdepth $2 -type d 2> /dev/null | sort | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
        fi;
    fi
}


------------------------ verbose: same as dt, with folder attribute info & size
d2(){
    if [[ -z $1 ]]; then
        find . -type d 2> /dev/null | sort | xargs -I % ksh -c "ls -ld % | tr '\n' ' ' ;   du -sh %" | awk '{print $9"        [ "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" " $10" ]"}' | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
    else
        if [[ -z $2 ]]; then
            if [ -d "$1" ]; then
               find $1 -type d 2> /dev/null | sort | xargs -I % ksh -c "ls -ld % | tr '\n' ' ' ;   du -sh %" | awk '{print $9"        [ "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" " $10" ]"}' | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
            else
               if [[ $1 =~ ^[0-9]+$ ]]; then
                  find . -maxdepth $1 -type d 2> /dev/null | sort | xargs -I % ksh -c "ls -ld % | tr '\n' ' ' ;   du -sh %" | awk '{print $9"        [ "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" " $10" ]"}' | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
               fi
            fi
        else
            find $1 -maxdepth $2 -type d 2> /dev/null | sort | xargs -I % ksh -c "ls -ld % | tr '\n' ' ' ;   du -sh %" | awk '{print $9"        [ "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" " $10" ]"}' | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
        fi;
    fi            
}