2020 Linux fundamentals

2020 Linux fundamentals

This page should serve as a reference for the many "things Linux" we use in this course.

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.

Wildcards and special file names

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

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.

Three special file names:

  1. . (single period) means "this directory".

  2. .. (two periods) means "directory above current." So ls .. means "list contents of the parent directory."

  3. ~ (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.

Standard streams

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.

redirecting output

  • 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

To see the difference between standard output and standard error try these commands:

Output redirection examples
# redirect a long listing of your $HOME directory to a file ls -la $HOME > cmd.out # look at the contents -- you'll see just files cat cmd.out # this command gives an error because the target does not exist ls -la bad_directory # redirect any errors from ls to a file ls -la bad_directory 2> cmd.out # look at the contents -- you'll see an error message cat cmd.out # now redirect both error and output streams to the same place ls -la bad_directory $HOME > cmd.out # look at the contents -- you'll see both an error message and files cat cmd.out

Piping

The power of the Linux command line is due in no small part to the power of piping. The pipe symbol ( | ) connects one program's standard output to the next program's standard input.