Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

How to redirect the output/error of command to file on Linux?

Posted on July 8, 2021July 8, 2021 by admin

File descriptors:

These are files that get opened as soon as you run any command in the terminal.

  • 0 – Standard Input (STDIN)
  • 1 – Standard Output (STDOUT)
  • 2 – Standard Error(STDERR)

Redirection ( > or < ) operators:

> – This operator is used to redirect the output of a command to a file or a device(again it’s a file as well). Also, This operator overwrites the file contents if the file has the contents.

< – This operator is used to pass inputs to a command from a file.

>> – This operator appends the contents in the file.

How to redirect the output of a command to a file?

ls -l > results.out

How to redirect the output and error of the command to a file?

ls -l > results.out 2>&1
#Above command simply telling that first redirect output to a file and then pass the error to output.  

How to redirect the output and error of the command to two different files separately?

ls -l > results.out 2> results.err
©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme