I'm trying to add something to my shell that shows the current Drupal root value so that we can use drush use more, but the only way I can see to return the current Drupal root value is to run drush status which is quite slow

cd /path/to/drupalroot/sites/default
drush drupal-directory '%root'

On the old system I'm on right now, that clocks in at 3.5s, vs. 11.8s for drush status

If you are using site aliases, as tenken suggested, then you can do this:

drush sa --format=csv --fields=root
  • awesome thanks greg for the additional info :) (because I learn something too!)
    – tenken
    Mar 2 '14 at 3:18
  • 1
    In short: drush dd %root
    – kenorb
    Mar 24 '16 at 16:49
  • 1
    If you want to be really short, drush dd defaults to %root if no argument is given. Mar 24 '16 at 16:53

Sure. Instead of running drush status from a DocRoot of a Drupal site you can create a Drush Site Alias file.

The simplest example from that documentation file is:

 * Aliases are commonly used to define short names for
 * local or remote Drupal installations; however, an alias
 * is really nothing more than a collection of options.
 * A canonical alias named "dev" that points to a local
 * Drupal site named "dev.mydrupalsite.com" looks like this:
 *   $aliases['dev'] = array(
 *     'root' => '/path/to/drupal',
 *     'uri' => 'dev.mydrupalsite.com',
 *   );

You basically are hardcoding the filepaths. You can later use this known structure and data to lookup the site root paths when you need them.

If you want to automate building your site alias file you can look at this example tutorial (I am not the author) called Drush Pro Tips: Automatic Site Aliases by Rob at LastCallMedia. His example uses the following code to build an alias file across a structured project codebase:

$aliases = array();
$dir_handle = new DirectoryIterator('/var/www');
while($dir_handle->valid()) {
  if($dir_handle->isDir() && !$dir_handle->isDot()) {
    // Does this subdirectory contain a Drupal site?
    if(file_exists($dir_handle->getPathname() . '/sites/default/default.settings.php')) {
      $basename = $dir_handle->getBasename();
      $aliases[$basename] = array(
        'uri' => 'http://localhost/' . $basename,
        'root' => $dir_handle->getPathname()

Once you have a site alias file you can then use BASH commands to easily rip out the information from the alias array or show it to the screen. This approved answer from our own site on showing site alias information is an example of utilizing BASH to get at the alias information https://drupal.stackexchange.com/a/50563/3279

The final example shows how to show the site paths on the filesystem from the answer, you could further tweak his BASH commands into an alias that just returns the raw PATH for a given @alias if you wanted, the work is ~80% for you in his example:

    drush sa | xargs -I {}  sh -c "echo {}; drush {} st" | egrep "(@|path)"
 Site path                     :  sites/8080.webtest2               
 File directory path           :  sites/8080.webtest2/files         
 Private file directory path   :  /var/www/drupal_private_files                   
 Site path              :  sites/default                                          
 File directory path    :  sites/default/files                                    
 Site path              :  sites/default                                          
 File directory path    :  sites/default/files 

In Drupal 7 you can just print DRUPAL_ROOT constant, e.g.

drush ev "print DRUPAL_ROOT"

which obviously won't be faster than drush dd %root.

So to make it quicker, you need to use something else than Drush.

For example if your Drupal is under git repository, quicker solution for the script is to check top dir of your repository, e.g.

git rev-parse --show-toplevel 2> /dev/null

To look for the first folder containing index.php, try:

dirname $(find -L $PWD -name index.php -print -quit)

Basically DRUPAL_ROOT is where index.php exists which have those lines:

 * Root directory of Drupal installation.
define('DRUPAL_ROOT', getcwd());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.