Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

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
share|improve this answer
    
awesome thanks greg for the additional info :) (because I learn something too!) –  tenken Mar 2 at 3:18
add comment

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()
      );
    }
  }
  $dir_handle->next();
}

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 http://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)"
@8080
 Site path                     :  sites/8080.webtest2               
 File directory path           :  sites/8080.webtest2/files         
 Private file directory path   :  /var/www/drupal_private_files                   
@commons_3_dev
 Site path              :  sites/default                                          
 File directory path    :  sites/default/files                                    
@none
@w2
 Site path              :  sites/default                                          
 File directory path    :  sites/default/files 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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