Key Concepts

Review core concepts you need to learn to master this subject

Append Redirect shell command

# This command will append "Hello World!" to greetings.txt echo "Hello World!" >> greetings.txt

The >> shell command is used to redirect the standard output of the command on the left and append (add) it to the end of the file on the right.

Pipe shell command

# This command will append "Hello World!" to greetings.txt echo "Hello World!" >> greetings.txt

The | command is called a pipe. It is used to pipe, or transfer, the standard output from the command on its left into the the standard input of the command on its right.

Redirecting Output

# This command will append "Hello World!" to greetings.txt echo "Hello World!" >> greetings.txt

The > symbol is used to redirect output by taking the output from the command on the left and passing as input to the file on the right.

cat Display

# This command will append "Hello World!" to greetings.txt echo "Hello World!" >> greetings.txt

The shell command cat displays the contents of one or more files to the terminal.

grep Search

# This command will append "Hello World!" to greetings.txt echo "Hello World!" >> greetings.txt

The shell command grep is used to search files for lines that match a pattern and returns the results. Various options can be specified along with the grep command to specify the search.

In the provided example, the lines in the file names.txt which contain “sonny” will be returned.

Case insensitive search

# This command will append "Hello World!" to greetings.txt echo "Hello World!" >> greetings.txt

The shell grep command searches files for a particular pattern. The grep command with the -i option can be used to search files for lines that match a pattern, case insensitive, and returns the results.

grep -R shell command

# This command will append "Hello World!" to greetings.txt echo "Hello World!" >> greetings.txt

The shell command grep has a -R option (grep -R) that searches all files in a directory, including its subdirectories, and outputs filenames and lines containing matched results.

Command Line Redirection

# This command will append "Hello World!" to greetings.txt echo "Hello World!" >> greetings.txt

On a command line, redirection is the process of using the input/output of a file or command to use it as an input for another file. It is similar but different from pipes, as it allows reading/writing from files instead of only commands.

Redirection can be done by using the operators > and >>.

Redirection
Lesson 1 of 1
  1. 1
    Up until now, we have run commands in the command line and received a stream of output in the terminal. In this lesson, we’ll focus on input and output (I/O) redirection. Through redirection yo…
  2. 2
    What happens when you type this command? $ echo “Hello” Hello The echo command accepts the string “Hello” as standard input, and echoes the string “Hello” back to the terminal as *standard ou…
  3. 3
    How does redirection work? $ echo “Hello” > hello.txt The > command redirects the standard output to a file. Here, “Hello” is entered as the standard input. The standard output “Hello” is redire…
  4. 4
    $ cat oceans.txt > continents.txt > takes the standard output of the command on the left, and redirects it to the file on the right. Here the standard output of cat oceans.txt is redirected to **…
  5. 5
    $ cat glaciers.txt >> rivers.txt >> takes the standard output of the command on the left and appends (adds) it to the file on the right. You can view the output data of the file with cat and th…
  6. 6
    $ cat < lakes.txt < takes the standard input from the file on the right and inputs it into the program on the left. Here, lakes.txt is the standard input for the cat command. The standard out…
  7. 7
    $ cat volcanoes.txt | wc | is a “pipe”. The | takes the standard output of the command on the left, and pipes it as standard input to the command on the right. You can think of this as “comma…
  8. 8
    $ sort lakes.txt sort takes the standard input and orders it alphabetically for the standard output. Here, the lakes in sort lakes.txt are listed in alphabetical order. $ cat lakes.txt | sort >…
  9. 9
    $ uniq deserts.txt uniq stands for “unique” and filters out adjacent, duplicate lines in a file. Here uniq deserts.txt filters out duplicates of “Sahara Desert”, because the duplicate of ‘Sahara…
  10. 10
    $ grep Mount mountains.txt grep stands for “global regular expression print”. It searches files for lines that match a pattern and returns the results. It is also case sensitive. Here, grep sear…
  11. 11
    $ grep -R Arctic /home/ccuser/workspace/geography grep -R searches all files in a directory and outputs filenames and lines containing matched results. -R stands for “recursive”. Here grep -R sea…
  12. 12
    $ sed ‘s/snow/rain/‘ forests.txt sed stands for “stream editor”. It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to “find a…
  13. 13
    Congratulations! You learned how to use the command line to redirect standard input and standard output. What can we generalize so far? Redirection* reroutes standard input, standard output, an…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo