From 1915a3edf9c9ecf34baf7c776c7f2891955f58ad Mon Sep 17 00:00:00 2001 From: Akelge Date: Fri, 25 Jan 2013 15:31:13 +0000 Subject: [PATCH] Added function 'bak'. Splitted other functions --- zsh/zsh.d/50-functions.zsh | 29 ++------ zsh/zsh.d/zshfunctions/bak | 131 ++++++++++++++++++++++++++++++++++++ zsh/zsh.d/zshfunctions/cdb | 1 + zsh/zsh.d/zshfunctions/dust | 1 + zsh/zsh.d/zshfunctions/hdu | 17 +++++ zsh/zsh.d/zshfunctions/hist | 1 + 6 files changed, 158 insertions(+), 22 deletions(-) create mode 100644 zsh/zsh.d/zshfunctions/bak create mode 100644 zsh/zsh.d/zshfunctions/cdb create mode 100644 zsh/zsh.d/zshfunctions/dust create mode 100644 zsh/zsh.d/zshfunctions/hdu create mode 100644 zsh/zsh.d/zshfunctions/hist diff --git a/zsh/zsh.d/50-functions.zsh b/zsh/zsh.d/50-functions.zsh index 95cdcae..f65c76d 100644 --- a/zsh/zsh.d/50-functions.zsh +++ b/zsh/zsh.d/50-functions.zsh @@ -2,33 +2,18 @@ # Functions # cdb - Goes to folder by complete path -function cdb { cd `dirname $1` } +autoload -U cdb # hist - Grep from history -function hist { grep -i $* $HISTFILE } +autoload -U hist # hdu - Human readable report of files and directories sizes -# *Same behaviour as du* -function hdu () { - du -kc $* | sort -nr | awk '{ - # Prepare human readable - if($1>=1024*1024) { size=$1/1024/1024; unit="G" } - else if($1>=1024) { size=$1/1024; unit="M" } - else { size=$1; unit="K" }; - format="%10.2f%s"; - hsize=sprintf(format,size,unit); +autoload -U hdu - # Remove $1 (size), then removes " " at the start of $0 - $1=""; thepath=$0; sub(/^ /,"",thepath); +# dust - list total size in . directory +autoload -U dust - # Print size and path (directories are bolded) - if ( system("[ -d \""thepath"\" ]") ) - printf "%-8s %s\n",hsize,thepath; - else - printf "%-8s \033[1m%s\033[0m/\n",hsize,thepath; - }' -} -# list total size in . directory -function dust () { hdu -s * } +# bak - remove backup files +autoload -U bak # vim: set ts=4 sw=4 tw=0 ft=zsh : diff --git a/zsh/zsh.d/zshfunctions/bak b/zsh/zsh.d/zshfunctions/bak new file mode 100644 index 0000000..11c5fb3 --- /dev/null +++ b/zsh/zsh.d/zshfunctions/bak @@ -0,0 +1,131 @@ +hflag=no # help +nflag=no # nodelete - do not delete files +rflag=no # recursion +pflag=no # pyc - consider .pyc file +vflag=no # list files (performs delete anyway) +aflag=no # all - flags r + p + m +mflag=no # vim - consider .swp, .swo, swn +dflag=no # specify directory +# +directory="" +startdir=$PWD +TMP=/tmp/bak`date +%s` +# +esc_bold='\033[1m' # http://en.wikipedia.org/wiki/ANSI_escape_code#CSI_Codes +esc_reset='\033[0m' +esc_red='\033[91m' +esc_green='\033[92m' +# + +#################################################################################### +# GETOPT +# +set -- $(getopt hnmrpva "$@") +while [ $# -gt 0 ] +do + case "$1" in + -h) hflag=yes;; + -n) nflag=yes;; + -r) rflag=yes;; + -p) pflag=yes;; + -m) mflag=yes;; + -v) vflag=yes;; + -a) pflag=yes; mflag=yes; rflag=yes;; + --) shift; break;; + -*) echo "$0: error - unrecognized option $1" 1>&2; return;; + *) break;; + esac + shift +done + + + #################################################################################### + # HELP FLAG + # print help and exit + + if [ $hflag = yes ]; then + echo + echo -e "${esc_bold}Usage:${esc_reset} bak [-hnrpva] [DIRECTORY]" + echo -e " Deletes backup files ( *~ )" + echo -e " If no DIRECTORY deletes in current directory" + echo + echo -e "${esc_bold}Options:${esc_reset}" + echo -e " ${esc_green}-h${esc_reset} Display this help" + echo -e " ${esc_green}-n${esc_reset} Dry run (${esc_green}no delete will occur${esc_reset})" + echo -e " ${esc_green}-r${esc_reset} Recursive" + echo -e " ${esc_green}-p${esc_reset} Deletes also .pyc" + echo -e " ${esc_green}-m${esc_reset} Deletes also VIM files (${esc_red}backup & swap${esc_reset})" + echo -e " ${esc_green}-v${esc_reset} Verbose (lists files)" + echo -e " ${esc_green}-a${esc_reset} Alias for -rpm" + echo + return + fi + + #################################################################################### + # PREPARE + + # Directory + directory=${1:-$PWD} + # check if given directory is a directory + if [ ! -d $directory ] ; then + echo "'$directory' is not a directory" + return + fi + + # Recursion + depth='-maxdepth 1' + andits='' + if [ $rflag = yes ]; then + depth="" #recursive + andits=' and its subdirectories' + fi + + #################################################################################### + # EXEC + + cd $directory + + #list files in TMP file + find . -name "*~" -type f ${=depth} -exec echo "\"{}\"" \; > $TMP 2> /dev/null + if [ $pflag = yes ]; then + find . -name \*.pyc -type f ${=depth} -exec echo "\"{}\"" \; >> $TMP 2> /dev/null + fi + if [ $mflag = yes ]; then + find . \( -name \*.swp -or -name \*.swo -or -name \*.swn \) ${=depth} -exec echo "\"{}\"" \; >> $TMP 2> /dev/null + fi + + FILES=`wc -l $TMP | cut -d/ -f1 | sed 's/[ ]//g' ` + if [ ${FILES} -ge 1 ] ; then + + if [ $vflag = yes ]; then + # list files + echo "Deleted files:" + cat -n $TMP | sed 's/"//g' + echo + fi + if [ ${FILES} = 1 ]; then + label_file="file" + else + label_file="files" + fi + + if [ $nflag = no ]; then + cat $TMP | xargs rm -f #delete + echo -e " ${esc_red}$FILES bak ${label_file}${esc_reset} deleted in ${esc_green}$directory${esc_reset}$andits" + else + echo -e " ${esc_green}$FILES bak ${label_file}${esc_reset} ${esc_red}listed${esc_reset} in ${esc_green}$directory${esc_reset}$andits" + fi + + else + echo -e " ${esc_green}No bak files${esc_reset} listed in ${esc_green}$directory${esc_reset}$andits" + fi + + #remove TMP file + rm -f $TMP + + # return to startdir + cd $startdir + unset startdir + unset directory + +# vim: set ts=4 sw=4 tw=0 ft=zsh : diff --git a/zsh/zsh.d/zshfunctions/cdb b/zsh/zsh.d/zshfunctions/cdb new file mode 100644 index 0000000..eceaa91 --- /dev/null +++ b/zsh/zsh.d/zshfunctions/cdb @@ -0,0 +1 @@ +cd `dirname $1` diff --git a/zsh/zsh.d/zshfunctions/dust b/zsh/zsh.d/zshfunctions/dust new file mode 100644 index 0000000..f06eb31 --- /dev/null +++ b/zsh/zsh.d/zshfunctions/dust @@ -0,0 +1 @@ +hdu -s * diff --git a/zsh/zsh.d/zshfunctions/hdu b/zsh/zsh.d/zshfunctions/hdu new file mode 100644 index 0000000..325fd90 --- /dev/null +++ b/zsh/zsh.d/zshfunctions/hdu @@ -0,0 +1,17 @@ +du -kc $* | sort -nr | awk '{ +# Prepare human readable + if($1>=1024*1024) { size=$1/1024/1024; unit="G" } + else if($1>=1024) { size=$1/1024; unit="M" } + else { size=$1; unit="K" }; + format="%10.2f%s"; + hsize=sprintf(format,size,unit); + +# Remove $1 (size), then removes " " at the start of $0 + $1=""; thepath=$0; sub(/^ /,"",thepath); + +# Print size and path (directories are bolded) + if ( system("[ -d \""thepath"\" ]") ) + printf "%-8s %s\n",hsize,thepath; + else + printf "%-8s \033[1m%s\033[0m/\n",hsize,thepath; +}' diff --git a/zsh/zsh.d/zshfunctions/hist b/zsh/zsh.d/zshfunctions/hist new file mode 100644 index 0000000..27e1b20 --- /dev/null +++ b/zsh/zsh.d/zshfunctions/hist @@ -0,0 +1 @@ +grep -i $* $HISTFILE