Skip to content

Instantly share code, notes, and snippets.

@pfrenssen
Last active December 21, 2016 20:29
Show Gist options
  • Save pfrenssen/1518473 to your computer and use it in GitHub Desktop.
Save pfrenssen/1518473 to your computer and use it in GitHub Desktop.
Get all paths to Drupal sites on a server
#!/bin/bash
# Author: Alan Ivey
# http://echodittolabs.org/blog/2011/01/are-your-drupal-sites-running-latest-core
# Default path.
WEBHOME=/var/www
# Default limit on subfolder depth to speed up the search.
MAX_DEPTH=6
function show_help {
echo -e "Usage: ./locatedrupal.sh [OPTIONS] [PATH]"
echo -e "Search the file system for Drupal installations. Default path: $WEBHOME\n\nOptions:"
echo -e "-h\t--help\t\tShow this help text."
echo -e "-v\t--version\tShow the Drupal version alongside the path."
echo -e "-m N\t--max-depth N\tLimit the search to a depth of N subfolders. Default: $MAX_DEPTH"
}
# Check command line arguments.
while [[ "$1" == -* ]]; do
case "$1" in
-h|--help|-\?) show_help; exit 0;;
-v|--version) SHOW_VERSION=true; shift;;
-m|--max-depth) shift; MAX_DEPTH=$1; shift;;
--) shift; break;;
-*) echo "invalid option: $1" 1>&2; show_help; exit 1;;
esac
done
# Use the given path if it exists, and strip a trailing slash.
if [ -n "$1" ]; then
WEBHOME=${1%/}
fi
if [ -n "$MAX_DEPTH" ]; then
MAX_DEPTH="-maxdepth $MAX_DEPTH"
fi
# Workaround to fix awk counting below, but it works.
WEBHOMECOUNT=$(($(echo "${WEBHOME}" | grep -o "/" | wc -l | sed s/\ //g)+2))
# Change maxdepth from 5 to some other value if your webroot is not being detected.
for i in $(find $WEBHOME $MAX_DEPTH -type f -name system.module); do
# Grab the version number from the system.module file.
# Ref. http://drupal.org/handbook/version-info
VERSION=$(grep "VERSION" ${i}|awk -F\' '{print $4}')
# Drupal 4 has the system.module file in a lower directory than Drupal 5+.
if [[ "$VERSION" == 4* ]]; then
SITE=${WEBHOME}/$(echo $i|awk -v count="$WEBHOMECOUNT" -F/ '{for(j=count;j<=NF-2;j++) \
printf $j"/"}' | sed 's/.$//g')
else
SITE=${WEBHOME}/$(echo $i|awk -v count="$WEBHOMECOUNT" -F/ '{for(j=count;j<=NF-3;j++) \
printf $j"/"}' | sed 's/.$//g')
# Drupal 7 keeps the version number in bootstrap.inc.
if [ -z "$VERSION" ]; then
VERSION=$(grep "define('VERSION'" ${SITE}/includes/bootstrap.inc \
| awk -F\' '{print $4}')
fi
# Drupal 8 stores the version number in Drupal.php.
if [ -z "$VERSION" ]; then
VERSION=$(grep "const VERSION =" ${SITE}/lib/Drupal.php | awk -F\' '{print $2}')
SITE=${WEBHOME}/$(echo $i|awk -v count="$WEBHOMECOUNT" -F/ '{for(j=count;j<=NF-4;j++) printf $j"/"}' | sed 's/.$//g')
fi
fi
if [ -n "$SHOW_VERSION" ]; then
echo $SITE - $VERSION
else
echo $SITE
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment