Skip to content

Instantly share code, notes, and snippets.

@gerhardqux
Last active February 8, 2018 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerhardqux/3145065972e2b528d0df810d1caaf125 to your computer and use it in GitHub Desktop.
Save gerhardqux/3145065972e2b528d0df810d1caaf125 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Usage:
# arcive [-t] [-l] prj1 prj2 prj3 ...
#
# Will execute:
# mkdir -p /home/user/arc/2014
# mv prj1 /home/user/arc/2014/20141201-prj1
# mkdir -p /home/user/arc/2015
# mv prj2 /home/user/arc/2015/20151207-prj2
# mv prj3 /home/user/arc/2015/20151209-prj3
LNK=0
while [ $# -gt 0 ]
do
case "$1" in
-l) LNK=1
echo "# Leaving symlinks from the old location to the new location"
;;
-t) TEST="echo "
echo "# Dry run, not actually archiving"
;;
*) FILENAME=$(basename "$1")
DIRNAME=$(dirname "$1")
if [ ! -e "${DIRNAME}"/"${FILENAME}" ]; then
echo "# File does not exist: $DIRNAME/$FILENAME"
fi
YEAR=$(date +%Y -r "$1")
STAMP=$(date +%Y%m%d -r "$1")
ARCHIVE_DIR=$HOME/arc/$YEAR
if [ ! -d "$ARCHIVE_DIR" ]; then
$TEST mkdir -p "$ARCHIVE_DIR"
fi
$TEST mv "$1" "$ARCHIVE_DIR"/$STAMP-"$FILENAME"
if [ ${LNK} -eq 1 ]; then
$TEST ln -s "$ARCHIVE_DIR"/$STAMP-"$FILENAME" "$1"
fi
;;
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment