Skip to content

Basic Linux

Learning objectives

  • Practice some basic Linux commands: echo, printf, cat, wc, rev
  • Practice with Linux pipes
  • Use the output of one process as the input for awk
For teachers

Teaching goals are:

  • The learners have practiced with UNIX pipes

Lesson plan:

  • 5 mins: prior knowledge
  • 5 mins: presentation
  • 15 mins: challenge
  • 5 mins: feedback

Overview

Most UNIX (i.e. Linux and MacOS) distributions includes awk as it is a tool that is part of a common standard for what operating systems should have. The UPPMAX clusters, running Linux, also have awk.

Here we discuss the most relevant Linux programs and terms.

flowchart TD

  subgraph basic_linux[Basic Linux]
    awk
    pipes
    echo
    printf
    stdin
    files[Files]
    input[Input]
    cat
    editor[Text editor]
    wc
    rev
    tr
  end

  %% Basic Linux
  stdin --> |need to know| pipes
  printf --> |need to know| echo
  pipes --> |need to know| printf
  files --> |need to know| cat
  files --> |need to know| editor
  files --> |a type of| input
  stdin --> |a type of| input
  awk --> |reads| input

Exercises

See the exercise procedure here.

Exercise 1: echo

Learning objectives

  • Use echo

Read:

Do, in a terminal:

  • man echo
  • echo hello
  • echo hello world
  • echo hello\nworld
  • echo hello\\nworld
  • echo -e hello\nworld
  • echo -e hello\\nworld
  • echo "Hello world"
  • echo -e "Hello\\nworld"
  • echo -e 'Hello\\nworld'

Answer the following questions:

  • echo can create output that spans multiple lines yes/no
  • When putting a newline in a string, the type of quotes (i.e. single or double) matters yes/no

Express in your own words: what does echo do?

Exercise 2: printf

Learning objectives

  • Use printf

Read:

Do, in a terminal:

  • man printf
  • printf hello
  • printf hello\n
  • printf hello\\n
  • printf hello world
  • printf hello\nworld
  • printf hello\\nworld
  • printf -e hello\nworld
  • printf -e hello\\nworld
  • printf "Hello world"
  • printf -e "Hello\\nworld"
  • printf -e 'Hello\\nworld'

Answer the following questions:

  • printf can create output that spans multiple lines yes/no
  • When putting a newline in a string, the type of quotes (i.e. single or double) matters yes/no

Express in your own words: what does printf do?

Exercise 3: cat

Learning objectives

  • Use cat

Read:

Do:

  • create a file called why_awk.txt with the following content (from here):
why_awk.txt
 "The Enlightened Ones say that....

    You should never use C if you can do it with a script;
    You should never use a script if you can do it with awk;
    Never use awk if you can do it with sed;
    Never use sed if you can do it with grep." 

Then, in a terminal, in the same folder as why_awk.txt, do:

  • man cat
  • cat why_awk.txt
  • cat -b why_awk.txt
  • cat -n why_awk.txt

Express in your own words: what does cat do?

Exercise 4: wc

Learning objectives

  • Use wc

Read:

Then, in a terminal, do:

  • man wc
  • wc why_awk.txt

Express in your own words: what does wc do?

Exercise 5: a UNIX pipeline

Learning objectives

  • Use a pipeline

Read:

The pipe symbol | is used in a UNIX pipeline.

Do, in a terminal:

  • echo hello world | rev
  • echo hello world | rev | rev
  • cat why_awk.txt | wc
  • cat why_awk.txt | rev
  • cat why_awk.txt | wc | rev
  • cat -n why_awk.txt | rev
  • rev --version | rev

Express in your own words: what does the pipe symbol | do?