Jay Gould

Terminal cheat sheet and CLI tools for web developers

August 26, 2020

Terminal header image

As developers we spend a lot of time hacking away in the command line, whether it’s embedded in VS Code, a separate app like iTerm, or the good old standard Mac Terminal. This post is a collection of commands or tools which make using the CLI a better experience.

The post covers basic CLI commands which most people will use on a daily basis, plus some extras which I don’t use too often but are invaluable at the right time.

Basic navigation

  • ls - List a directory contents.
  • ls -al - List a directory contents containing hidden folders and more file/directory metadata.
  • pwd - Prints the current, full, absolute directory on screen.
  • cd some-folder - Change directory to another location if location exists.
  • cd .. - Go up/back a directory.
  • cd ~/ - Go to the “home” directory which contains useful settings and files such as SSH keys and ZSH settings (more on that later).
  • cd and TAB - The tab key can be used to autocomplete a file or directory in the current directory. This is useful to get around quickly without having to type the whole file or directory name.
  • open . - Open the current directory.

Opening and editing files

The cat command can be used to open and edit files:

  • cat an-existing-file.txt - View contents of file in terminal.
  • cat an-existing-file.txt another-existing-file.txt - View contents of both files in terminal.
  • cat > a-new-file.txt - Create a new file and begin writing to it in terminal.
  • cat an-existing-file.txt >> another-existing-file.txt - Append the contents of one file to another file. nano or vim = view and edit

The nano command can also be used to open and edit files, however nano is more of a terminal editor than cat with extra functionality such as going to specific line:

  • nano an-existing-file.txt - Open contents of file in terminal editor.
  • nano a-new-file.txt - Create a new file an open in the terminal editor.
  • Pressing keys ctrl+x - Exit the terminal editor and ask to save if there are changes.
  • Pressing keys ctrl+c - See current line number and current character number.
  • Pressing keys ctrl+w - Search file for characters matching input.

Using “sudo”

The sudo command is short for “superuser do”, and allows any command to be executed as the machine superuser. This command is often required when trying to perform actions that the current user doesn’t have permissions to use. By default, Apple’s Linux automatically has you logged in as a regular user, which is why sudo is required sometimes.

  • sudo nano a-secure-file.txt - Open a file as superuser, allowing you to save the file afterwards which may not be possible as a regular user.

You can run commands which mean you don’t need to use sudo, but this should only be used if you know what you’re doing as sudo is there for a reason. You don’t want to be running all commands as a superuser as you may break something unintended and having the block there can be helpful.

  • sudo chown your-username a-secure-file.txt - Sets owner of the file to your current user. This can also be done to a directory.
  • sudo -s - Changes your current user to the root user of the machine. Allows root privileges for all commands, meaning you don’t need to run sudo.
  • su [regular-user] - Changes back to your regular user.

In most cases it’s much better and safer to just use sudo.

Using zsh settings with .zprofile and .zshrc

Now that Apple sets zsh as the default shell in MacOS, this section will be specific to zsh rather than bash.

  • ~/.zprofile - This file is ran at login on most Linux systems, which is when the linux account is first logged in. However on a Mac, this file is ran each time a new Terminal window is opened.
  • ~/.zshrc - This file runs each time a new Terminal window is opened.

The above two files don’t exist on a Mac by default, so they must be created to be used. For the purposes of this post, we’ll be using the .zshrc file only.

There’s usually some command line witchcraft sourcing involved with non-Mac Linux systems whereby you can source the .zshrc file from the .zprofile file to ensure both files are ran when a new Terminal window is opened, however this is not required on a Mac as both files are ran each time a new window is opened. So for the sake of keeping things simple, they are very similar to the point that they can be used interchangeably for most people.

The ~/.zshrc file can contain aliases. Here’s a few of my favourites:

alias q='exit'
alias lsa='ls -al'
alias c='clear'
alias goprojects='cd /users/[user]/desktop/[projects]' # Go to the location of your web projects
alias sshEc2='cd ~/.ssh && ssh -i "key.pem" some-long-ass-aws-url-which-is-better-in-this-file.aws.com' # SSH in to your EC2 box easily

Once the file has been changed, you’ll need to run source ~/.zshrc or open a new shell window to see the changes.

Pro tip: alias - Lists all current aliases just in case you forget :)

Updating and using the PATH variable

The PATH variable is a collection of absolute URI’s on your system which are hit when you enter a command. It means you can run a command where ever you are on your machine without having to run the full URI to the command. For example, when you install something like MAMP, there are separate versions of MySQL and PHP installed directly to MAMP. Updating your path to point to these locations mean you can run their versions of software anywhere on your machine.

