UNIX Commandline Cheatsheet


02 Dec 2015

This is the cheatsheet accompanying a workshop I gave recently for the BDSS IGERT trainees.

Resources
Basic Commands
Filesystem
Moving around
$ pwd               # Where am I?
$ cd [dir]          # Change directory
$ pushd [dir]       # Save current location and change to [dir]
$ popd              # Change back to directory stored with pushd
Moving creating and destroying things
$ cp [origin] [target] # Copy
$ mv [origin] [target] # Move    
$ mkdir [dir]          # Make directory
$ rm (-r) [file]       # Remove [file] (-r for recursive for dirs)
$ touch [file]         # Create empty file
Looking at stuff
$ ls                   # List stuff in current directory 
$ cat [file]           # Print [file] contents stdout
$ head (-n) [file]     # Show first (-n) lines of [file]
$ tail (-n) [file]     # Show the last (-n) lines of [file]
$ less                 # Scroll thought the file
Finding Things
$ find [file/dir]         # Find file or directory
$ grep 'something' [file] # Find string in file
Pipe and Redirect
$ [command] | [programm] # Input the output of [command] to a [program]
$ [command] > [file]     # Create / overwrite [file] containing output of [command]
$ [command] >> [file]    # Append output of [command] to [file]
$ [program] < [file]     # Input content of [file] to [program]
Shell Configuration Example

If you want to make permanent changes to your command line you can do so in your ~/.bashrc or ~/.bash_profile (mac) files. Example: Shortcuts (aliases) for commands you often use. To create the shortcut ll for ls -alH put the following into your config file: alias ll='ls -alH'

Editing
nano [file]       # Opens [file] in a simple text editor

In nano ^ means cntrl. So ^X is cntrl-X in case you are confused.

vim  [file]       # Opens [file] in a more advanced editor

sed               # Stream editor
sed '' [file]     # print the contents of the file
sed 's/[pattern]/[replacement]/' [file]   # replace the first match of [pattern] with [replacement]
sed 's/[pattern]/[replacement]/3' [file]   # replace the third match 
sed 's/[pattern]/[replacement]/g' [file]   # replace all matches

If you want to learn vim check out Vim Adventures See this tutorial for the basics of sed and the documentation for all the details.