Skip to main content
eLearner.app
Module 3 · Lesson 1 of 26/11 in the course~15 min
Module lessons (1/2)

Output and redirects (echo, cat, >)

In Linux, almost all commands read data from an input stream and write results to an output stream. These channels are called standard streams:

  • Standard Input (stdin): The stream from which the command reads its input (default is the keyboard).
  • Standard Output (stdout): The stream to which the command writes its successful output (default is the terminal screen).
  • Standard Error (stderr): The stream to which the command writes its error messages (default is the screen).

Using the command line, we can redirect these streams to save results directly into files.


Printing Text: echo

The echo command simply writes its arguments to its standard output (the screen):

Bash
echo "Hello everyone"

Redirecting Output (Overwrite): >

If you want to save the output of a command to a file instead of displaying it on the screen, you can use the greater-than redirect operator >:

Bash
echo "Hello everyone" > greetings.txt

This command runs echo and redirects its output to the file greetings.txt.

[!IMPORTANT] If the file greetings.txt does not exist, it is created. If it already exists, its existing content is completely deleted and overwritten with the new output.


Reading File Contents: cat

To view the content of one or more text files on the fly, use the cat command (short for concatenate):

Bash
cat greetings.txt

Appending Output: >>

If you want to append new lines to the end of an existing file rather than overwriting it, you must use the append operator >>:

Bash
echo "New line of text" >> greetings.txt

Now, reading the file with cat greetings.txt will show both the first line and the second line.


Usage Examples and Common Errors

You can pass multiple files to cat at the same time to display them sequentially (concatenated):

Bash
cat notes.txt info.txt
# Output:
# Keep learning Linux!
# Linux is awesome.

You can redirect the combined output to a third file using the > operator:

Bash
cat notes.txt info.txt > combined.txt

If you try to read a file that does not exist, cat will write an error message:

Bash
cat nonexistent_file.txt
# Output:
# cat: nonexistent_file.txt: No such file or directory

Try it yourself

Exercise 1: Create a file with a greeting

Exercise#linux.m3.l1.e1
Attempts: 0Loading…

Create a file named 'hello.txt' containing exactly the text 'Hello World' using echo and the > operator.

Loading editor…
Show hint

Use echo followed by the string in quotes, the > operator, and hello.txt.

Solution available after 3 attempts

Exercise 2: Append notes to a file

Exercise#linux.m3.l1.e2
Attempts: 0Loading…

Append the line 'Linux is fun' to the end of the existing 'notes.txt' file without deleting its current contents.

Loading editor…
Show hint

Use the >> operator to append text to the notes.txt file.

Solution available after 3 attempts

Exercise 3: Concatenate files

Exercise#linux.m3.l1.e3
Attempts: 0Loading…

Concatenate the contents of the files 'notes.txt' and 'info.txt' and save the combined output to a new file named 'combined.txt'.

Loading editor…
Show hint

Use cat followed by the names of the two source files and redirect the output with > to the destination file.

Solution available after 3 attempts