#! /bin/ksh # # Author: Mike Fleming mike@tauzero.co.uk # # Perform an ls and convert listing to CSV in a useful format # # lstocsv [ -d ] [ -f ] [ dirname ] # # -d - directories only # -f - files only # # dirname - name of directory to do ls on (default current directory) # # CSV line format is name, date, time, size, type (directory or file) DIR= FIL= USAGE="\nlstocsv [-d] [-f] directory\n\n directory can be . for current directory (default)\n\n" while getopts :df opt do case $opt in d) DIR=Y;; f) FIL=Y;; ?) echo "Unknown option $OPTARG" echo "$USAGE" return 1;; *) echo "$USAGE" return 1;; esac done shift $((OPTIND - 1)) if [[ "$DIR" == "" && "$FIL" == "" ]] then DIR=Y FIL=Y fi TARGET=${1:-.} echo "Name,Date,Time,Size,Type" ls -l $TARGET | cut -c"1 32-44 45-47 48-51 52-57 58-" | while read T S D M Y N do if [[ "$T" == "d" && "$DIR" == "Y" || "$T" == "-" && "$FIL" == "Y" ]] then if [[ "$T" == "d" ]] then T=dir DU=$(du -s "$TARGET/$N" 2>/dev/null) if [[ $? == 0 ]] then S=$(($(echo "$DU" | cut -f1) * 512)) else S=? fi else T=file fi datetoymdhm $D $M $Y | read DT TM echo "$N,$DT,$TM,$S,$T" fi done