#!/bin/sh #lls v1.0.1 # "lls" is a sript that works as "ls" with additional info on files spiced with some fancy colors. # You can config your own colors in ~/.llsrc #color table black="30;2" dark_gray="30;1" blue="34;2" light_blue="34;1" green="32;2" light_green="32;1" cyan="36;2" light_cyan="36;1" red="31;2" light_red="31;1" purple="35;2" light_purple="35;1" yellow="33;2" light_yellow="33;1" light_gray="37;2" white="37;1" #color config if [ -f ${HOME}/.llsrc ]; then source ${HOME}/.llsrc else echo -e " # lls colour config: # # Avaliable colours: # black, dark_gray # blue, light_blue # green, light_green # cyan, light_cyan # red, light_red # purple, light_purple # yellow, light_yellow # light_gray, white # # The lls defaults are set already for convenience. # You can add the desired colour after \"=\$\" # choose your lls mode: \"simple\" or \"default\" (Note that there is no \"\$\" sign after \"=\") lls_mode=simple #File additional info colour all_prop=\$dark_gray #Searchable(owned) directory colors o_dir=\$light_blue o_dir_tag=\$light_cyan #Not searchable(owned) directory colors c_dir=\$red c_dir_tag=\$light_red #Readable and(-a) writable file colors fo_file=\$green fo_tag=\$light_green #Readable writable and(-a) executable file colors fox_file=\$yellow fox_tag=\$light_yellow #Executable only file colors fc_file=\$red fc_tag=\$light_red #Executable and(-a) read only file colors fsx_file=\$red fsx_tag=\$light_red #Read only file colors fso_file=\$red fso_tag=\$light_red #Executable and(-a) writable only file colors fex_file=\$purple fex_tag=\$light_purple #Writable only file colors feo_file=\$purple feo_tag=\$light_purple #Not writable readable and(-a) executable file colours fz_file=\$dark_gray fz_tag=\$dark_gray" > ${HOME}/.llsrc source ${HOME}/.llsrc fi if [ $lls_mode = "simple" ]; then label1="DIR read/write" label2="DIR closed" label3="FILE read/write" label4="FILE read/write/executable" label5="FILE closed executable" label6="FILE read only executable" label7="FILE read only" label8="FILE write only executable" label9="FILE write only" lable0="FILE zombie" elif [ $lls_mode = "default" ]; then label1="DRWX" label2="D---" label3="-RW-" label4="-RWX" label5="---X" label6="-R-X" label7="-R--" label8="--WX" label9="--W-" label0="----" else echo "please, provide a valid TAG mode in ~/.llsrc!" exit 0 fi colorize() { color="$1" str="$2" echo -e "\033[${color};148m${str}\033[39m" # tput sgr0 } filter_x () { if [ $SHOW_X -eq 1 -a $lls_mode = "simple" ]; then grep -E 'FILE .*executable' elif [ $NO_X -eq 1 -a $lls_mode = "simple" ]; then grep -Ev 'FILE .*executable' elif [ $SHOW_X -eq 1 -a $lls_mode = "default" ]; then grep -E '(DRWX|-RWX|---X|-R-X|--WX)' elif [ $NO_X -eq 1 -a $lls_mode = "default" ]; then grep -Ev '(DRWX|-RWX|---X|-R-X|--WX)' else cat fi } ###################################################################### add_file_props() { if [ $ADD_FILE_PROPS -eq 1 ]; then echo -ne "$1" fi echo "" tput sgr0 } ###################################################################### file_cnt () { files=$(find "$fname" -maxdepth 1 -type f | wc -l) dirs=$(find "$fname" -maxdepth 1 -type d | wc -l) tab=$tab$customtab echo -e "($((dirs-1)) dirs, $files files)" } findfunc () { shift tab=${tab#:}$customtab for fname in $@; do echo -e "$tab\___\033[34;1;148m${fname##*/}\033[39m \033[30;1;148m`file_cnt`\033[39m" if [ -d "$fname" ]; then findfunc $(find "$fname" -maxdepth 1 -type d \( ! -iname ".*" \) | sort ) fi done tab=${tab%"$customtab"} } tree_loop () { for fname in $cmd_filt_dirs; do fname=${fname##${PWD}/} echo -e "\033[34;1;148m$fname\033[39m \033[30;6;148m`file_cnt`\033[39m" # tput sgr0 if [ -d $fname -a $fname != $curdir ]; then findfunc $(find "$fname" -maxdepth 1 -type d \( ! -iname ".*" \) | sort) fi unset dir singletab tab done } ###################################################################### filter_types () { types=${types#"\|"} eval $cmd_list_files | while read files; do matched=$(file "$files" | sed -n "/$types/p") if [ ! -z $matched ]; then matched=${matched%%:*} file_info $matched fi done } ###################################################################### file_info ( ) { file=$1 # dont bother computing stuff we will not use if [ $ADD_FILE_PROPS -eq 1 ]; then file_prop=$(file -b "$file") file_size=$(ls -lh "$file" | awk '{print " "$5}') fi fname=${file##$PWD/} # about directories if [ -d "$file" ]; then if [ -x "$file" ]; then echo -n `colorize "$o_dir" "$fname"` echo -n `colorize "$o_dir_tag" " > $label1"` tput sgr0 add_file_props " `colorize "$all_prop" "{$file_prop}"`" else echo -n `colorize "$c_dir" "$fname"` echo -n `colorize "$c_dir_tag" " > $label2"` tput sgr0 add_file_props " `colorize "$all_prop" "{$file_prop}"`" fi fi # about files if [ -f "$file" ]; then if [ -r "$file" -a -w "$file" -a ! -x "$file" ]; then echo -n `colorize "$fo_file" "$fname"` echo -n `colorize "$fo_tag" " > $label3"` tput sgr0 add_file_props " `colorize "$all_prop" "{$file_prop}$file_size"`" elif [ ! -w "$file" -a ! -r "$file" -a -x "$file" ]; then echo -n `colorize "$fc_file" "$fname"` echo -n `colorize "$fc_tag" " > $label5"` tput sgr0 add_file_props " `colorize "$all_prop" "{$file_prop}$file_size"`" elif [ -w "$file" -a -r "$file" -a -x "$file" ]; then echo -n `colorize "$fox_file" "$fname"` echo -n `colorize "$fox_tag" " > $label4"` tput sgr0 add_file_props " `colorize "$all_prop" "{$file_prop}$file_size"`" elif [ ! -w "$file" -a -r "$file" -a -x "$file" ]; then echo -n `colorize "$fsx_file" "$fname"` echo -n `colorize "$fsx_tag" " > $label6"` tput sgr0 add_file_props " `colorize "$all_prop" "{$file_prop}$file_size"`" elif [ -w "$file" -a ! -r "$file" -a -x "$file" ]; then echo -n `colorize "$fex_file" "$fname"` echo -n `colorize "$fex_tag" " > $label8"` tput sgr0 add_file_props " `colorize "$all_prop" "{$file_prop}$file_size"`" elif [ -w "$file" -a ! -r "$file" -a ! -x "$file" ]; then echo -n `colorize "$feo_file" "$fname"` echo -n `colorize "$feo_tag" " > $label9"` tput sgr0 add_file_props " `colorize "$all_prop" "{$file_prop}$file_size"`" elif [ ! -w "$file" -a -r "$file" -a ! -x "$file" ]; then echo -n `colorize "$fso_file" "$fname"` echo -n `colorize "$fso_tag" " > $label7"` tput sgr0 add_file_props " `colorize "$all_prop" "{$file_prop}$file_size"`" elif [ ! -w "$file" -a ! -r "$file" -a ! -x "$file" ]; then echo -n `colorize "$fz_file" "$fname"` echo -n `colorize "$fz_tag" " > $label0"` tput sgr0 add_file_props " `colorize "$all_prop" "{$file_prop}$file_size"`" fi fi } ###################################################################### usage() { tput sgr0 echo -e " Usage: lls [OPTION] PATH \"lls\" is a sript that works as \"ls\" with additional info on files spiced with some fancy colors. You can config your own colors in ~/.llsrc OPTIONS: -h, shows this help -e, list extra file information -a, list all files and directories -f, list files only -d, list directories only -x, list executable files only -X, list all except executable and hidden files -c, list all compressed (archived) files -p, list all images only -v, list all video files only -t, list all text files or documents only -u, list all audio files only -z, make a pretty tree of folders " tput sgr0 } ###################################################################### LIST_ALL=0 ONLY_FILES=0 ONLY_DIRS=0 ADD_FILE_PROPS=0 SHOW_X=0 NO_X=0 TREE=0 FILT=0 LIST_ARC=0 LIST_PIC=0 LIST_M=0 LIST_DOC=0 LIST_AU=0 cmd_filt_files="" cmd_filt_dirs="" customtab=":\t" IFS=" " curdir="${@: -1}" # last argument if [[ "$curdir" != /* ]]; then curdir=$PWD fi unset name # reseting $name in case it was set by environment while getopts :aefdxXpcvuthz opt; do case $opt in a) LIST_ALL=1 ;; e) ADD_FILE_PROPS=1 ONLY_FILES=1 ONLY_DIRS=1 ;; f) ONLY_FILES=1 ;; d) ONLY_DIRS=1 ;; x) SHOW_X=1 ONLY_FILES=1 ;; X) NO_X=1 ONLY_FILES=1 ;; h) usage exit 0 ;; z) cmd_filt_dirs=$(find "$curdir" -maxdepth 1 -type d \( ! -iname ".*" \) | sort) tree_loop exit 0 ;; c) LIST_ARC=1 ONLY_FILES=1 FILT=1 ;; p) LIST_PIC=1 ONLY_FILES=1 FILT=1 ;; v) LIST_M=1 ONLY_FILES=1 FILT=1 ;; t) LIST_DOC=1 ONLY_FILES=1 FILT=1 ;; u) LIST_AU=1 ONLY_FILES=1 FILT=1 ;; \?) usage exit 0 ;; esac done shift $((OPTIND-1)) # initializing conditions and vars if [ $ONLY_FILES -eq 1 ]; then cmd_filt_files='find "$curdir" -maxdepth 1 -type f \( ! -iname ".*" \) | sort' fi if [ $ONLY_DIRS -eq 1 ]; then cmd_filt_dirs='find "$curdir" -maxdepth 1 -type d \( ! -iname ".*" \) | sort' fi if [ $LIST_ALL -eq 1 ]; then if [ $ONLY_FILES -eq 1 ]; then cmd_filt_files='find "$curdir" -maxdepth 1 -type f | sort' elif [ $ONLY_DIRS -eq 1 ]; then cmd_filt_dirs='find "$curdir" -maxdepth 1 -type d | sort' else cmd_filt_files='find "$curdir" -maxdepth 1 -type f | sort' cmd_filt_dirs='find "$curdir" -maxdepth 1 -type d | sort' fi fi if [ $OPTIND -eq 1 ]; then cmd_filt_files='find "$curdir" -maxdepth 1 -type f \( ! -iname ".*" \) | sort' cmd_filt_dirs='find "$curdir" -maxdepth 1 -type d \( ! -iname ".*" \) | sort' fi cmd_list_dirs=$cmd_filt_dirs cmd_list_files=$cmd_filt_files if [ $LIST_ARC -eq 1 ]; then types="Archive\|archive\|compressed" fi if [ $LIST_PIC -eq 1 ]; then types=$types"\|Image\|image" fi if [ $LIST_M -eq 1 ]; then types=$types"\|Video\|video\|Matroska\|matroska\|3GPP\|3GPP2\|AVC\|movie\|MPEG sequence\|RealMedia" fi if [ $LIST_DOC -eq 1 ]; then types=$types"\|Document\|document\|Text\|text\|ASCII" fi if [ $LIST_AU -eq 1 ]; then types=$types"\|Audio\|AAC\|FLAC" fi # processing query if [ $FILT -eq 1 ]; then filter_types | filter_x else eval $cmd_list_dirs | while read files; do file_info "$files" done | filter_x eval $cmd_list_files | while read files; do file_info "$files" done | filter_x fi