#! /bin/zsh # # this file: # https://dataswamp.org/~incal/conf/.zsh/files-fs [[ $(uname) == "SunOS" ]] && return alias cp="cp -i -L -r" alias dfh="df -h -T" alias mkdir="mkdir -p" alias mv="mv -i" alias rm="rm -r" file-age () { local f=$1 stat -c %w $f | cut -d ' ' -f 1 } remove-smaller () { local ext=$1 local size=${2:-1024k} find . -name "*.${ext}" -type f -size -${size} -delete } alias rms=remove-smaller system-wide-search () { local str=$1 sudo find / -xdev -type f -print0 | xargs -0 grep -H $str 2> /dev/null } watch-out () { watch -t -n 0.5 'df 2> /dev/null | grep /$' } biggest-files () { local num=${1:-10} local dir=${2:-./} local opts=-ahS if [ -w $dir ]; then du $opts $dir else sudo du $opts $dir 2> /dev/null fi | sort -h -r | head -n $num } alias bf=biggest-files find-symlinks () { local src=$1 find $src -type l } alias find-links=find-symlinks drop-dash-suffix () { local files=($@) local words=() local name local ext local suffix local new for f in $files; do name=${f:r} ext=${f:e} if [[ $name != ${name#*-} ]]; then words=("${(@s/-/)name}") words[-1]=() new=$words[1] words[1]=() for w in $words; do new=${new}-${w} done new=${new}.${ext} mv -u $f $new fi done } do-sth () { for i in TASC{A..Z}O3RJMV{A..Z}WDJKX{A..Z}ZM; do echo $i echo -n $i | md5sum | cut -d ' ' -f 1 done | tr '[:lower:]' '[:upper:]' | egrep -m 1 -B 1 'E903[A-Z0-9]{3}4DAB[A-Z0-9]{4}08[A-Z0-9]{5}51[A-Z0-9]80[A-Z0-9]{2}8A[A-Z0-9]' } find-duplicates () { local files=($@) md5sum $files 2> /dev/null | sort | awk '{ print $2,$1 }' | uniq -c -f1 | grep -v '^ *1 ' | tr -s " " | awk '{print $3}' } create-sized-file () { local size=$1 local name=${2:-rdm-$size} head -c $size < /dev/urandom > $name ls -b -l $name } alias {csf,sf}=create-sized-file touch () { local fs=($@) local last=$fs[-1] for f in $fs; do /usr/bin/touch $f done store-file-path $last } recent-files () { local hits=${1:-10} find -type f -printf "%C@ %p\n" | sort -rn | head -n $hits } alias newest-{files,file}=recent-files numf () { local pth=${1:-.} find $pth -type f | wc -l } dir-size () { local dirs=($@) local sudo_needed=0 for d in $dirs; do if [ ! -w $d ]; then sudo_needed=1 break fi done local opts='-hs' [ $# -gt 1 ] && opts+='c' if [[ $sudo_needed = 0 ]]; then du $opts $dirs else sudo du $opts $dirs 2> /dev/null fi } alias dirsize=dir-size list-directories () { local fs=($(ls -1d */)) dir-size $fs } identical-files () { local f1=$1 local f2=$2 cmp --silent $f1 $f2 && echo "identical" || echo "different" } clean-filename () { local fs=($@) local new_name local ext local buzz local buzzwords=( \ 1024x768 \ 1080p \ 1220-x-1080 \ 1220-x-1200 \ 1220x1080 \ 1220x1280 \ 1280x1024 \ 1280x800 \ 1280x960 \ 1280 \ 1440x900 \ 1600-1200 \ 1600x1200 \ 1600x900 \ 1920x1080 \ 1920x1200 \ 2012 \ 2560x1600 \ 3000x2000 \ 720p \ 768x1024 \ adults \ best \ blogsport \ com \ desktop \ download \ edit \ full \ gif \ hd \ hq \ images \ img \ jpg \ lastest \ mix \ video \ normal \ official \ original \ ori \ pack \ photo-shoot \ photo \ picture \ remix \ the \ tumblr \ version \ video \ wallpapers \ wallpaper \ wallpa \ wall \ widescreen \ wide \ www \ zaroo7 \ ) for f in $fs; do if [[ -f $f || -d $f ]]; then # downcase new_name=${f:l} new_name=${new_name:r} ext=${new_name:e} # buzzwords for buzz in $buzzwords; do new_name=${new_name//$buzz/} done # delimiters new_name=${new_name//[-:_ ]##/-} # initial dash new_name=${new_name/#-/} # non-alphanums new_name=${new_name//[^a-z0-9-]/} # extention [[ -n $ext ]] && new_name=${new_name}.${ext} # misc new_name=${new_name//-./.} new_name=${new_name//jpeg/jpg} [[ $f != $new_name ]] && mv $f $new_name 2> /dev/null fi done } count-word-in-files () { local word=$1 shift local files=($@) for f in $files; do (( $(grep $word < $f | wc -l) )) && echo $f done } count-files () { find ./ -type f | wc -l } alias files=count-files count-all () { find ./ | wc -l } most-recent-modify () { local num_files=${1:-10} /bin/ls -dt ${PWD}/* | head -n $num_files } alias mr=most-recent-modify most-recent-access () { local num_files=${1:-10} /bin/ls -du ${PWD}/* | head -n $num_files } alias mra=most-recent-access