2021 Linux fundamentals
This page should serve as a reference for the many "things Linux" we use in this course.
- 1 Terminal programs
- 2 Getting around in the shell
- 2.1 Important keyboard shortcuts
- 2.1.1 Tab key completion
- 2.1.2 Arrow keys
- 2.1.3 Command line editing
- 2.2 Wildcards and special file names
- 2.3 Standard streams
- 2.3.1 redirecting output
- 2.3.1.1 Output redirection examples
- 2.3.1 redirecting output
- 2.4 Piping
- 2.4.2 piping a histogram
- 2.4.2.1 The power of chaining pipes
- 2.5 Environment variables
- 2.6 Quoting in the shell
- 2.1 Important keyboard shortcuts
- 3 Using Commands
- 3.1 Command options
- 3.1.1 Useful options for ls
- 3.1.2 Examples of word options
- 3.2 Getting help
- 3.2.1 --help option
- 3.2.2 -h or -? options
- 3.2.3 just type the program name
- 3.2.4 Google
- 3.2.5 man pages
- 3.1 Command options
- 4 Basic linux commands you need to know
- 5 Advanced commands
- 6 Copying files between TACC and your laptop
- 7 Editing files
Terminal programs
You need a Terminal program in order to ssh to a remote computer.
Macs and Linux have a Terminal program built-in
Windows options:
Windows 10
Command shell has ssh and scp (may require latest Windows updates)
Start menu → Search for Command
Windows Subsystem for Linux – Windows 10 Professional includes a Ubuntu-like bash shells
See https://docs.microsoft.com/en-us/windows/wsl/install-win10
We recommend the Ubuntu Linux distribution, but any Linux distribution will have an SSH client
or
Putty – http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
simple Terminal and file copy programs
download either the Putty installer (https://the.earth.li/~sgtatham/putty/latest/w64/putty-64bit-0.70-installer.msi)
or just putty.exe (terminal) and pscp.exe (secure copy client)
Cygwin – http://www.cygwin.com/
A full Linux environment, including X-windows for running GUI programs remotely
Complicated to install
Getting around in the shell
Important keyboard shortcuts
Type as little and as accurately as possible by using keyboard shortcuts!
Tab key completion
The Tab key is your best friend! Hit the Tab key once or twice - it's almost always magic! Hitting Tab invokes shell completion, instructing the shell to try to guess what you're doing and finish the typing for you. On most modern Linux shells, Tab completion will:
single Tab – complete file or directory names up to any ambiguous part
if nothing shows up, there is no unambiguous match
Tab twice – display all possible completions
you then decide where to go next
work for shell commands too (like ls or cp)
Arrow keys
Use Up arrow to retrieve any of the last 500 commands you've typed, going backwards through your history.
You can then edit them and hit Enter (even in the middle of the command) and the shell will use that command.
The Down arrow "scrolls" forward from where you are in the command history.
Right arrow and Left arrow move the cursor forward or backward on the current command line.
Command line editing
Use Ctrl-a (holding down the "control" key and "a") to jump the cursor to the beginning of the line.
Use Ctrl-e to jump the cursor to the end of the line.
Arrow keys are also modified by Ctrl
e.g. Ctrl-right-arrow will skip by word forward; Ctrl-left-arrow backward.
Use Backspace to remove text before the cursor; Delete to remove text after the cursor.
The shell has shorthand to refer to groups of files by allowing wildcards in file names.
* (asterisk) is the most common filename wildcard. It matches "any length of any characters".
This technique is sometimes called filename globbing, and the pattern a glob.
Other useful ones are
brackets ( [ ] ) to allow for any character in the list of characters between the brackets.
and you can use a hyphen ( - ) to specify a range of characters (e.g. [A-G])
braces ( { } ) enclose a list of comman-separated substrings to match
For example:
ls *.bam – lists all files in the current directory that end in .bam
ls [A-Z]*.bam – does the same, but only if the first character of the file is a capital letter
ls [ABcd]*.bam – lists all .bam files whose 1st letter is A, B, c or d.
ls *.{fastq,fq}.gz – lists all .fastq.gz and .fq.gz files.
Three special file names:
.(single period) means "this directory"...(two periods) means "directory above current." So ls .. means "list contents of the parent directory."~ (tilde) means "my home directory".
Avoid spaces in filenames
While it is possible to create file and directory names that have embedded spaces, that creates problems when manipulating them.
To avoid headaches, it is best not to create file/directory names with embedded spaces.
Every command and Linux program has three "built-in" streams: standard input, standard output and standard error.
It is easy to not notice the difference between standard output and standard error when you're in an interactive Terminal session – because both outputs are sent to the Terminal. But they are separate streams, with different meanings. When running batch programs and scripts you will want to manipulate standard output and standard error from programs appropriately.
To take the standard output of a program and save it to a file, you use the > operator
a single > overwrites any existing target; a double >> appends to it
since standard output is stream #1, this is the same as 1>
To redirect the standard error of a program you must specify its stream number using 2>
To redirect standard output and standard error to the same place, use the syntax 2>&1