Blog Post

Introduction to Terminal Commands

May 17, 2022

Category: Command Line

Tags: cli npm terminal

This post will introduce you to basic command line commands. I’ll keep explanations short so this acts as more of a cheat sheet.

Note: I’m assuming you are running a zsh shell. I’ve also installed oh my zsh, although most of the commands below do not rely upon it. For help setting up your terminal, see my video overview here.

These basic commands help you move around your system.

  • pwd: Prints the current working directory.
  • cd: Change the current working directory.
  • .: Current working directory (oh-my-zsh).
  • ..: Parent working directory (oh-my-zsh).
  • : Grandparent working directory (oh-my-zsh).
  • ls: Lists the contents of the current working directory.
  • -: Toggle to the previous directory (oh-my-zsh).
  • code .: Opens the current working directory in VSCode.
  • open .: Opens the current working directory in the Finder (use explorer . for Windows).

Creating and Deleting

  • touch: Creates a new file (follow the command with the file name and extension).
  • mkdir: Creates a new directory (follow the command with the folder name).
  • mkcd or take: Creates a new directory (follow the command with the folder name) and changes to it (oh-my-zsh).
  • rm: Permanently deletes a file (follow the command with the filename).
  • rm -r: Permanently deletes a directory (follow the command with the directory name).

Note: For deleting, I strongly recommend the trash mdn package below, as it will move deleted files to your trash can / recycle bin.

Searching and Repeating Terminal Commands

  • Tab: With zsh, you can start typing any directory and press the Tab key to autocomplete. If multiple options fit the search, you can press the Tab key again to cycle through them (you can also use the arrow keys to navigate other options).
  • : Press the up arrow key to go to the previous command (continue pressing the up arrow key to go to the previous command). If you remember the beginning of the command, start typing and then press the up arrow key to show previous commands that match.
  • ctrl + r: Press the ctrl key and then the r key to search previous commands.
  • &&: Chain commands together with the && operator. For instance, npm install && npm run build will install the dependencies and then run the build command.
  • man: You can open the terminal manual pages for any command with the man command followed by the command. For example, man ls shows all possible flags available with the ls command. Press q to exit man pages.

Cancel, Stop, and Clear Commands

  • ctrl + c: Cancel current command.
  • ctrl + l: Clear previous terminal outputs.

Common Terminal Interface Commands

  • cmd + t: Open a new terminal tab.
  • cmd + w: Close current terminal tab.
  • cmd + d: Open horizontally split terminal tab.
  • cmd + shift + d: Open vertically split terminal tab.

Note: I recommend Hyper, a cross-platform terminal with tons of customizations, themes, etc. For help setting up your terminal, see my video overview here.

Helpful Custom Terminal Features

Install z.sh for quick navigation.

Once configured, typing z followed by a shorthand for a directory to quickly cd to that directory.

  1. Save this z.sh file at the root of your computer.
  2. Open your .zshrc file and add the following line to the end of the file: . ~/z.sh.
  3. Use your terminal like normal.

Once you’ve established patterns of going to directories, you can use the z command to quickly go to those directories. For example, if you have a folder at ~/Documents/Blog, z.sh will learn your pattern of going to that directory and will automatically go there when you type z blog.

Note: This script requires Oh My Zsh. Learn more at the Z GitHub repo.

Install the Trash CLI

By default, when you use rm to delete a file/folder, it will permanently and irrevocably delete the file/folder. This action can both be inconvenient if you accidentally delete something you later want, but it can also be dangerous (it’s easy to delete whole sections of your hard drive).

The trash cli replaces this default behavior by moving deleted files to your trash can. The trash cli works with both bash and zsh.

  1. Open your terminal to any directory.
  2. Type npm install –global trash-cli and press enter.
  3. Once installed, use trash followed by a file or folder to move it to the trash can/recycle bin.

The docs suggest the following:

Add the line alias rm=trash to your .zshrc or .bashrc file to ensure you always safely delete files: rm unicorn.png.

Note: If that doesn’t make sense, add this line anywhere in your .zshrc file: alias rm=trash. See the Trash CLI repo for more details.