As a frontend developer, there is a myth that CLI is for engineers or for sys admins and to don't touch it, but really, Magento 2 provides a straight forward list of commands and they can save you a lot of time on your development workflow.

What is CLI?

CLI stands for Command Line Interface and it's stated that the first one was created and used in 1950-1960. At it's core, is used to interact with a computer either by writting lines of text or with scripts.

What do I need in order to run CLI commands?

Depending on your operating system, you can use:

  • Windows: Git Bash
  • MAC: iTerm2 Or any other software that you're comfortable with. First of all, you can see all the available commands by running: php bin/magento The result should be something like this but it can be different depending on your project:
    command [options] [arguments]
    --help (-h)           Display this help message
    --quiet (-q)          Do not output any message
    --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
    --version (-V)        Display this application version
    --ansi                Force ANSI output
    --no-ansi             Disable ANSI output
    --no-interaction (-n) Do not ask any interactive question
    Available commands:
    help                                      Displays help for a command
    list                                      Lists commands
    admin:user:create                         Creates an administrator
    admin:user:unlock                         Unlock Admin Account
    app:config:dump                           Create dump of application
    cache:clean                               Cleans cache type(s)
    cache:disable                             Disables cache type(s)
    cache:enable                              Enables cache type(s)
    cache:flush                               Flushes cache storage used by cache type(s)
    cache:status                              Checks cache status
    catalog:images:resize                     Creates resized product images
    catalog:product:attributes:cleanup        Removes unused product attributes.
    cron:run                                  Runs jobs by schedule
    customer:hash:upgrade                     Upgrade customer's hash according to the latest algorithm
    deploy:mode:set                           Set application mode.
    deploy:mode:show                          Displays current application mode.
    dev:source-theme:deploy                   Collects and publishes source files for theme.
    dev:tests:run                             Runs tests
    dev:urn-catalog:generate                  Generates the catalog of URNs to *.xsd mappings for the IDE to highlight xml.
    dev:xml:convert                           Converts XML file using XSL style sheets
    i18n:collect-phrases                      Discovers phrases in the codebase
    i18n:pack                                 Saves language package
    i18n:uninstall                            Uninstalls language packages
    indexer:info                              Shows allowed Indexers
    indexer:reindex                           Reindexes Data
    indexer:reset                             Resets indexer status to invalid
    indexer:set-mode                          Sets index mode type
    indexer:show-mode                         Shows Index Mode
    indexer:status                            Shows status of Indexer
    info:adminuri                             Displays the Magento Admin URI
    info:backups:list                         Prints list of available backup files
    info:currency:list                        Displays the list of available currencies
    info:dependencies:show-framework          Shows number of dependencies on Magento framework
    info:dependencies:show-modules            Shows number of dependencies between modules
    info:dependencies:show-modules-circular   Shows number of circular dependencies between modules
    info:language:list                        Displays the list of available language locales
    info:timezone:list                        Displays the list of available timezones
    maintenance:allow-ips                     Sets maintenance mode exempt IPs
    maintenance:disable                       Disables maintenance mode
    maintenance:enable                        Enables maintenance mode
    maintenance:status                        Displays maintenance mode status
    module:disable                            Disables specified modules
    module:enable                             Enables specified modules
    module:status                             Displays status of modules
    module:uninstall                          Uninstalls modules installed by composer
    queue:consumers:list                      List of MessageQueue consumers
    queue:consumers:start                     Start MessageQueue consumer
    sampledata:deploy                         Deploy sample data modules
    sampledata:remove                         Remove all sample data packages from composer.json
    setup:backup                              Takes backup of Magento Application code base, media and database
    setup:config:set                          Creates or modifies the deployment configuration
    setup:cron:run                            Runs cron job scheduled for setup application
    setup:db-data:upgrade                     Installs and upgrades data in the DB
    setup:db-schema:upgrade                   Installs and upgrades the DB schema
    setup:db:status                           Checks if DB schema or data requires upgrade
    setup:di:compile                          Generates DI configuration and all missing classes that can be auto-generated
    setup:install                             Installs the Magento application
    setup:performance:generate-fixtures       Generates fixtures
    setup:rollback                            Rolls back Magento Application codebase, media and database
    setup:static-content:deploy               Deploys static view files
    setup:store-config:set                    Installs the store configuration
    setup:uninstall                           Uninstalls the Magento application
    setup:upgrade                             Upgrades the Magento application, DB data, and schema
    theme:uninstall                           Uninstalls theme

    Now that we know all commands here we go with my favourite commands:

    1. Create a new admin
      php bin/magento admin:user:create --admin-user='Harry' --admin-password='Expelliarmus' --admin-email='' --admin-firstname='Harry' --admin-lastname='Potter' 
    2. Empty cache folders
      php bin/magento cache:clean 
      php bin/magento cache:flush
    3. Disable or enable cache
      php bin/magento cache:disable
      php bin/magento cache:enable
    4. Check what is the site current mode set as
      php bin/magento deploy:mode:show
    5. Change the mode to developer or production
      php bin/magento deploy:mode:set developer
      php bin/magento deploy:mode:set production
    6. Get the admin url
      php bin/magento info:adminuri
    7. Reindex All Data
      php bin/magento indexer:reindex
    8. Disable or enable a module
      php bin/magento module:enable {Module_Name}
      php bin/magento module:disable {Module_Name}
    9. Upgrade Magento 2 application
      php bin/magento setup:upgrade
    10. Deploy static files
      php bin/magento setup:static-content:deploy

      Bonus round

      Top 3 CLI commands, used while working on a Magento 2 project:

    11. Import DB from SQL dump
      mysql -u username -p database_name < file.sql
    12. Check file permissions in directory
      ls -la
    13. Edit file from CLI
      nano <filename>