### Display Information for All Administrators Source: https://www.drush.org/12.5.2/commands/user_information This example demonstrates how to find and display information for all users with the 'administrator' role. It uses a subquery to get the UIDs of administrators. ```bash drush uinf --uid=$(drush sqlq "SELECT GROUP_CONCAT(entity_id) FROM user__roles WHERE roles_target_id = 'administrator'") ``` -------------------------------- ### Install Drupal with Specified Database URL Source: https://www.drush.org/12.5.2/commands/site_install Installs Drupal using the provided database connection details. This is required for initial installations. ```bash drush si --db-url=mysql://user:pass@localhost:port/dbname ``` -------------------------------- ### Install Drupal with Existing Configuration Source: https://www.drush.org/12.5.2/commands/site_install Installs Drupal and imports configuration from the 'sync' directory. This is useful for setting up a site based on existing configuration files. ```bash drush si --existing-config ``` -------------------------------- ### Install a Module Using --uri Option Source: https://www.drush.org/12.5.2/usage For multi-site installations, specify the target site using the '--uri' option when running commands like 'pm:install'. ```bash $ drush --uri=http://example.com pm:install ``` -------------------------------- ### Site Specification Example Source: https://www.drush.org/12.5.2/site-aliases An example of a site specification used directly on the command line, combining user, host, path, and URI. ```bash drush user@server/path/to/drupal#uri core:status ``` -------------------------------- ### Start Server with DNS Resolution and Path Source: https://www.drush.org/12.5.2/commands/runserver Starts a web server using DNS/rDNS to resolve the binding IP, specifying a port and a path to open in the browser. Use Ctrl-C to stop. ```bash drush rs --dns localhost:8888/user ``` -------------------------------- ### Start Server on Default Port with Root Path Source: https://www.drush.org/12.5.2/commands/runserver Starts a web server on the default IP and port, opening the root path ('/') in the browser. Use Ctrl-C to stop. ```bash drush rs / ``` -------------------------------- ### Start Web Server on IPv6 Localhost Source: https://www.drush.org/12.5.2/commands/runserver Starts a web server on the IPv6 localhost address and a specified port. Use Ctrl-C to stop. ```bash drush rs [::1]:80 ``` -------------------------------- ### Start Server with Port and Specific Path Source: https://www.drush.org/12.5.2/commands/runserver Starts a web server on localhost with a specified port and path, opening the path in the browser. Note the colon usage for port and path without an IP. Use Ctrl-C to stop. ```bash drush rs :9000/admin ``` -------------------------------- ### Install Drupal with SQLite Database Source: https://www.drush.org/12.5.2/commands/site_install Installs Drupal using a SQLite database file. The database file will be created at the specified path. ```bash drush si --db-url=sqlite://sites/example.com/files/.ht.sqlite ``` -------------------------------- ### Start Web Server on Specific IP and Port Source: https://www.drush.org/12.5.2/commands/runserver Starts a web server on a specific IP address and port. Use Ctrl-C to stop. ```bash drush rs 10.0.0.28:80 ``` -------------------------------- ### Start Web Server on Specific Port Source: https://www.drush.org/12.5.2/commands/runserver Starts a web server on localhost, binding to the specified port. Use Ctrl-C to stop. ```bash drush rs 8080 ``` -------------------------------- ### Install Drupal with In-Memory SQLite Database Source: https://www.drush.org/12.5.2/commands/site_install Installs Drupal using an in-memory SQLite database. This is useful for testing or temporary installations. ```bash drush si --db-url=sqlite://:memory: ``` -------------------------------- ### Static Installation: Local Completion File Source: https://www.drush.org/12.5.2/commands/completion Dump the completion script to a local file and source it manually or add it to your shell's startup configuration. ```bash *%command.full_name% > completion.sh ``` ```bash *source completion.sh ``` ```bash *source /path/to/completion.sh ``` -------------------------------- ### Install and Run All Drush Tests Source: https://www.drush.org/12.5.2/contribute/unish Clone the Drush repository, install dependencies, configure test settings, and run all test suites using Composer. ```bash git clone https://github.com/drush-ops/drush.git cd drush composer install composer test ``` -------------------------------- ### Generate Install File Source: https://www.drush.org/12.5.2/generators/install-file Use this command to generate a new install file using Drush. ```bash drush generate install-file ``` -------------------------------- ### Install Drupal with Umami Profile and Danish Language Source: https://www.drush.org/12.5.2/commands/site_install Installs Drupal using the Umami profile and sets the default language to Danish. Ensure the Danish language pack is available. ```bash drush si demo_umami --locale=da ``` -------------------------------- ### Static Installation: Global Completion File Source: https://www.drush.org/12.5.2/commands/completion Dump the completion script to a global file for system-wide availability. This requires root privileges and a shell restart. ```bash *%command.full_name% | sudo tee /etc/bash_completion.d/drush ``` -------------------------------- ### Dynamic Installation: Shell Configuration Source: https://www.drush.org/12.5.2/commands/completion Add an eval command to your shell's configuration file to dynamically load the completion script on shell startup. ```bash *eval "$(/home/runner/work/drush/drush/drush completion )" ``` -------------------------------- ### Command-specific Drush options for site:install Source: https://www.drush.org/12.5.2/using-drush-configuration Configure command-specific options for site:install, such as setting a predetermined username and password. ```yaml command: site: install: options: # Set a predetermined username and password when using site:install. account-name: 'alice' account-pass: 'secret' ``` -------------------------------- ### Run Drush Deploy with Verbose Logging and Auto-Accept Prompts Source: https://www.drush.org/12.5.2/commands/deploy This example demonstrates how to run the deploy command with verbose output and automatically accept all prompts. Use this for automated deployments or when you want detailed logs. ```bash drush deploy -v -y ``` -------------------------------- ### Copy Database Between Multisite Installations Source: https://www.drush.org/12.5.2/commands/sql_sync This command transfers the database between two multisite installations, identified by their paths (e.g., '#prod' and '#dev'). ```bash drush sql:sync #prod #dev ``` -------------------------------- ### Get Help for Views List Command Fields Source: https://www.drush.org/12.5.2/output-formats-filters Consult the help text for the 'views:list' command to discover all available fields. Use '--fields=*' to display all available fields. ```bash $ drush help views:list Get a list of all views in the system. Options: --fields=FIELDS Available fields: Machine name (machine-name), Name (label), Description (description), Status (status), Tag (tag) [default: "machine-name,label,description,status"] ``` -------------------------------- ### PSR4 Autoload Example for Drush Generators Source: https://www.drush.org/12.5.2/generators This example shows how to configure PSR4 autoloading for Drush generators within a custom library. Ensure the generator class namespace aligns with the PSR4 mapping. ```json { "autoload": { "psr-4": { "My\\Custom\\Library\\": "src" } } } ``` -------------------------------- ### Run Drush Tests with DDEV Source: https://www.drush.org/12.5.2/contribute/unish Start DDEV containers and execute a specific functional test using DDEV's exec command. ```bash ddev start ddev exec drupal composer functional -- --filter testUserRole ``` -------------------------------- ### Drush xhprof profiling settings Source: https://www.drush.org/12.5.2/using-drush-configuration Configure Drush to start profiling via xhprof/tideways and show a link to the run report. Includes options for profiling built-ins, CPU, and memory. ```yaml xh: # Start profiling via xhprof/tideways and show a link to the run report. link: http://xhprof.local # See https://github.com/drush-ops/drush/blob/12.x/src/Commands/core/XhprofCommands.php for more settings. profile-builtins: true profile-cpu: false profile-memory: false ``` -------------------------------- ### Kubernetes Site Alias Configuration Source: https://www.drush.org/12.5.2/site-aliases Configure a site alias for a Kubernetes environment. This example specifies the namespace, resource, and container for Drush to connect to. ```yaml prod: kubectl: namespace: 'my-drupal-namespace' resource: 'pods/my-drupal-pod' container: 'drupal' ``` -------------------------------- ### Execute Drush from Project Root Source: https://www.drush.org/12.5.2/install Run Drush commands by navigating to your project's root directory and using the vendor binary. This is the standard method after installing Drush via Composer. ```bash vendor/bin/drush ``` -------------------------------- ### Pretty print JSON output Source: https://www.drush.org/12.5.2/commands/jn_get Pipe the JSON output from the jn:get command to the 'jq' utility for pretty printing and advanced JSON manipulation. Ensure 'jq' is installed. ```bash drush jn:get jsonapi/node/article | jq ``` -------------------------------- ### Install Drupal and Disable Update Status Emails Source: https://www.drush.org/12.5.2/commands/site_install Installs Drupal and disables email notifications for update status during the installation process. This can prevent errors on servers without an MTA. ```bash drush si standard install_configure_form.enable_update_status_emails=NULL ``` -------------------------------- ### Show all available views Source: https://www.drush.org/12.5.2/commands/views_list Use this command to display a list of all views configured in the system. ```bash drush vl ``` -------------------------------- ### Equivalent Command with Full Options Source: https://www.drush.org/12.5.2/site-aliases This command demonstrates the equivalent of using a site alias, explicitly specifying the Drupal root and URI. ```bash $ drush --root=/path/to/drupal --uri=http://example.com status ``` -------------------------------- ### Create Database Using DB URL Source: https://www.drush.org/12.5.2/commands/sql_create Create a database using a full DB URL, which includes credentials and connection details. This is an alternative to setting individual options like --db-su and --db-su-pw. ```bash drush sql:create --db-su=root --db-su-pw=rootpassword --db-url="mysql://drupal_db_user:drupal_db_password@127.0.0.1/drupal_db" ``` -------------------------------- ### Create a new user account Source: https://www.drush.org/12.5.2/commands/user_create Use this command to create a new user with a specified username, email, and password. Ensure all required parameters are provided. ```bash drush user:create newuser --mail='person@example.com' --password='letmein' ``` -------------------------------- ### Interactively Choose a Configuration File to Edit Source: https://www.drush.org/12.5.2/commands/config_edit Run this command without arguments to be prompted to choose a configuration file from a list. Edits are imported after closing the editor. ```bash drush config:edit ``` -------------------------------- ### Navigate to Drupal Site Root Source: https://www.drush.org/12.5.2/commands/drupal_directory This command changes your current directory to the root of your Drupal installation. ```bash cd $(drush dd) ``` -------------------------------- ### Set Cron Last Run Timestamp Source: https://www.drush.org/12.5.2/commands/state_set This example demonstrates setting a timestamp for the last cron run. ```bash drush state:set system.cron_last 1406682882 --input-format=integer ``` -------------------------------- ### List Available Formats for a Command Source: https://www.drush.org/12.5.2/output-formats-filters Check the help text for a specific command to see the available output formats. The default format is usually key-value. ```bash $ drush help version Show drush version. Options: --format= Select output format. Available: json, string, var_export, yaml. Default is key-value. ``` -------------------------------- ### Get Status for All Migrations Source: https://www.drush.org/12.5.2/commands/migrate_status Retrieve the status for all available migrations. This is the default behavior when no arguments or options are provided. ```bash migrate:status ``` -------------------------------- ### Filter Drush Commands Source: https://www.drush.org/12.5.2/commands/list Filter the list of Drush commands to show only those matching a specific pattern, such as commands starting with 'devel_generate'. ```bash drush list --filter=devel_generate ``` -------------------------------- ### Set a Site Name Source: https://www.drush.org/12.5.2/commands/config_set Sets the 'name' configuration value for the 'system.site' object. ```bash drush config:set system.site name MySite ``` -------------------------------- ### Get Art Data Source: https://www.drush.org/12.5.2/examples/ArtCommands.php Retrieves the list of available artwork, utilizing a cache to ensure fast access on subsequent calls. ```php /** * Get a list of available artwork. Cache result for future fast access. */ protected function getArt(): array { if (!isset($this->arts)) { $this->arts = $this->findArt(); } return $this->arts; } ``` -------------------------------- ### Perform all migrations Source: https://www.drush.org/12.5.2/commands/migrate_import Use the --all option to execute all available migration processes. ```bash drush migrate:import --all ``` -------------------------------- ### Export Dutch Translations Source: https://www.drush.org/12.5.2/commands/locale_export Exports all Dutch translations to a .po file. Use this to get all available translations for a specific language. ```bash drush locale:export nl > nl.po ``` -------------------------------- ### Open SQL CLI Source: https://www.drush.org/12.5.2/commands/sql_cli Opens a SQL command-line interface using Drupal's database credentials. ```bash drush sql:cli ``` -------------------------------- ### Pass Extra Options to mysqldump Source: https://www.drush.org/12.5.2/commands/sql_dump Passes additional arguments directly to the mysqldump command. This example excludes schema creation statements. ```bash drush sql:dump --extra-dump=--no-data ``` -------------------------------- ### Get a list of articles as JSON Source: https://www.drush.org/12.5.2/commands/jn_get Execute a JSONAPI request to retrieve a list of articles. The output is returned in JSON format by default. ```bash drush jn:get jsonapi/node/article ``` -------------------------------- ### Import with limit and feedback frequency Source: https://www.drush.org/12.5.2/commands/migrate_import Configure both the --limit for the number of items and --feedback for the frequency of progress messages. ```bash drush migrate:import user --limit=50 --feedback=20 ``` -------------------------------- ### Create a Site Archive Source: https://www.drush.org/12.5.2/commands/archive_dump Creates a site archive file in the temporary directory. This archive includes code, database, and Drupal files. ```bash drush archive:dump ``` -------------------------------- ### Set a Front Page Path Source: https://www.drush.org/12.5.2/commands/config_set Sets the 'page.front' configuration value for the 'system.site' object to a specific URL path. ```bash drush config:set system.site page.front /path/to/page ``` -------------------------------- ### Get Help for a Specific Command Source: https://www.drush.org/12.5.2/usage Use the 'help' command to view available options and details for a specific Drush command, such as 'pm:list'. ```bash $ drush help pm:list ``` -------------------------------- ### Partial Config Import from Specific Source Source: https://www.drush.org/12.5.2/commands/config_import Perform a partial import from a specified directory, useful for importing only a few configuration files without deleting existing ones. ```bash drush config:import --partial --source=/app/config ``` -------------------------------- ### Basic Config Import Source: https://www.drush.org/12.5.2/commands/config_import Import configuration from the default config directory to update Drupal's configuration. ```bash drush config:import ``` -------------------------------- ### Get Maintenance Mode Status Source: https://www.drush.org/12.5.2/commands/maint_get Prints the current status of Drupal's maintenance mode. Returns 1 if enabled, 0 if disabled. ```bash drush maint:get ``` -------------------------------- ### Get Status for Specific Migrations Source: https://www.drush.org/12.5.2/commands/migrate_status Retrieve the status for only the 'classification' and 'article' migrations. This is useful for checking the status of a small, predefined set of migrations. ```bash migrate:status classification,article ``` -------------------------------- ### Display Configuration Items Only in Sync Directory Source: https://www.drush.org/12.5.2/commands/config_status This command shows configuration items that exist only in the sync directory and would be created in the active storage upon import. It allows you to preview new configuration before importing. ```bash drush config:status --state='Only in sync dir' --prefix=node.type. ``` -------------------------------- ### Basic Drush Command Structure Source: https://www.drush.org/12.5.2/usage This is the general syntax for running Drush commands. Use this to understand how to pass options, commands, and arguments. ```bash $ drush [options] [argument1] [argument2] ``` -------------------------------- ### Get Status Grouped by Tag Source: https://www.drush.org/12.5.2/commands/migrate_status Retrieve the status for all migrations, with the output grouped by their associated tags. This is useful for organizing and viewing migrations by category. ```bash migrate:status --tag ``` -------------------------------- ### Create a Field Interactively Source: https://www.drush.org/12.5.2/commands/field_create Use this command to create a new field by answering interactive prompts. This is useful for a guided field creation process. ```bash drush field:create ``` -------------------------------- ### Injecting Services with create() Method Source: https://www.drush.org/12.5.2/dependency-injection Use the create() method to inject services into command files not using Autowire. The Container passed should be of type Psr\Container\ContainerInterface. ```php class WootStaticFactoryCommands extends DrushCommands { protected $configFactory; protected function __construct($configFactory) { $this->configFactory = $configFactory; } public static function create(Psr\Container\ContainerInterface $container): self { return new static($container->get('config.factory')); } } ``` -------------------------------- ### Display Configuration Items Needing Synchronization Source: https://www.drush.org/12.5.2/commands/config_status Use this command to see which configuration items are different between the database and the exported configuration files. This is useful for understanding what needs to be imported or exported. ```bash drush config:status ``` -------------------------------- ### Create Database for Current Site Source: https://www.drush.org/12.5.2/commands/sql_create Use this command to create the database for the currently configured site. Ensure your Drush alias or environment is set up correctly. ```bash drush sql:create ``` -------------------------------- ### Docker Compose Site Alias Configuration Source: https://www.drush.org/12.5.2/site-aliases Configure a site alias for a Docker Compose environment. This example specifies the Docker service and execution options. ```yaml # File: mysite.site.yml local: This environment is an example of the DockerCompose transport. docker: service: drupal exec: options: --user USER stage: uri: http://stage.example.com root: /path/to/remote/drupal/root host: mystagingserver.myisp.com user: publisher os: Linux paths: - files: sites/mydrupalsite.com/files - custom: /my/custom/path command: sql: sync: options: no-dump: true dev: root: /path/to/docroot uri: https://dev.example.com ``` -------------------------------- ### Show Raw Command List Source: https://www.drush.org/12.5.2/commands/list Display a simple table of command names and descriptions without any additional formatting. This provides a clean, text-based overview. ```bash drush list --raw ``` -------------------------------- ### Get Drush Version in JSON Format Source: https://www.drush.org/12.5.2/output-formats-filters Use the --format=json option to retrieve the Drush version in JSON format. This is useful for machine-to-machine communication. ```bash $ drush version --format=json { "drush-version": "10.3.1" } ``` -------------------------------- ### Get Migration Status as JSON Source: https://www.drush.org/12.5.2/commands/migrate_status Retrieve a JSON-serialized list of migrations, where each item includes only the migration ID and its status. This is useful for programmatic consumption of migration data. ```bash ms --fields=id,status --format=json ``` -------------------------------- ### List All Configuration Names Source: https://www.drush.org/12.5.2/commands/config_status Retrieve a list of all configuration item names, regardless of their state. This is useful for auditing or when you need a comprehensive overview of all managed configuration. ```bash drush config:status --state=Any --format=list ``` -------------------------------- ### Get Status for Specific Tags Source: https://www.drush.org/12.5.2/commands/migrate_status Retrieve the status for migrations that are tagged with either 'user' or 'main_content'. This allows for focused status checks on specific migration groups. ```bash migrate:status --tag=user,main_content ``` -------------------------------- ### Generate Controller with Pre-filled Answers Source: https://www.drush.org/12.5.2/commands/generate Generates a controller class and pre-fills the first two questions in the generation wizard using the --answer option. ```bash drush generate controller --answer=Example --answer=example ``` -------------------------------- ### Display Identical Configuration Items Source: https://www.drush.org/12.5.2/commands/config_status Filter the output to show only configuration items that are identical in both the database and the sync directory. This confirms that the configuration is up-to-date. ```bash drush config:status --state=Identical ``` -------------------------------- ### Create Base Field Override Interactively Source: https://www.drush.org/12.5.2/commands/field_base-override-create Use this command to create a base field override by answering interactive prompts. This is useful for guided field creation. ```bash drush field:base-override-create ``` -------------------------------- ### Open SQL CLI with Extra Options Source: https://www.drush.org/12.5.2/commands/sql_cli Opens a SQL CLI and adds custom options to the connect string, such as skipping table information. ```bash drush sql:cli --extra=--progress-reports ``` -------------------------------- ### Complex Site Alias Configuration Source: https://www.drush.org/12.5.2/site-aliases Defines a comprehensive site alias with SSH options, custom Drush script path, environment variables, and command-specific settings for installation. ```yaml # File: remote.site.yml live: host: server.domain.com user: www-admin root: /other/path/to/drupal uri: http://example.com ssh: options: '-p 100' paths: drush-script: '/path/to/drush' env-vars: PATH: /bin:/usr/bin:/home/www-admin/.composer/vendor/bin DRUPAL_ENV: live command: site: install: options: admin-password: 'secret-secret' ``` -------------------------------- ### Evaluate PHP to Load and Print Node Title Source: https://www.drush.org/12.5.2/commands/php_eval Loads a Drupal node by its ID and prints its title. This is a basic example of interacting with Drupal's entity API. ```bash drush php:eval '$node = \Drupal\Entity\Node::load(1); print $node->getTitle();' ``` -------------------------------- ### Create User Entity with Specified Editor Source: https://www.drush.org/12.5.2/commands/entity_create Create a user entity and specify a text editor, such as Nano, for input. ```bash drush entity:create user user --editor=nano ``` -------------------------------- ### Archive with Excluded Code Paths Source: https://www.drush.org/12.5.2/commands/archive_dump Creates an archive, excluding specific files or patterns from the code. For example, excluding 'foo_bar.txt' and any 'settings.php' files within 'web/sites/*' subdirectories. ```bash drush archive:dump --exclude-code-paths=foo_bar.txt,web/sites/.+/settings.php --destination=/path/to/archive.tar.gz ``` -------------------------------- ### Execute SQL Query with Pretty Table Output Source: https://www.drush.org/12.5.2/commands/sql_query Executes a query and displays the results formatted as a table with borders and column headers. ```bash $(drush sql:connect) -e "SELECT * FROM users LIMIT 5;" ``` -------------------------------- ### Export Source Strings as Template Source: https://www.drush.org/12.5.2/commands/locale_export Exports all source strings as a .pot template file. Use this to create a base file for translators to work from. ```bash drush locale:export --template > drupal.pot ``` -------------------------------- ### Create a Site Archive to a Specific Destination Source: https://www.drush.org/12.5.2/commands/archive_dump Creates a specified archive file (e.g., /path/to/archive.tar.gz) containing code, database, and Drupal files. ```bash drush archive:dump --destination=/path/to/archive.tar.gz ``` -------------------------------- ### Filter core:requirements by title and severity Source: https://www.drush.org/12.5.2/output-formats-filters Combine multiple filter conditions using the logical AND operator (`&&`). This example filters by title using regex and severity using an exact match. ```bash drush core:requirements --filter='title~=#(php|gd)#i&&severity=warning' ``` -------------------------------- ### Archive with Extra Database Dump Options Source: https://www.drush.org/12.5.2/commands/archive_dump Creates an archive and passes additional options to the database dump command, such as '--no-data'. ```bash drush archive:dump --extra-dump=--no-data --destination=/path/to/archive.tar.gz ``` -------------------------------- ### Export Config from Production to Staging Source: https://www.drush.org/12.5.2/commands/config_pull Use this command to export configuration from a production environment and transfer it to a staging environment. ```bash drush config:pull @prod @stage ``` -------------------------------- ### Run Cron Only When Not in Maintenance Mode Source: https://www.drush.org/12.5.2/commands/maint_status This example demonstrates how to conditionally run the cron command only if Drupal is not in maintenance mode. It uses the exit code of maint:status to control the execution flow. ```bash drush maint:status && drush cron ``` -------------------------------- ### Get Raw List of Migration IDs Source: https://www.drush.org/12.5.2/commands/migrate_status Retrieve a raw, unformatted list containing only the IDs of all available migrations. The output format is forced to 'string' when using the --field option. ```bash migrate:status --field=id ``` -------------------------------- ### Import migrations by tag Source: https://www.drush.org/12.5.2/commands/migrate_import Use the --tag option to import migrations associated with specific tags. Multiple tags can be provided, separated by commas. ```bash drush migrate:import --tag=user,main_content ```