0Pricing
DevOps Bootcamp · Lesson

Input/Output Redirection (>, >>, <)

Master redirecting command output to files and using files as input for commands, enhancing script flexibility.

Input/Output Redirection (>, >>, <) is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is I/O Redirection?

In Linux, commands often produce output or require input. Input/Output (I/O) redirection lets you change where a command gets its input from or sends its output to.

Instead of just seeing output on your screen (the terminal), you can save it to a file, or even feed a file's content directly into another command.

Understanding Standard Streams

Every Linux command interacts with three "standard" data streams:

  • Standard Input (STDIN): Where a command expects to receive input (usually your keyboard).
  • Standard Output (STDOUT): Where a command sends its normal output (usually your screen).
  • Standard Error (STDERR): Where a command sends error messages (also usually your screen).

Redirection allows us to change these default destinations or sources.

Capturing Output with '>'

The > operator is used to redirect Standard Output (STDOUT) from a command to a file. Instead of printing to the screen, the command's output will be written into the specified file.

Important: If the file already exists, > will overwrite its entire content with the new output. Be careful not to accidentally delete important data!

Example: Saving Command Output

Let's save the output of the ls -l command into a file named my_files.txt. Then, we'll view the file's content using cat.

ls -l > my_files.txt
cat my_files.txt

Adding to a File with '>>'

Sometimes you don't want to overwrite a file; you want to add new content to the end of it. That's where the >> operator comes in!

>> redirects Standard Output (STDOUT) to a file, but if the file already exists, it will append the new output to the end of the file, preserving existing content.

Example: Appending to a Log

First, create a simple file. Then, use >> to add another line to it. Observe how the original content is kept.

echo "First line of log." > app_log.txt
echo "Second line added." >> app_log.txt
cat app_log.txt

Handling Errors with '2>'

What about error messages? They go to Standard Error (STDERR). To redirect STDERR to a file, we use the file descriptor 2 followed by the > operator: 2>.

This is extremely useful for logging errors without cluttering your main output or for debugging scripts.

Example: Logging Error Messages

Let's try to list a directory that doesn't exist. The error message will normally show on screen. We'll redirect it to errors.log instead.

ls /this/path/does/not/exist 2> errors.log
echo "Command finished. Check errors.log."
cat errors.log

Providing Input with '<'

The < operator is used to redirect Standard Input (STDIN). Instead of a command waiting for you to type input from the keyboard, it will read its input directly from the specified file.

This is powerful for automating tasks where a command expects a lot of input, or when you want to process a file's content directly.

Example: Sorting from a File

We'll create a file with unsorted names, then use the sort command to read its input from that file using <.

echo -e "Charlie\nAlice\nBob" > names.txt
sort < names.txt

Quick Check: Redirection

You've learned about redirecting standard output, appending output, handling errors, and providing input from files. Let's test your understanding.

Recap: I/O Redirection Power

Great job! You've mastered the fundamentals of I/O redirection in Linux.

  • > redirects STDOUT, overwriting files.
  • >> redirects STDOUT, appending to files.
  • 2> redirects STDERR, capturing error messages.
  • < redirects STDIN, allowing commands to read from files.

These operators are incredibly powerful for scripting, logging, and automating tasks. Keep practicing!

Frequently asked questions

Is the “Input/Output Redirection (>, >>, <)” lesson free?

Yes — the full text of “Input/Output Redirection (>, >>, <)” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Input/Output Redirection (>, >>, <)”?

Master redirecting command output to files and using files as input for commands, enhancing script flexibility. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Input/Output Redirection (>, >>, <)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Managing Processes (ps, top, kill)
  2. Input/Output Redirection (>, >>, <)
  3. Piping Commands (|)
  4. Background Jobs & Job Control
← Back to DevOps Bootcamp