The Basics
  • $ ls : shows what's listed in the current directory
  • $ ls -a : shows what's listed in the current directory, including hidden files
  • $ mkdir foo : makes a folder in the current directoy called foo
  • $ cd foo : move into the folder foo
  • $ cd .. : move backwards into the parent directory
  • $ pwd : prints current working directory
  • $ ls foo : lists the contents of the folder foo

  • $ ls ~/foo : lists the contents of the folder foo that lives in your home directory (~/, e.g. '/Users/hills')
  • $ cp foo.txt bar.txt : duplicates foo.txt and saves it as bar.txt
  • $ mv foo.txt some-folder/bar.txt : moves foo.txt to the some-folder folder, and calls it bar.txt (foo.txt is deleted)
  • $ rm bar.txt : deletes foo.txt

  • $ ^L ([Control]-[d]) : clears terminal screen (clear also does this)
  • $ cat foo.txt : prints all of foo.txt to the screen
  • $ less foo.txt : prints the first few lines of foo.txt to the screen: to continue, press space, to quit reading, press q.
  • $ head foo.txt : prints the first ten lines of foo.txt
  • $ tail foo.txt : prints the last ten lines of foo.txt
  • Text Searches
  • $ less /keyword : If you use the less foo.txt command to inspect the contents of foo.txt, you can type a keyword: /some-keyword before you exit (q), and any matches to that keyword will be highlighted.

  • $ grep keyword foo.txt : searches for an exact match to keyword in the file foo.txt
  • $ grep 'I like ice cream' foo.txt : searched for an exact match to the phrase 'I like ice cream'
  •    - grep -i : ignores cases
       - grep -v : only displays lines that don't match
       - grep -c : only prints the number of lines that include a match
       - grep -ivc : all three i, v, and c options at once
  • wc foo.txt : displays the line count, word count, and character count of foo.txt
  •    - wc -l : line count
       - wc -w : word count
       - wc -c : character count
    * is called a wildcard (sort of like in regular expressions) - it's used in text searches. A * matches against any (or no) characters in a file or directory name.
  • $ ls kittens* :
  • shows all files in the current directory that starts with kittens.
  • $ ls *kittens :
  • shows all files in the current directory that ends with kittens. The ? character matches to any single character.
  • $ ls ?ittens* : could match to mittens, kittens, or mittons.csv, but not, er, lots-of-kittens.
  • File Access Rights
  • $ ls -l : displays a very verbose version of the items in the current directory. Each line represents a file, and there are various bits of information displayed (access rights, owner, group-owner, size of the file, time the file was created, filename).

  • Access rights consist of a ten character string. The first letter is either 'd' (if it's a directory), or '-' (if it's not). The next three letters show the read, write, and execution permssions for the user that owns the file. The next three shows the read, right, and execution permissions for the group of people that own the file (group owner), and the last three characters gives the read right and execution permissions for everyone else. If the symbol showing permissions is 'r', 'w', or 'x', that means the user(s) can respectively read, write to, or execute the given file, but if the character is '-', they cannot. So if the permissions are -rw-r--r--, nobody but the owner can write to the file, but everyone can read it.
    The command chmod allows you to change the permissions of a file or directory.
    - u stands for user, g stands for group, o stands for other, and a stands for all.
    - r stands for read, w stands for write, and x stands for execute.
    - + = add the permission, and - = remove a permission.
  • $ chmod go-rwx : remove the read, write, and execute rights for the group and other users.
  • $ chmod a+rw : give the read and write permissions to all users.
  • Other Useful Tips & Tricks
  • $ df . : shows how much space is left on your computer
  • $ du : shows the amount of space used by each subdirectory. This is usually a super long list though, so instead:
  • $ du -d 1 : goes one directory deep
  • $ du -d 2 : goes two directories deep, and so on.

  • $ zip foo foo.txt : compresses foo.txt and saves it as foo.txt.zip. Note that you can zip up whole directories, or use wildcards to zip multiple files in a directory at once.
  • $ unzip foo.txt.zip : unzips it

  • $ file * : shows the types of objects in your current directory (jpeg, text file, etc...)

  • $ history : shows a list of the 15 most recent commands that you typed into the terminal. If you type history -k, the last k commands will show up.
  • Redirection
    type $ cat > deepest-thoughts. Then type:
      $ I
      $ am a mega awesome
      $ unixer
    Then enter $ ^D. The text you entered will be routed to a file named deepest-thoughts, because the > symbol redirects the output of a command ingto whatever is to the right of the > symbol.

    Type open deepest-thoughts to open the file or cat deepest-thoughts to see the lines printed to your screen. >> appends to a file. So:
    $ cat >> deepest-thoughts
    $ seriously
    $ ^D
    Will append the line 'seriously' to your deepest-thoughts file.
  • $ cat foo1 foo2 > megafoo : appends foo1 to foo2 and saves it as megafoo.

  • The < symbol redirects the input of a command.
  • $ sort < list.txt > foo.txt : sorts list.txt and saves it to the files sorted.txt
  • $ list.txt | sort : the | symbol pipes list.txt into the sort command. So $ list.txt | sort is equivalent to $ sort list.txt, which is the same as sort < list.txt
  • Jobs
  • $ ps : shows the the currently running jobs
  • $ sleep 10 : sleeps the terminal for 10 seconds
  • $ sleep 100 & : sleeps the terminal for 100 seconds, but in the background due to the ampersand. The unqiue job identifier (PID) is also printed. You can type ps before the 100 seconds is up to see the job running.
  • $^Z bg : ^Z suspends a currently running process, abd bg restarts the process but puts it in the background.
  • $ jobs : displays a list of currently running jobs (many processes can be contained in one job)
  • $ fg %i : restarts the ith job in the list of jobs, where i is an integer.
  • $ kill %i : kills the ith job if it is suspended or a background process.
  • $ ^C : kills a job that is currently running in the foreground.
  • $ kill 1234 : kills a process with PID number 1234
  • Manuals
  • $ man some-command : shows the manual page for some-command. (Press q to quit)
  • $ apropos keyword | head : returns the first few commands that have the word 'keyword' in their header. So, if you type