#!/bin/bash # lsdir v0.1 -- list only directories, not other files # # Written by Hean Kuan Ong ( mysurface@gmail.com) # Sept 9, 2007 # # Copyright (C) 2007 Hean Kuan Ong All rights reserved. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. SPATH="." CLI="" helpme() { echo "lsdir v0.1" echo "USAGE: lsdir [OPTION] [PATH]" echo "" echo "OPTION:" echo " -r, recursively list all the directories, including hidden directories." echo " -l, detail list of directories." echo " -s, list also hidden directories." echo " -v, verbose, display the acture find command line." echo " -h, help, display the usage." echo "" echo "LIMITATION:" echo " 1. lsdir do NOT support options separation well," echo " the line below may NOT work as expected." echo " lsdir -l -s ~/" echo " Options have to specified together , i.g. lsdir -ls ~/" echo "" echo " 2. Not like ls, options have to be specified before PATH," echo " the line below may NOT work as expected." echo " lsdir ~/ -l" echo "" echo " 3. PATH do NOT support regular expression." echo " the line below do NOT work as expected." echo " lsdir ~/b*" echo "" exit 2 } mkspath() { if [ -e $1 ] then SPATH="$1" cmdS2=" ! -wholename '$1'" else SPATH='.' cmdS2="" fi } optl=0 opts=0 optr=0 optv=0 while getopts "lsrvh" option do case $option in l ) optl=1 echo $OPTARG ;; s ) opts=1 ;; r ) optr=1 ;; v ) optv=1 ;; h ) helpme ;; * ) echo "unknown option $option" ;; esac done # command line detail list if [ $optl -ne 1 ] then cmdL=" -printf '%f\n' | column" else #option select cmdL=" -printf '%M+%u+%g+%f+%a\n' | column -s'+' -t" fi # command line list hidden directory if [ $opts -ne 1 ] then cmdS="-name '*' ! -name '.*'" else #option select cmdS="-name '*'" fi # command line for recursive if [ $optr -ne 1 ] then cmdR="-maxdepth 1" else #option select cmdR="" cmdL="" fi arg=$1 if [[ ${arg:0:1} != "-" ]] then SPATH="$1" cmdS2=" ! -wholename '$1'" else mkspath $2 fi CLI="find -L $SPATH $cmdR -type d $cmdS $cmdS2 $cmdL" if [ $optv -ne 0 ] then echo $CLI echo fi #execute the command line eval $CLI