# Unix and the MacOS Terminal App

Unix is an operating system written in C. It is hardware independent.

MacOs is based off a branch of unix (BSD + next + apple = darwin). On a Mac the Finder and system preferences interact with unix for you, so you don't have to use the Terminal for everthing.

# MacOS Terminal

On a Mac you will use the Terminal app to run commands. When you enter the Terminal app, you are automatically logged in as your user account.

$ is the command prompt, and it means “tell me to do something”

# Keyboard Shortcuts

Here are some common Terminal keyboard shortcuts you'll probably use often.

  • cmnd + text size up
  • cmnd - text size down
  • opt click to go to a point in the line
  • cntrl a start of line
  • cntrl e end of line
  • up arrow and down arrow to navigate past commands
  • tab to autocomplete
  • cmnd k to clear

# Unix Commands

# Structure of a Unix Command

A command is composed of 3 parts: command -> options -> arguments

In this example1: echo -n 'Hello World'

  1. echo is the command
  2. -n is an option (preceded by a hyphen)
  3. 'Hello World' is a string argument

Here's another example ls -lah Desktop

# What Are Unix Commands

A unix command is a program that does something, an executable file. Many of these executable files are stored in /bin. For example, when you use the echo command, you are executing the file /bin/echo — that's all there is to it!

You can get information on a command/file using:

  • whatis echo
  • wherein echo
  • which echo

Add the option: -v, --version, or --help too.

To exit a command use one of the following: q, x, ctrl + q, ctrl + x, ctrl + c, or ESC

ctrl + c force quits a command, the process may keep running even if you close the terminal window otherwise

You can put semicolons between commands, to execute them very quickly. For example:

echo 'hello world' ; echo 'same here'

# The $PATH Variable

Your $PATH configures which directories to look for a command file and which order to look for them in.

You can see your path using echo $PATH which should output something like:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin

You can change the $PATH temporarily or permanently. How to do so varies based on your operating system.

# Unix Terms to Know

# kernel

The kernel is the core of the operating system. It allocates time and memory to programs. MacOS uses the Mach kernel.

# shell

The shell is the outer layer of the OS. The shell interacts with the user and sends requests to the kernel. MacOS uses zsh by default but includes other choices too. We typically use the bash shell.

# man (manual) pages

Most unix commands will have manual pages that provide instructions on how they work and what they do. You can access them like this.

  • man echo
  • man -k echo
  • apropos banner
  • whatis banner

To navigate around man pages use:

  • spacebar to advance
  • f and b forward and back
  • q to quit (exit)

# Getting Around in Unix File Systems

# present working directory

The pwd (present working directory) is where a unix command is taking place. (You'll want to take care to know where you are in the file system for any given command).

Type pwd for your present working directory

Some other common file system commands are:

  • ls to list contents of present working directory
  • ls -l to list contents of present working directory vertical format list with file info
  • ls -la to list with hidden files
  • ls -lah to list with file size in a human readable format
  • . represents the current directory
  • .. represents the parent directory
  • .filenamehere is an invisible configuration files
  • cd Directorynamehere used to change directory
  • cd .. means go to parent directory
  • cd ../.. means go up 2 parent directories

NOTE: you can use the tab key to autocomplete files names (very handy)!

  • cd / starts your path at the root of the hard drive, otherwise your path is relative to the present working directory
  • cd ~ takes you back to your user folder /Users/yourusername

# General Rules for Unix Filenames

  • max of 255 characters long
  • avoid most symbols (use A-Z, a-z, 0-9, period, underscore, hyphen)
  • use lowercase letters (unix is case sensitive)
  • avoid spaces in file names
  • use files endings whenever possible (.txt)

# Built-in Unix Text Editors

Sometimes you may need to edit a text file directly from the Terminal app. If you do, you can you one of the built-in text editors.

  • vim
  • nano (good for beginners, basic easy to use)

# Creating and Moving Files and Folders

You can create files with the command touch somefile.txt (this command updates file access info, or creates the file if it does not exist).

To read the contents of a file

  • cat (Concatenate, if you pass it two files it will combine them? good for small files)
  • less (backword scrolling, better memory use, good for large files)
  • head (show the beginning of a file)
  • tail (show the end of a file)

# Directories (Folders)

  • mdkir makes a folder
  • mkdir -p pass the command a full path and it will make all directories in the path that do not exist

# Moving Files Around

  • mv file.txt testdir/file.txt moves file to specified path, cane rename the file as well
  • mv -n will not overwrite, mv does overwrite by default!
  • cp somfile.txt copyfie.txt like move, but copies instead of moving
  • rm deletes a file permanently, does not put stuff in trash
  • rmdir deletes an empty directory
  • rm -R deletes files or directories recursively, all contents of a directory, etc. (very powerful and dangerous)

NOTE: aliases created in MacOS finder are actually files, they are very useful to finder, but are not true Unix links and directories (ex. you can not cd into a folder alias, etc.)

ln filetolink hardlink reference to a file in filesystem, does not break if file is moved, does not break if file is deleted ln -s references the path to the file, breaks if file is moved or deleted

# Users and Permissions

Essential user commands:

  • whoami logged in as what user right now
  • chown change ownership
  • chmod change permissions
  • chmond -R change permissions for directory and all contents

# File permissions

# Alpha Notation

r w x stands for read write execute - stand for no permissions

Example: rw-r--r--

chmod ugo=rwx filename
chmod u=rwx,g=rw,o=r filename
chmod ug+w filename

# Octal Notation

chmod 777 filename is equivalent to rwxrwxrwx

# The root user

The root user can do ANYTHING on the unix file system. (The root user is disabled by default on MacOS). You can let a user temporarily take on the powers of another user, usually the root user.

sudo means "substitue user do" and is a command that runs other commands that only admins can do

# Using git

Git is one of the most commands you will use in Terminal (when you are not using git via a MacOS application). Here are the things you'll use it for often:

git checkout dev where "dev" is a branch name

This changes your files and folders in the present working directory to reflect whatever branch you are working on.

git add -i

Interactive interface to add files you are working on to a commit.

git add .

Looks at the working tree and adds all paths to the staged changes if they are either changed or are new (and not ignored) It does not stage any 'rm' actions.

git add -u

Looks at all the currently tracked files and stages changes to those files if they are different or if they have been removed. (It does not add any new files, it only stages changes to already tracked files.)

git add -A

A handy shortcut for doing both, equivalent to running git add .; git add -u;

git rm

Used to delete files.

git rm -r

Used to delete files and directories including, their contents.

git commit -m 'message here'

Used to commit to your changes with a commit message.

git push origin master

Push your changes where "master" is the branch name.

git pull

Pull changes from remote repository.

git status

See what files are modified, what new files exist, what havs been removed, basically, "What's up git?"