So in the context of MAMP:

  • export PATH=\$PATH:/Applications/MAMP/Library/bin - Adds MAMP binaries to be ran from anywhere on your machine. This can be added to the ~/.zshrc as mentioned in the previous section.

SSH keys

SSH keys enable secure and efficient authentication to remote servers via SSH. There’s usually a key pair which consists of a public and private key. The private key is kept on your machine which should be kept secure, and the public key resides on the remote server. A simple SSH command is then used to log you in without having to enter a password.

  • ls -al ~/.ssh - Check if you have existing SSH keys. This is the usual location of SSH keys.

Create new ssh key (ssh-keygen should already be installed):

  • ssh-keygen -t rsa - Create new SSH key pair. Most cloud services like AWS or Heroku have their own SSH key process so you might not need to create your own. I’ve used this before mainly with cPanel servers.
  • ssh some-awesome-server.com - Log in to the remote server via SSH (requires SSH keys to be set up).

NPM

  • npm init - Initialize a project with NPM.
  • npm install - Installs packages listed in the package.json file (must be in same dir as that file).
  • npm install [package-name] - Installs a package to be added to your node_modules directory and the package.json file.
  • npm uninstall [package-name] - Uninstalls the package, removing it from node_modules and the package.json file.

Aside from NPM, npx executes a binary from NPM directly from the internet rather than having to install on your machine. This is useful for one-off commands.

  • npx pretty-quick --check && npx pretty-quick - Uses prettier to format files quickly.
  • npx depcheck - Searches codebase and flags any packages which are in your package.json file but are not being used. Great to run ever now and then to keep code clean.
  • npx sort-package-json - Alphabetises the package.json file so all packages and scripts are in order.
  • npx create-react-app [app-name] - Uses CRA to generate a new project without having to install CRA.

Encrypting files with GPG

GPG is an implementation of the PGP (Pretty Good Privacy) standard, allowing fast and very secure encryption of files. It’s great for securely sending sensitive data over file transfer sites or email if there’s no alternative sending method. A private, secure file transfer service is best used in conjunction with this method for better security.

If not done so already, download the GPG Suite and install.

  • gpg --gen-key - Generates a new public and private key which is used to encrypt and decrypt files.
  • gpg --list-keys - Lists keys on your system. The keys are stored in ~/.gnupg but it’s easier to list them using list keys command.
  • gpg --list-secret-keys - Lists secret keys on your system.
  • gpg --encrypt -r [email protected] - Encrypts a file which can only be opened by the defined recipient address (-r). You can test with your own email address here first. The email address will reference the public key on your system, so you should have your own one already. This file can only be decrypted with the recipients private key.
  • gpg --encrypt --sign -r [email protected] - Encrypts a file which can only be decrypted if you have the recipients private key, and the senders public key. More secure as it requires both parties to be involved in the process.
  • gpg --decrypt encrypted-file.gpg - Decrypts an encrypted file. Automatically looks for the correct private key which you will have if you’ve been sent this file specifically and it’s been encrypted with your public key. Requires you to enter the password for your private key.
  • gpg --decrypt encrypted-file.gpg > decrypted-file.txt - Same as above but outputs to a file.
  • gpg --import public.key - Import a public key to your system. This must be done before you encrypt a file using someone elses public key (defined by the -r param which associates the recipient email address to their key).

Most of the GPG functionality can be done using the GPG Suite UI program, but it’s worth knowing how to use CLI alternative.

Random command line gems

  • curl http://wttr.in/ - Get a nice looking text based weather report straight to your CLI tool, with colours and text-images.
  • cal - Open a calendar.
  • nc towel.blinkenlights.nl 23 - Watch an ASCII version of Star Wars - this must have taken a long time to make!
  • history - See full history of CLI commands you’ve entered.

Supercharging your terminal with Oh My Zsh

Oh My Zsh is a powerful and popular CLI framework, allowing all sorts of incredible functionality. Visit the site and install:

Install Oh My Zsh:

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

If you have any custom config in your ~/.zshrc file this will now be overridden with the installation config, but don’t worry! It’s still accessible in another file:

  • sudo nano ~/.oldzshrc - The exact file may be different, so cd in to the home directory and list all files. There will be your old .zshrc file there somewhere.
  • source ~/.zshrc - Re-initialise the terminal window so new setup comes in to effect.

Install a great looking theme to Oh My Zsh by visiting the GitHub for Powerlevel10k. This requires installing a font for which ever Terminal app you use and installing the theme directly to the Oh My Zsh settings, but it’s totally worth the effort:

Powerlevel10k terminal theme


Jay Gould

Senior Web Developer at Day Out With The Kids