Notes about Drush version 7.
Index
Installation
See my drush installation notes.
Commands
To list all available commands:
drush help
Further help for a [command]:
drush help [command]
E.g. drush help cc
to view help for the cache-clear (cc) command.
To list commands provided by [module]:
drush --filter=[modules]
E.g. drush --filter=core
to view core commands.
Also see the Drush commands reference.
Commands I use often
Or forget often!
Command | Shortcut | Description |
---|---|---|
pm-list | List all modules, whether enabled or not | |
pm-update | up | Update everything |
pm-update [modules] | up | Update only the given [modules] |
update-status | ups | Get list of available updates |
pm-refresh | rf | Refresh available updates data/cache |
l10n-update | Update available translations (l10n_update module required) | |
l10n-update-refresh | Refresh available translations (110n_update module required) | |
role-list | rls | List roles. This can be used to list permissions of a role |
features-components | fc | See all features components i.e. all types of stuff that you can export to a feature |
features-export [feature] [components] | fe | Export components to a feature module |
features-diff | fd | List all feature modules along with their state, to see which ones have changed |
features-diff [feature] | fd | See differences between [feature] module and database |
features-revert [feature] | fr | Update the database to be the same as the feature module |
features-update [feature] | fu | Update the feature module with the latest from the database |
–filter=[module] | List available commands for [module]. See the list commands section. | |
php-script | scr | Run a PHP script. See running scripts section. |
References
- drush command to show available commands per module
- Drush commands reference.
- Import po-files using Drush
Multisite
To execute a command for all sites in a multi-site setup, use the @sites
flag, e.g:
me@pc ~/clients/foobar/dev/website/drupal $ drush @sites rf
You are about to execute 'rf' non-interactively (--yes forced) on all of the following targets:
/home/steph/clients/foobar/dev/website/drupal#foobar-za
/home/steph/clients/foobar/dev/website/drupal#foobar-uk
/home/steph/clients/foobar/dev/website/drupal#default
Continue? (y/n): y
.home.steph.clients.foobar.dev.website.drupal#default >> Refreshing update status information ...
.home.steph.clients.foobar.dev.website.drupal#default >> Done.
.home.steph.clients.foobar.dev.website.drupal#foobar-uk >> Refreshing update status information ...
.home.steph.clients.foobar.dev.website.drupal#foobar-uk >> Done.
.home.steph.clients.foobar.dev.website.drupal#foobar-za >> Refreshing update status information ...
.home.steph.clients.foobar.dev.website.drupal#foobar-za >> Done.
As it says, this is non-interactive, so after inputting ‘y’ it will run the command for each site without asking you anything - it will assume an answer of ‘y’ for every question it would have asked in interactive mode.
Note that it is a bit flakey. E.g. When running drush @sites up
I’ve noticed that it sometimes fails to run database updates and I’ve had to then run drush @sites updb
to make sure the updates were properly made. Best to check your sites’ status report after running any updates.
Running scripts
Drush can run PHP scripts inside a bootstrapped Drupal environment.
E.g. lets say we have this file called pages-by-219.php, which shows the titles of all pages published by user uid 219:
<?php
// Load all published pages by uid 219 and show their titles.
$query = new EntityFieldQuery;
$result = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'page')
->propertyCondition('status', 1)
->propertyCondition('uid', 219)
->execute();
$nodes = node_load_multiple(array_keys($result['node']));
print_r(array_map(function ($n) {return $n->title;}, $nodes));
We can run this code via drush:
drush scr pages-by-219.php
With the following output:
Array
(
[293] => Toast to Summer
[294] => What's Cooking Next Door
[320] => Seafood Sandwich Cakes
[321] => Meringue Roll with Berries
)
Features how tos
Export to a non standard destination
If you don’t keep your feature modules in sites/all/modules then you can set a destination with the --destination
flag:
drush fe --destination=sites/all/modules/custom_modules/features my_feature [things to export]
Export all roles and their permissions
drush fe --destination=sites/all/modules/custom/features roles_and_permissions user_role:* user_permission:*
Export a role and its permissions
The following command will export all permissions of ‘the role’ in to a feature called the_feature:
drush rls 'the role' --pipe | xargs -L 1 -I {} drush fe the_feature 'user_permission:{}' -y
It works by getting a list of a role’s permissions with the following command…
drush rls 'the role'
…then using xargs to export each permission via this command:
drush fe the_feature user_permission:the permission
Reference: Drush: export permissions of a role
Bugs / gotchas
Don’t use tilde in drush aliases configuration
When you define directory locations in your alias configuration (in ~/.drush/aliases.drushrc.php) use the full expanded path to your home directory e.g. /home/myuser/whatever instead of ~/whatever.
I found that if you use the tilde character (which bash normally interprets as the location of the home directory) then drush won’t work properly.
For example, drush @local sql-conf
returned nothing when I used tilde. Without tilde it returned my sql config.
No code updates available
If you get ‘no code updates available’ when running drush up
then try the following:
- Refresh its update status information with
drush rf
- Restart your database server.