Created
July 23, 2012 13:52
-
-
Save bjoseru/3163733 to your computer and use it in GitHub Desktop.
take given files and archive them in a pre-specified folder under descriptive names
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 | |
# Copyright (C) 2012 by Bjoern Rueffer | |
# take given files and put them into ~/Documents20xx under a | |
# descriptive name | |
date=`date +%m-%d` | |
destdir=~/Documents`date +%Y` | |
prefix="" | |
if [ ! \( -d "$destdir" \) ]; then | |
destdir=~/Documents\ `date +%Y` | |
fi | |
if [ ! \( -d "$destdir" \) ]; then | |
echo "error: expected destination folder unavailable" | |
exit 1 | |
fi | |
mvcmd="true" | |
help(){ | |
cat<<EOF | |
useage: $0 [-f] [-m "description"] [-p "prefix"] [-d destdir] file1.ext file2.ext ... | |
File given files into $destdir (can be overridden with -d), and rename | |
them into the following naming scheme: | |
prefix/$date description1.ext | |
prefix/$date description2.ext | |
prefix/$date description3.ext | |
Here prefix is the name of an optional subdirectory (defaults to ""). | |
If the optional description is not given then this naming scheme is | |
used: | |
prefix/$date file1.ext | |
prefix/$date file2.ext | |
prefix/$date file3.ext | |
The moving and renaming will only be performed if the "safety switch" | |
-f is given. | |
EOF | |
} | |
newname(){ # return new name for a given file and number | |
filename=`basename "$1"` | |
number="$2" | |
if [ "x$description" = "x" ]; then | |
basename="${filename%.*}"; | |
else | |
basename="$description"; | |
fi | |
ext="${filename##*.}"; | |
if [ $number -lt 2 ]; then | |
echo "$date $basename.$ext" | |
else | |
echo "$date $basename $number.$ext" | |
fi | |
} | |
if [ $# -lt 1 ]; then | |
help; | |
exit 0 | |
fi | |
for ((i=1;$#;i++)); do | |
file=$1; | |
if [ $file = -f ]; then | |
mvcmd="mv" | |
echo ">> actual file moving enabled" | |
shift 1 | |
i=$(($i - 1)); | |
elif [ $file = -m ]; then | |
shift 1 | |
description="$1" | |
shift 1 | |
i=$(($i - 1)); | |
echo ">> description set to $description" | |
elif [ $file = -p ]; then | |
shift 1 | |
prefix="${1%%/}"/ | |
shift 1 | |
i=$(($i - 1)); | |
echo ">> prefix set to $prefix" | |
elif [ $file = -d ]; then | |
shift 1 | |
destdir="${1%%/}" | |
shift 1 | |
i=$(($i - 1)); | |
echo ">> destdir set to $destdir" | |
else | |
echo $i | |
file="$1" | |
shift | |
echo `basename "$file"` "-->" `basename "$destdir"`/$prefix`newname "$file" $i` | |
if [ ! -d "$destdir"/"$prefix" ]; then | |
mkdir "$destdir"/"$prefix" | |
fi | |
if [ ! -d "$destdir"/"$prefix" ]; then | |
echo "Error: $destdir/$prefix does not exist and could not be created." | |
exit 1 | |
fi | |
$mvcmd "$file" "$destdir/$prefix`newname "$file" $i`"|| echo "error: mv command unsuccessful" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment