Key Concepts

Review core concepts you need to learn to master this subject

Command Line Redirection

ls > directories_list.txt ls >> directories_list.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 >>.

Redirecting Output

ls > directories_list.txt ls >> directories_list.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 shell command

ls > directories_list.txt ls >> directories_list.txt

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

Append Redirect shell command

ls > directories_list.txt ls >> directories_list.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

ls > directories_list.txt ls >> directories_list.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.

grep shell command

ls > directories_list.txt ls >> directories_list.txt

The grep shell command 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 example code block, the lines in the file names.txt which contain bob will be returned.

Case insensitive search

ls > directories_list.txt ls >> directories_list.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

ls > directories_list.txt ls >> directories_list.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.

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? […] The […] command accepts the string “Hello” as standard input, and echoes the string “Hello” back to the terminal as standard output. Let’…

  3. 3

    How does redirection work? […] The […] command redirects the standard output to a file. Here, […] is entered as the standard input. The standard output […] is redirected by […]…

  4. 4

    […] […] takes the standard output of the command on the left, and redirects it to the file on the right. Here the standard output of […] is redirected to continents.txt. Note that…

  5. 5

    […] […] 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 […] and the filename. Her…

  6. 6

    […] […] 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 […] command. The standard outp…

  7. 7

    […] […] 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 “command to co…

  8. 8

    […] […] takes the standard input and orders it alphabetically for the standard output. Here, the lakes in […] are listed in alphabetical order. […] Here, the command takes the st…

  9. 9

    […] […] stands for “unique” and filters out adjacent, duplicate lines in a file. Here […] filters out duplicates of “Sahara Desert”, because the duplicate of ‘Sahara Desert’ directly f…

  10. 10

    […] […] 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, […] searches for “Mount…

  11. 11

    […] […] searches all files in a directory and outputs filenames and lines containing matched results. […] stands for “recursive”. Here […] searches the **/home/ccuser/workspace/geo…

  12. 12

    […] […] 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 and replace”. Let’s lo…

  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