What is a shell? • a program to interact with a computer • a program that runs other programs • especially useful for line programs • not a GUI/windows system • many flavors, is the most popular

Why use a shell? • your favorite program only has a command line interface. This happens often if you work remotely, e.g. by dialing into department machines. – : writing a quick R script on a remote machine – $ ssh [email protected] – $ nano mydocument.txt • automation of mundane tasks in file system and os interactions

Secure shell • ssh # a secure shell connection • scp # copy a file with secure shell

Navigating the file system

• $ # print working directory • $ # list files and directories

• $ ls -tahlF # list files and directories in a list (l), including hidden files (a) with human readable file sizes (h), in chronological order (t), with slashes after directories (F) • $ Documents # change working directory to Documents/

• $ new_paper # make a new directory inside the Documents folder • $ nano draft.txt # nano is a super lightweight text editor • $ draft.txt # delete the text file (BE CAREFUL WITH THE RM COMMAND)

• $ new_paper # delete an empty directory • $ # copy a file • $ # move a file

1 • $ # find lines in files

– $ grep -n stats draft.txt

• $ find # find files

– $ find . - d – $ find . -maxdepth 1 -type f – $ find . -empty – $ find . -name ‘*.md’

• generic wildcards with back quotes

– $ -l . -name ’*.md’

• grep and find together

– $ grep SNP find . -name ’*.md’ – $ grep ‘\vec{q}’ find . -name ’*.tex’ to find some expression in your .tex files. Note the quotation marks ‘’ and ‘ ‘ are different.

Useful odds and ends

• . # shortcut for the current working directory

• .. # shortcut for the parent directory • - # shortcut for the previous directory • $ clear # clears the page • $ # creates a file without running any other program

• $ wc # counts the lines, words, and characters in a file • redirecting output to a file

– $ wc -l draft.txt > filelengths.txt # find the number of lines in draft.txt and save in filelengths.txt – $ filelengths.txt # show the content of filelengths.txt – redirecting with >> appends instead of overwriting.

• piping output to another command

– $ wc -l *.txt | -1 # finds the line length of all .txt files and sends the result to the ‘head’ command, which prints only the first line – $ wc -l *.txt | | head -1

2 • $ tree - tree needs to be installed (apt-get or brew both work). It displays files/directories in a tree structure, and can accept most of the options you can pass to ls. • top - displays system usage information • htop - displays system usage information with some ASCII graphics for processor usage. • screen # for persistent SSH sessions with better control • nohup

Aliases and shell scripting

• $ lm=“ls -tahlF”

– not permanent. – to make a permanent alias, save it in .bashrc or .bash_aliases

• $ set • $ $PATH • $ echo $HOME • looping

– $ for file in *.md; do head -1 $file; done

Running shell commands in other programs • R “system” – system(‘echo $HOME’) – x = system(‘echo $HOME’, intern=TRUE) – print(x) • IPython “!” – basic commands are native (pwd, ls, cd) – x = !echo $HOME – print x

Password-free SSH • $ ssh-keygen • $ [enter a passphrase if so desired]

3