Skip to content

Instantly share code, notes, and snippets.

@apexskier
Last active May 8, 2017 18:45
Show Gist options
  • Save apexskier/36710c7e0f1538e8e74b to your computer and use it in GitHub Desktop.
Save apexskier/36710c7e0f1538e8e74b to your computer and use it in GitHub Desktop.
Dokuwiki Clean up
#!/usr/local/bin/bash
# find all files that should be start pages
for dir in `find . -type d`
do
file="${dir}.txt"
if [ `ls "${dir}.txt" 2> /dev/null ` ]
then
# generate the link to the file
dokulink=$(sed "s|^\./\(.*\)\.txt|\1|" <<< "${file}" | sed "s|/|:|g")
# replace all links to this file with one to a new start page
for f in `grep -rl "\[\[\s*${dokulink}" .`
do
if [[ "$f" != *.bak ]] # don't process backup files
then
regex="s/(\[\[\s*)(${dokulink})(\s*(?:\|.*)?\]\])/\1\2:start\3/g;"
# see if there's already a backup file
if [ -e "${f}.bak" ]
then
perl -pi -e $regex $f
else
perl -pi.bak -e $regex $f
# remove the backup file if there were no changes
if diff --brief "$f" "${f}.bak" > /dev/null
then
rm "${f}.bak"
fi
fi
fi
done
# move the file to it's new location (a start page)
newstart="${file/\.txt/}/start.txt"
if [ -e "$newstart" ]
then
echo "ALERT: start page already exists: $newstart"
else
echo "moving '$file' to '$newstart'"
mv $file $newstart
fi
fi
done
#!/usr/local/bin/bash
# in case of rm -r in the `data/pages` directory
# run from the `data` directory
# this is very quick and dirty
for file in $(find ./attic -type f)
do
newfile="pages/$(echo $file | sed "s|\./attic||" | sed "s|\.gz||" | sed "s|\.[0-9]\{10\}||")"
echo Restoring $newfile
gzip -d < $file > $newfile
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment