Skip to content

Terminal Cheat Sheets

Elton edited this page May 3, 2017 · 6 revisions

Cheat Sheet for Terminal Commands!

This guide is made to provide a list of commonly used terminal commands with a brief description.

Manual: man

For all the commands below, we can use the man command to view awesome documentation/info. This command provides you with the name, synopsis, description, and a list of flags that can be attached.

Example

$ man ls
NAME
     ls -- list directory contents

SYNOPSIS
     ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1] [file ...]

DESCRIPTION
     For each operand that names a file of a type other than directory, ls displays...

     The following options are available:

     -@      Display extended attribute keys and sizes in long (-l) output.

     -1      (The numeric digit ``one''.)  Force output to be one entry per line.  This is the default when output is not to a terminal.

     -A      List all entries except for . and ...  Always set for the super-user.
     ...

Change Directory: cd

Change directories with cd by specifying paths.

  • $ cd ~ (or $HOME)
    • goes to home directory
  • $ cd ..
    • moves up a directory
  • $ cd {directoryName}
    • moves into directory in current folder

List Files: ls

Displays a list of contents in directory.

  • $ ls
    • Displays current directory
  • $ ls {Path}
    • Displays directory specified by the path
  • $ ls -a
    • Includes hidden items in display
  • $ ls -l
    • Displays read, write, and execute permissions on files
  • $ ls -la
    • Includes hidden items

Make Directory: mkdir

Creates a new directory.

  • $ mkdir {directoryName}
    • Creates a directory in current directory
  • $ mkdir {directoryName1} {directoryName2} {directoryName3}
    • Creates multiple directories in the current directory
  • $ mkdir -p {parentDirectory}/{childDirectory}
    • The p flag allows you to create multi-levels of new directories
  • $ mkdir {Path}/{directoryName}
    • Creates a directory at specified paths

Create a File: touch

touch creates a new file if it doesn't exist or updates the time stamp of an existing file.

  • $ touch {fileName}
    • Creates a new file.
  • $ touch {fileName 1} {fileName2} etc
    • Creates multiple Files

Copy: cp

Creates a copy of file or directory in the working directory

  • $ cp -i {filename} {newFileName}
    • The flag checks to see if the newFileName already exists within the current directory. If it does exist, you will be prompted to overwrite the existing file or not. Without the i tag, an existing file with the newFileName will be overwritten without a prompt. BE CAREFUL
  • $ cp -i {filename} {Path}
    • Move copied file into another directory, name remains the same as original
  • $ cp -i {filename} {Path}/{newFileName}
    • Move copied file into another directory, name changes to newFileName

Remove: rm

Remove files and directory

  • $ rm {fileName}
    • Deletes file
  • $ rm -rf {directoryName}
    • For directories we need to use the r and f flags. r stands for recursive, anytime you are trying to remove a folder with contents, you must use r. f stands for force, to force remove directive. Use man rm for more info!

Move or Rename: mv

Move files and directories to new directives or rename them in the working directory.

  • $ mv {fileName} {newFileName}
    • If a path to a different directive is not specified, mv works as a renaming command.
  • $ mv {fileName} {Path}
    • When a new path is specified, the designated file will be moved from the current working directive to the new location

Protips

Print Working Directory: pwd

pwd allows you to see the full working path of your current directory

User:someRandomFile User$ pwd
/Users/User/someDirectory/someRandomFile

To have the full path always displaying, we can edit our .bash_profile.

Check to see if you have a .bash_profile

$ cd ~
$ ls -a

If .bash_profile does not exist, create it in your home directory with touch!

touch .bash_profile

Open your .bash_profile with your preferred text editor and add the following snippet to your file.

# Shows route path in terminal
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

After editing we need to reload our .bash_profile in order to see the effects.

source ~/.bash_profile

Writing an Alias

Establish shorthand commands to make your life simpler while using the terminal. Add aliases to your .bash_profile and reload to get your shortcuts working

Alias Syntax

alias aliasName='command you want fired'

Example

# opens google chrome by typing chrome
alias chrome='open ~/../../Applications/Google\ Chrome.app/'

Clone this wiki locally