Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@openjck
Last active January 18, 2016 01:21
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 openjck/e4ff62090702b01e04d9 to your computer and use it in GitHub Desktop.
Save openjck/e4ff62090702b01e04d9 to your computer and use it in GitHub Desktop.
Script that restores original filenames (misc/drupal/index.js -> misc/drupal.js) after following the steps in this article: http://webikon.com/cases/park-your-old-drupal-site
#!/bin/bash
# Fix filenames after making a static copy of a Drupal website. This script
# should only be run after following all of the steps in this article:
#
# http://webikon.com/cases/park-your-old-drupal-site
#
# When the steps in the article are followed, all files are renamed to
# index.[ext], so you end up with a bunch of files like misc/drupal.js.
# This script restores the original filenames (e.g., misc/drupal.js), and
# updates all references accordingly.
#
# Be sure to modify the settings below. This script has only been tested on
# Linux. YMMV.
############
# Settings #
############
WEBROOT='/path/to/static/copy'
##########
# Script #
##########
# When iterating, consider newlines to be item separators. This allows us to
# iterate over files that contain spaces and handle them as expected.
IFS=$'\n'
WRONG_FILENAMES=`find $WEBROOT -type f -name 'index.*' -not -name 'index.html'`
for WRONG_FILENAME in $WRONG_FILENAMES; do
CORRECT_BASENAME=$(dirname $WRONG_FILENAME)
CORRECT_FILENAME=`echo $WRONG_FILENAME | sed 's/\/index\\.\(.*\)/.\1/g'`
mv $WRONG_FILENAME $CORRECT_FILENAME
# Hide errors because some directories have mutliple index files (like
# index.js and index.css) and rmdir will complain until all index files have
# been converted.
rmdir $CORRECT_BASENAME &> /dev/null
done
# Update all references to the files that were just renamed
find $WEBROOT -type f -exec sed -i 's/\/index\.\([a-zA-Z0-9]*\)/.\1/g' {} \;
find $WEBROOT -type f \( -name '*.css' -o -name '*.js' \) -exec sed -i 's/\.\.\///' {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment