Added function 'bak'. Splitted other functions

This commit is contained in:
Andrea Mistrali 2013-01-25 15:31:13 +00:00
parent 39f83930ab
commit 1915a3edf9
6 changed files with 158 additions and 22 deletions

View File

@ -2,33 +2,18 @@
# Functions # Functions
# cdb - Goes to folder by complete path # cdb - Goes to folder by complete path
function cdb { cd `dirname $1` } autoload -U cdb
# hist - Grep from history # hist - Grep from history
function hist { grep -i $* $HISTFILE } autoload -U hist
# hdu - Human readable report of files and directories sizes # hdu - Human readable report of files and directories sizes
# *Same behaviour as du* autoload -U hdu
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);
# Remove $1 (size), then removes " " at the start of $0 # dust - list total size in . directory
$1=""; thepath=$0; sub(/^ /,"",thepath); autoload -U dust
# Print size and path (directories are bolded) # bak - remove backup files
if ( system("[ -d \""thepath"\" ]") ) autoload -U bak
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 * }
# vim: set ts=4 sw=4 tw=0 ft=zsh : # vim: set ts=4 sw=4 tw=0 ft=zsh :

131
zsh/zsh.d/zshfunctions/bak Normal file
View File

@ -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 :

View File

@ -0,0 +1 @@
cd `dirname $1`

View File

@ -0,0 +1 @@
hdu -s *

View File

@ -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;
}'

View File

@ -0,0 +1 @@
grep -i $* $HISTFILE