Unix introduction

# The Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.
# In other words: Keep It Simple, Stupid

In the examples below, lines that start with a "$" are Unix commands. You can copy and paste those lines, but do not copy/paste the "$".

# Print the current working directory
$ pwd 

# List the files in the current directory
$ ls

# Long listing of the files in the current directory # The majority of unix commands will take options, these are prefixed with a dash

$ ls -l

# Options may be combined
$ ls -lh

# The following output shows three files and one directory. The directory is
# prefixed with a ‘d’. Several more information is shown, the majority of which
# we will talk about later.

$ ls -lh
total 16911584
-rwxr-xr-x  1 danny  staff   8.1G Oct 28 12:58 2057_Illumina.tgz
-rwxr-xr-x  1 danny  staff   5.3M Oct 28 12:28 Dmau_Chr3R_r1.0.fasta.gz
-rw-r--r--@ 1 danny  staff    61K Oct 30 00:14 Unix introduction.docx
drwxr-xr-x  7 danny  staff   238B Oct 29 22:44 deNovoSNPs

# For help with or information about a command

$ man 

# Your current user id

$ whoami

# Tab completion is a handy feature of unix. Type ‘who’ and hit tab twice to see what commands you have available to you.


# Changing directories
$ cd /tmp
$ cd /Users/

# There are several "special" ways to navigate directories in unix.
$ cd .. 	# one level back
$ cd ~ 	# home directory
$ cd - 	# last directory

# Make a directory
$ mkdir foobar

# Remove a directory
$ rm –r foobar

# Create an empty file
$ touch test.file

# Remove a file
$ rm test.file

# Move a file
$ touch test.file
$ mv test.file new.file
$ mkdir testDirectory
$ mv new.file testDirectory/

# Disk usage information
# notice that –h when passed to ls gave a similar result

$ df -h

# Current directory size
$ du -sh

# Getting and exchanging data can occur many ways. The most common commands are wget, curl, ftp, and scp.

$ curl -O http://goo.gl/Gd5SO1

# This would work as well, but wget isn’t typically installed on Mac OSX

$ wget http://goo.gl/Gd5SO1

# Files may be viewed several ways. The most popular are:

$ less example.vcf
$ more example.vcf
$ cat example.vcf
$ head example.vcf
$ tail example.vcf

# Passing options changes the behavior of commands:

$ head -20 example.vcf
$ tail -25 example.vcf

# grep is a pattern searching program that allows you to find specific text in a file or group of files

$ grep 14223 example.vcf
$ grep 14223 *.vcf

# Pipes make unix very powerful. They allow you to string commands together to perform a task.

$ cat example.vcf | grep 12759146

# Using the man page, or by just thinking about it, what is the command ‘wc’ doing below? What’s another useful flag for ‘wc’?

$ cat example.vcf | wc –l

# The –v flag to grep says "exclude anything that matches this". I put the # in quotes because it’s a special character. Quotes are optional, for example ‘grep –v foo’. Note that if you’re copying from word, the quotes will probably be incorrect, please replace them on the command line.

$ cat example.vcf | grep -v "#" | wc -l

# There are several handy tricks for matching with grep. Using a ^ to match a character at the beginning of the line lets us match only X chromosome entries. Alternatively, if we knew a line ended with the word "foobar" we could use ‘grep "foobar$"’ to force the match to the end of the line.

$ cat example.vcf | grep -v INDEL | grep "^X" | wc -l

# By the way, you just learned a regular expression (aka: regex). I tricked you! Don’t ever let regex matching scare you—it’s a very powerful skill and you can learn it.


CSHL ECG2014 Course schedule