This script lists all directories, or optionally, all of any type recognized by the -type T
option of find
. By default, if no arguments are provided, it lists all directories in the current dir. To list absolute paths, pass an absolute path as a target directory.
#!/bin/bash
# usage: ${0} [-type [fd]] [-l] <directory>
_TYPE="d" # if f, list only files, if d, list only directories
let _long=0
let _typeflag=0
# collect dirs and files
DIRS=( ) ; FILS=( )
for A in "$@" ; do
if [ $_typeflag -eq 1 ]; then
_TYPE=$A
let _typeflag=0
elif [ -d "$A" ]; then
DIRS=( ${DIRS[@]} "$A" )
elif [ -f "$A" ]; then
FILS=( ${FILS[@]} "$A" )
else
case "$A" in
"-type") let _typeflag=1 ;;
"-l") let _long=1 ;;
*) echo "not a directory: [$A]" 1>&2
exit 1
;;
esac
fi
done
# list files in current dir, if nothing specified
[ ${#DIRS[@]} -eq 0 ] && DIRS=( "$(pwd -P)" )
if [ $_long -eq 0 ]; then
find ${DIRS[@]} -maxdepth 1 -type $_TYPE | while read F ; do
if [[ "$F" != "." && "$F" != ".." ]]; then
echo "\"$F\""
fi
done | xargs ls -ltrad --time-style=long-iso | sed 's#.*:[0-9][0-9] ##'
else
find ${DIRS[@]} -maxdepth 1 -type $_TYPE | while read F ; do
echo "\"$F\""
done | xargs ls -ltrad --time-style=long-iso
fi