Created
September 8, 2012 22:20
-
-
Save Kedrigern/3680325 to your computer and use it in GitHub Desktop.
Utility to clean/sort huge directories. For example for move all preview images (small files) from dir to another dir.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/bash | |
# Author: Ondřej Profant, 2012 | |
# Mail: ondrej.profant <> gmail.com | |
# Licence: GPL | |
# Utility to clean/sort huge directories. For example for move all preview images (small files) from dir to another dir. | |
LICENCE=GPL | |
VERSION=0.1 | |
DEFAULT_LIMIT=$((10 << 10)); #10kb | |
function help() { | |
echo -e "Parametrs: | |
\t-p\t--prefix\tDefine files prefix. Like \"tmp_\" | |
\t-s\t--sufix\t\tDefine files sufiix. Like \".jpg\" | |
\t-l\t--limit\t\tDefine files maximum size in bytes. Default value is 10kb. | |
\t-d\t--do\t\tDefine command - what to do. Use shell basic utility like echo, mv, rm, touch. | |
\t-v\t--version\tShow version. | |
\t-h\t--help\t\tShow this help. | |
Author: Ondřej Profant, 2012 | |
Mail: ondrej.profant <> gmail.com | |
Licence: $LICENCE"; | |
}; | |
function prepare_size() { | |
unit=${1##*[0-9]} # crop start number | |
value=${1%[kKmMgG][bB]}; # number itself | |
case $unit in | |
kb | Kb ) | |
size=$(( $value << 10 )); | |
;; | |
mb | Mb ) | |
size=$(( $value << 20 )); | |
;; | |
gb | Gb ) | |
size=$(( $value << 30 )); | |
;; | |
* | b | B ) | |
size=${value} | |
;; | |
esac; | |
echo $size | |
} | |
size_limit=$DEFAULT_LIMIT; | |
do=echo | |
while [ $# -gt 0 ]; do | |
case $1 in | |
-p | --prefix ) | |
prefix=$2; | |
shift 2; | |
;; | |
-s | --sufix ) | |
sufix=$2; | |
shift 2; | |
;; | |
-l | --limit | --size-limit ) | |
size_limit=$( prepare_size $2 ); | |
shift 2; | |
;; | |
-d | --do ) | |
do=$2; | |
shift 2; | |
;; | |
-h | --help ) | |
help; | |
exit 0; | |
;; | |
-v | --version ) | |
echo "Version $VERSION"; | |
exit 0; | |
;; | |
* ) | |
echo "Unknow parametr $1. Use help ($0 --help) for usage."; | |
exit 0; | |
;; | |
esac; | |
done; | |
for i in ${prefix}*${sufix}; do | |
if [ -d "$i" ]; then continue; fi; # jump if dir | |
if [ "$0" = "./$i" ]; then continue; fi; # jump if this script | |
if [ "$0" = "$i" ]; then continue; fi; # jump if this script | |
size=$( ls -l $i | cut -d " " -f 5 ) ; # size of the file (bytes) | |
if [ $size -lt $size_limit ]; then | |
$do $i ; | |
fi; | |
done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment