This tutorial works best if you are using the Bash shell. Linux and macOS come standard with Bash, which you can access in the Terminal application. Windows PowerShell uses the .NET framework, which is not Bash. For Windows users, you have several options:
- Best option: Install Git Bash and use the Bash shell that comes with it. This is fast.
- Next best option: Install a Linux virtual machine like VirtualBox and use the Terminal that comes with it. This takes more time but gives you access to a virtual Linux machine on your PC.
- Last resort: Use Windows PowerShell. This has some functionality, as you saw in Appendix A of Shaw. But many of the commands below will not work, including
cd
(by itself),cd -
,touch
,cat
(trytype
or a text editor),head
,tail
, and others.
- Open a shell in Terminal (Mac/Linux) or GitBash (Windows)
- Create a working directory for the course, and a lessons directory inside that
- Copy the day's lesson from GitHub to your lessons folder
- Moving between directories
- Absolute and relative paths
- Creating, moving, and removing files and directories
- Investigating text files
- Working with commands and processes
- Tab-completion of commands and paths
- GUI and command-line text editors
- Customize your Finder window (or similar on non-Mac)
- Customize up your Bash environment
pwd
— print working directoryls
— list contents of working directorycd DIRECTORY
— change to directorycd ~
orcd
— change to home directorycd -
— change to previous directory
.
— current directory..
— one directory up../..
— two directories up/
— root directory~
— home directory
mkdir DIRECTORY
— make directoryrmdir DIRECTORY
— remove empty directorytouch FILE
— create an empty file or change file modification timecat FILE
— print contents of filecat > FILE
— write to file (warning: this will overwrite the file if it exists)cat >> FILE
— append to file (safer alternative to above in some cases)cat FILE1 FILE2 > FILE3
— combine two files into third filecp FILE1 FILE2
— copy filemv FILE1 FILE2
— move filerm FILE
— remove fileln -s FILE LINK
— make a symbolic link
less FILE
— view a text filehead -n N FILE
— first N lines of filetail -n N FILE
— last N lines of filetail -n +S FILE
– last lines of file starting at line Swc FILE
— count the words and characters in a fileopen FILE
— open a file using default program (on a Mac)file FILE
— get the file type for file(s)grep REGEX FILE
— search a text file for a string or regular expression
man COMMAND
— display manual page of command if it existsCOMMAND --help
— display usage informationwhich COMMAND
— show location of commandhistory
— display past commandstop
— display current processesexit
— close the current session
Popular text editors for writing code are Atom and Sublime Text. Download and install one of them if you haven't already.
A great way to document your work is using Markdown, a simple markup style. It is used widely in Jupyter notebooks and GitHub. Many text editors support Markdown, but I like the Mac program MacDown. I use it as a virtual lab notebook for my bioinformatics research.
For small jobs or if you want to stay inside the terminal, there are other useful programs. Note: cat
is only useful for creating very basic files, or starting files and finishing them in a proper text editor.
nano FILE
— nano is the most basic text editor (see Appendix: The Nano Text Editor at end of this lesson)emacs FILE
— emacs is a popular full-featured text editor controlled by keystrokesvim FILE
— vim or vi is a popular competitor to emacs that loads faster
There are several things you can do to set up your bash environment, which is what you see when you use the terminal (command line). You put these commands in a file called .bash_profile
(or .bashrc
or .profile
) in your home directory. Yes, that's a period at the beginning of the file, and the file is called a dot-file, which is a system file your computer uses to customize your shell.
In Assignment 1, you will have the chance to edit your .bash_profile
using a text editor like Nano or Atom. Be careful to not overwrite what's already there, and to not add any extra commands or stray text. It's a good idea to save a copy of your existing .bash_profile
with a different name as a backup.
As an alternative to editing your .bash_profile
directly, you can edit (or create and then edit) your .bashrc
file and have .bash_profile
source this file whenever it runs. To do this:
- Type
cd
to go to your home directory. - Type
nano .bash_profile
(orsudo nano .bash_profile
if you need root access). - Past this text at the top of your
.bash_profile
:
if [ -r ~/.bashrc ]; then
source ~/.bashrc;
fi
- Save
.bash_profile
and exit nano. - Type
nano .bashrc
. - Add any of the commands you want to .bashrc instead of .bash_profile. This way, you won't have to use sudo every time you want to customize your shell.
You can learn more about .bash_profile
and .bashrc
on macOS here.
Shown here are some commands that you might want to put in your .bash_profile
or .bashrc
to customize your terminal:
# customize prompt with color and pwd
PS1="\[\033[1;33m\][\u@\h:\w]$\[\033[0m\] "
# customize terminal window title to display pwd
PROMPT_COMMAND='echo -ne "\033]0; ${PWD##*/}\007"'
# autocomplete history with up arrow
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
# grep coloring
export GREP_OPTIONS='--color=auto'
export GREP_COLOR='01;38;5;226'
# command aliases (shortcuts)
alias cd..='cd ..'
alias cd...='cd ../..'
alias cd....='cd ../../..'
alias du='du -sh'
alias excel='open -a /Applications/Microsoft\ Office\ 2011/Microsoft\ Excel.app'
alias l='ls -p'
alias la='ls -ap'
alias lal='ls -alhp'
alias ll='ls -lhp'
alias m2u="tr '\015' '\012'"
alias u2m="tr '\012' '\015'"
alias rm='rm -i'
alias taill='ls -lrt | tail'
# history settings
export HISTCONTROL=ignoredups:erasedups # no duplicate entries
export HISTSIZE=100000 # big big history
export HISTFILESIZE=100000 # big big history
shopt -s histappend # append to history, don't overwrite it
# save and reload the history after each command finishes
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
Credit: SDSU Department of Astronomy
Nano is a text editor suited to working in UNIX. It is not as powerful as PC window-based editors, as it does not rely on the mouse, but still has many useful features.
Most nano commands are invoked by holding down the Ctrl
key (that is, the control key), and pressing one of the other keys. In this text, the control key is referred to using ^
. For example, ^X
means "hold down the control key and press the x key". Most of the important commands are listed at the bottom of your screen.
^G
nano help
To edit a file called filename, type nano filename. In nano, you can insert another file:
^R
read an existing file into nano (inserted at the current cursor position)^T
opens a browser that allows you to select a file name from a list of files and directories
The usual mouse-based point-and-click method is not supported by nano. Use the arrow keys to move around the page in nano.
Other navigation commands:
^A
move to beginning of line^E
move to end of line^Y
move down a page^V
move up a page^_
move to a specific line (^_^V
moves to the top of the file,^_^Y
to the bottom)^C
find out what line the cursor is currently on^W
search for some text.
When searching, you will be prompted for the text to search for. It searches from the current cursor position, wrapping back up to the top if necessary.
Insert new text at the current cursor position just by typing the text in.
Delete commands:
^D
delete character currently under the cursorBackspace
delete character currently in front of the cursor^K
delete entire line^\
search for (and replace) a string of characters
Cut and paste:
^K
does not delete lines permanently; the most recent set of deletions are stored in a buffer. These lines may be re-inserted at the current cursor location using^U
. This may be used to simulate cut and paste:- Repeatedly use
^K
until all of the text you want to move has been deleted. - Move to the line that you want to insert the text at, and use
^U
.
- Repeatedly use
- Note that pressing
^U
more than once will cause multiple copies to be inserted. This is particularly useful if you want to copy text:- Repeatedly use
^K
until all of the text you want to copy has been deleted. - Press
^U
immediately to put a copy back in its original location. - Move to the line that you want to copy the text to, and use
^U
.
- Repeatedly use
^O
save contents without exiting (you will be prompted for a file to save to)^X
exit nano (you will be prompted to save your file if you haven't)^T
when saving a file, opens a browser that allows you to select a file name from a list of files and directories