Helpful Terminal Commands for Beginners!
Tags: beginners, bash, productivity, linux
Published: 2018-09-05 05:39:12

---
title: Helpful Terminal Commands for Beginners!
published: true
description: Check out this cheat sheet of helpful commands to supercharge your work in the Mac command line.
tags: beginners, bash, productivity, linux
canonical_url: https://www.malikbrowne.com/blog/helpful-terminal-commands
cover_image: https://thepracticaldev.s3.amazonaws.com/i/cjpho9923u50ftylp0bn.gif
---

This cheat sheet was originally posted on [malikbrowne.com](https://www.malikbrowne.com/blog). 

I **love** learning new commands and tools to optimize my workflow on my Mac. Since I have started working with more terminal-oriented applications, there are several commands that I use that I'd love to share with beginners and terminal lovers alike.

**Note:** This article assumes that you're using some type of **UNIX shell**, with a preference towards Mac. Some of these commands may not work on Windows CMD/Powershell.

## Basic Commands

Commands in a shell can be used in a ton of different ways - but I'd say that the important ones to learn fall under three main categories:

1. Navigating and Working with Files & Directories
2. Manipulating Output for Input
3. Finding Things

### Navigating and Working with Files & Directories

On a computer, files and directories (otherwise known as folders) are responsible for managing information. Here are some commands to make your life easier when working with files:

- `cd` - navigate to different directories
- `pwd` - see the name of the current directory
- `ls` - lists all files in the current directory
- `mkdir` - make a new directory
- `touch` - make a new file
- `cp` - copy a file
- `mv` - move a file or directory
- `rm` - removes a file or directory
- `zip` - compresses files into a zip archive
- `unzip` extracts files from a zip archive
- `chmod` - Allows you to make a file executable and change permissions granted to it by your machine.
    - In order to make a file executable, you can type `chmod +x [name of file]`
- `tar` - Allows you to work with tarballs in a Linux command line. It has a long list of uses, including package management for systems.
    - `tar -cvf` allows you to create a **.tar** archive
    - `tar -xvf` allows you to "untar" a **.tar** archive
    - `tar -tvf` list the contents of a **.tar** archive

**Honorary Mention: `sudo`**

`sudo` is a very widely used command in bash interfaces that allows you to run a command with administrative or root privileges. 

For example, if you edit any files in the root directory (also known with the path `/`) of your machine without `sudo` privileges, you will be denied permissions to edit the file. 

For your safety, there are a couple of things your computer will not allow you to do as a `sudo`'d command, i.e. running bash scripts.

### Manipulating Output for Input

Often times when scripting the goal you are trying to achieve is to run filters through text, check output logs from a server, or run batch commands on multiple files. 

Here are some commands that can be beneficial for manipulating file output:

- `cat` - show a file's contents
- `head` - displays the first few lines of a file
- `tail` - displays the last few lines of a file
    - Using the `-f` flag with `tail` will allow you to see updates to the file as they are coming in. This is extremely helpful for tracking log output from servers.
- `|` - The pipe character take a command's output and uses it for the input of another command
- `*` - matches zero or more characters in a filename
    - There is actually a whole way of matching multiple files and names via globs!
- `?` - matches any single character in a filename
- `>` - stores a command's output to a file, and creates a new file if one doesn't already exist
    - **WARNING:** This will override and replace all contents that were in the original file.
- `>>` - concatenates a command's output to a file, creates a new file if none is found

### Finding things

- `echo` - show text or the compiled text of other commands. Great for testing commands out.
- `grep` - allows you to find results line by line using some sort of pattern or regular expression.
- `ack` - similar to grep, but has a better code search because it knows to search in places you'd expect it to search. (ignores version control directories, etc.)

## Other helpful commands I've found

- `pbcopy` - Copy selected text into your clipboard buffer
- `history` - Prints the command history for your session
    - This is really useful with grep and the pipe symbol, which will allow you to search through your history: `history | grep [part of command]`
- `pbpaste` - Paste the selected text in the terminal from your clipboard buffer
- `curl` - Allows you to make HTTP calls RESTful endpoint
- `kill` - Kills a running process given a process id (pid)
- `killall` - Kills all running processes of a certain type
- `lsof -i tcp:[port number here]` - list all running processes on a specific port
    - This one is particularly useful for when you get an error like:

```
    Error: listen EADDRINUSE :::3005
        at Object.exports._errnoException (util.js:1023:11)
        at exports._exceptionWithHostPort (util.js:1046:20)
        at Server._listen2 (net.js:1261:14)
        at listen (net.js:1297:10)
        at Server.listen (net.js:1375:9)
        at Object.<anonymous> (/path/to/node/server/server.js:15:34)
        at Module._compile (module.js:571:32)
        at loader (/path/to/node/modules/node_modules/babel-register/lib/node.js:144:5)
        at Object.require.extensions.(anonymous function) [as .js] (/var/www/html/gcsbpo/rocc/node_modules/babel-register/lib/node.js:154:7)
        at Module.load (module.js:488:32)
```

It provides an easy way to find out the pid to kill the running process.

### Shortcuts/Tips & Tricks

- `!$` expands the final argument of the previous command. Useful for chaining commands on the same file
- `!!` - repeats the last command executed
    - These are thanks to [Thomas](https://dev.to/ferricoxide)
    - ![number]: Repeat command from your history (you can see the available command-history using fc -l (or fc -l 1 to see all the contents of your history)
    - !! [extra stuff]: Re-execute last command, tacking on <extra stuff> at the end of the command.
    - [extra stuff]!!: Re-execute last command, tacking on [extra stuff] at the beginning of the command. Very handy for executing something as a regular user that needs to be ran with `sudo`
    - !!:s/[SEARCH]/[REPLACE]: Re-execute last command, replacing first substring [SEARCH] with [REPLACE]
    - !!:s/[SEARCH]/[REPLACE]: Re-execute last command, replacing all substring [SEARCH]es with [REPLACE]es
- `clear` - clears all output from the terminal, old output - can still be accessed by scrolling up
- `CMD + k` - clears all output from the session, only  previously called commands are retained
- `CTRL + c` - Aborts the current running process and closes it.
- `CTRL + z` - Pauses (`SIGSTP`) any the current running process
    - **NOTE:** `CTRL + C` aborts the process, but `CTRL + z` leaves it idling in memory.
- `CTRL + a` - Takes you to the beginning of the bash input
- `CTRL + e` - Takes you to the end of the bash input
- `CTRL + u` - Clears all input *before* the cursor
- `CTRL + r` - Opens a prompt that will search the previous commands from your session

## Conclusion

Learning how to navigate the terminal is definitely daunting at first, but after spending some time scripting you'll find that these commands can save you tons of time on a day to day tasks that you do.

Personally, I'm starting to reach the point where I navigate faster via Terminal than when I use Finder.

If you have any commands, shortcuts, or tips I should add to this post, please leave me a comment! Would love to add more.