#!/bin/bash # mg - minion gather # Creates a new file $HOME/Journal/YYYY/MM-topic.md with all contents from all matches for all terms # Optionally include a second search term year=`date +%Y` month=`date +%m` # File to gather all content into topic=$1 if [ ! -n "$1" ]; then echo "A topic to gather is required." exit 1 fi mkdir -p "$HOME/Journal/$year" fname="$HOME/Journal/$year/$month-$topic.md" archive="$HOME/Journal/archive-$year/" # Files to gather files=$(find $HOME/Journal/$year -type f -iname "*$1*\.md") if [ -n "$files" ]; then echo "Will merge $files into $fname" else echo "Nothing to gather for $topic" fi # optional - more files to gather if [ -n "$2" ]; then more_files=$(find $HOME/Journal/$year -type f -iname "*$2*\.md") fi if [ -n "$more_files" ]; then echo "Will merge $more_files into $fname" else echo "No second keyword." fi echo "" read -n1 -p "Continue? [y,N]" input echo "" if [[ ! $input == "Y" && ! $input == "y" ]]; then echo "Aborted by user" exit 1 fi for f in $files; do echo "Gathering $f into $fname" cat "$f" >> "$fname.gathered" done mkdir -p $archive mv $files $archive if [ -n "$more_files" ]; then for f in $more_files; do echo "Gathering $f into $fname" cat "$f" >> "$fname.gathered" done mv $more_files $archive fi mv $fname.gathered $fname if [ -z ${EDITOR} ]; then echo "EDITOR unset, defaulting to Nano" export EDITOR=nano fi $EDITOR $fname