This post will introduce you to basic command line commands. I’ll keep explanations short so this acts as more of a cheat sheet.
Navigating Files and Folders
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).
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 buildwill install the dependencies and then run the build command.
man: You can open the terminal manual pages for any command with the
mancommand followed by the command. For example,
man lsshows all possible flags available with the
lscommand. 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.
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.
- Save this z.sh file at the root of your computer.
- Open your .zshrc file and add the following line to the end of the file:
- 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
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.
- Open your terminal to any directory.
npm install –global trash-cliand press enter.
- Once installed, use
trashfollowed 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
.bashrc file to ensure you always safely delete files:
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.