• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Linux-Ubuntu | Redirection/Pipe operators
  1. Standard input/output/error redirection operators
  2. STDIN redirection operator
  3. STDOUT redirection operator
  4. STDERR redirection operator
  5. Redirect the output to another "stream"
  6. Redirect both STDOUT and STDERR to the same "stream"
  7. pipe operator
  8. tee command

  1. Standard input/output/error redirection operators
    Redirection operators redirects the output/input of a script (or file) to another script (or file) as input/output.
    • "<", "0<": STDIN operator (file, keyboard, mouse, ...)

    • ">", "1>": STDOUT operator (file, monitor, printer, ...)

    • "2>": STDERR operator (file, monitor, printer, ...)

    • "&>": STDOUT & STDERR operator (file, monitor, printer, ...)

    STDIN: standard input
    STDOUT: standard output
    STDERR: standard error

    STDOUT and STDERR are two different streams of output.
  2. STDIN redirection operator
    • "<" STDIN redirection operator.

      Search the word "foo" in the file "file1":
      $ grep foo < file1

    • "0<" STDIN redirection operator (same as "<").

      Search the word "foo" in the file "file1":
      $ grep foo 0< file1

    • The use of the STDIN redirection operator "<" or "0<" is optional, as it's the default operator in the absence of an explicit redirection operator.

      Search the word "foo" in the file "file1":
      $ grep foo file1

    • You can also use the redirection operator "<<" that allow you to type a text that can be use a standard input for a command.
      The syntax for using this operator is as follow: command <<FLAG <ENTER> text <ENTER> ... FLAG<ENTER>
      The word flag will act as an indicator that you you have ended typing your text.

      Sort the text entered in the standard input (between <<end and end):
      $ sort <<end
      > aaa
      > ccc
      > bbb
      > end
      aaa
      bbb
      ccc
  3. STDOUT redirection operator
    • ">" STDOUT redirection operator

      Write the result of the comand "ls -al" in the file "file1":
      $ ls -al > file1

    • "1>" STDOUT redirection operator (same as ">")

      Write the result of the comand "ls -al" in the file "file1":
      $ ls -al 1> file1

    • ">>" STDOUT redirection operator + append

      Append the result of the comand "ls -al" in the file "file1":
      $ ls -al >> file1

    • "1>>" STDOUT redirection operator + append (same as >>)

      Append the result of the comand "ls -al" in the file "file1":
      $ ls -al 1>> file1
  4. STDERR redirection operator
    • "2>" STDERR redirection operator

      Write the error (ls: -: No such file or directory) of the comand "ls -" in the file "file1":
      $ ls - 2> file1

    • "2>>" STDERR redirection operator + append

      Append the error (ls: -: No such file or directory) of the comand "ls -" in the file "file1":
      $ ls - 2>> file1
  5. Redirect the output to another "stream"
    • "1>&2" redirect STDOUT to the same "stream" as STDERR

      Write/append the result of the command "ls -al" to the same "stream" as STDERR ("file1"):
      $ ls -al 2> file1 1>&2
      
      $ ls -al 2>> file1 1>&2

    • "2>&1" redirect STDERR to the same "stream" as STDOUT

      Write/append the error (ls: -: No such file or directory) of the command "ls -" to the same "stream" as STDOUT ("file1"):
      $ ls - > file1 2>&1
      
      $ ls - >> file1 2>&1
      
      $ ls - 1> file1 2>&1
      
      $ ls - 1>> file1 2>&1

    Redirect errors to /dev/null:
    $ ls - > file1 2>/dev/null
  6. Redirect both STDOUT and STDERR to the same "stream"
    "&>" both STDOUT and STDERR redirection operator.

    • Write the result of the comand "ls -al" in the file "file1":
      $ ls -al &> file1

    • Write the error (ls: -: No such file or directory) of the comand "ls -" in the file "file1":
      $ ls - &> file1
  7. pipe operator
    pipe operator redirects the output of a command (on the left of the pipe) as input to the next command (on the right of the pipe).

    # "|" pipe operator
    $ ps -ef | grep java
  8. tee command
    tee command redirects the output of a script to files and STDOUT.

    $ ls -1 | tee files
    file1
    file2
    file3
    
    $ tee file1_copy < file1
    file1 content
© 2025  mtitek