Skip to content

Basic Command Line Tools

Richard Frank edited this page Jul 23, 2021 · 2 revisions

This ticket will explain some key commands needed to work with your development environment. Typically developers work within a Unix-like environment like MacOSX or Windows WSL or Linux and these commands should all work in these environments.

Understanding pipes

Unix philosophy is to have small programs that do one thing really well, and the to combine them to get your desired effect

Combining scripts uses pipes like this | to redirect the output of one script into the input of the next script. For instance taking a database dump and pipeing it through the grep command to find a phrase. e.g.

mysqdump database_name | grep "password"

Database commands

Typically you will want to grab a remote database and use it locally to continue maintaining a website

Dump a database from remote mysql server (with SSH hop)

This is if the mysql does not allow remote connections. Here we run a remote command on the server that outputs to

ssh root@www.example.com "mysqldump -uUSERNAME -pPASSWORD -hHOST_NAME" > db.sql

Dump a database from remote mysql server (without SSH hop)

mysqldump -uUSERNAME -pPASSWORD -hHOST_NAME > db.sql

Dump a database and import it in one command

Here we log into the server, output the mysqldump and pipe the resulting mysql command directly to the database

ssh root@www.example.com "mysqldump -uUSERNAME -pPASSWORD -hHOST_NAME" | mysql -uUSERNAME -pPASSWORD database_name

Dump a database and import it into another remote database

Taking it to the extreme, this is quite dangerous and should not be done on production databases without backups

ssh root@www.example.com "mysqldump -uUSERNAME -pPASSWORD -hHOST_NAME" | ssh root@www.otherhost.com "mysql -uUSERNAME -pPASSWORD database_name"

Tip if you don't want to have to type the username and password for the mysql command, you can add this to a .my.cnf file locally or on the relevant remote server, e.g.

# contents of ~/.my.cnf 
[mysql]
user=root
host=localhost
password='xyz'
socket=/var/run/mysqld/mysqld.sock

Copying files commands

Rsync remote contents locally

rsync -avz root@www.remotehost.com:/var/www/examplesite/public/uploads/. ~/webserver/www/examplesite/public/uploads/.

Rsync local contents remotely

Proceed with caution, if the same file exists it will overwrite that file

rsync -avz ~/webserver/www/examplesite/public/uploads/. root@www.remotehost.com:/var/www/examplesite/public/uploads/.

Virtual hosts

When you need to point your local server to your site you typically need to create a vhost file (or similar). This is sometimes done automatically with tools like MAMP or a script, but learning how to do this manually will allow you to manage your own server and production servers

https://linuxconfig.org/apache-ip-and-name-based-virtual-hosts-explained

PHP

PHP is either compiled as an Apache module or run as a separate service (FPM). Running PHP has an FPM service has advantages because you can swap out PHP version a little easier. Out the box tutorials typically use PHP as a module.

Remember PHP is also run via the command like using php commands. Console tools like composer artisan and ./craft are just PHP files that are run using PHP CLI.

If you want to set up a Mac from scratch to use PHP/Apache follow this tutorial https://getgrav.org/blog/macos-bigsur-apache-multiple-php-versions

There are several other ways of running a local development setup. See Laravel Homestead, Laravel Valet, Laravel Sail etc. Do not be dissuaded by the term "laravel", these tools work with all PHP projects.

https://aschmelyun.com/blog/the-current-state-of-local-laravel-development/

Using docker as a local development environment on Mac can be slow, so watch out for docker based solutions. TBC.

Symlinking

Symlinking is like making a shortcut to another directory. This is useful if you don't want to make duplicate copies of files.

ln -s /source/path /target/path

Vim

All servers typically have vim or vi installed by default, so having skills to navigate and edit a text file using vim is very useful. https://www.openvim.com/

Spending a few hours getting muscle memory in vim will give you a lifetime of productivity on your local environment and servers.

vim filename.txt

Tail

When monitoring logs, tail is invaluable. The follow command (-f) is all you need in 99% of situations.

tail -f filename.txt

Find and Grep

Quick way to find a file in a tree:

find . | grep "filename"

Quick way to find a file with a particular text string in a tree of files The r flag means "recursive" so the command traverses the entire tree of files

grep -r "text"