Redirecting CLI outputs using file descriptors

A terminal shell (bash, zsh) in Unix-like systems provide three standard file descriptors for any file in the filesystem.

There are other cool things you can do with file descriptors but this post is just as a quick guide to some specific use cases - which is to redirect terminal outputs.

Let's see what we can do with the default file descriptors:

  1. Redirect STDOUT of a command to a file.

    ls 1> file_list
    

    Even if we skip to mention file descriptor in our commands, it is defaulted to 1 (STDOUT)

    ls > file_list
    
  2. Redirect STDERR of a command to a file.

    ls 2> file
    
  3. Redirect both STDOUT and STDERR to a file.

    ls -ltra &>file
    # another way to do it
    ls -ltra >&file
    
  4. Redirect both STDOUT and STDERR to the same destination one after another.

    # STDOUT is redirected first, followed by STDERR
    ls -a >file 2>&1
    

This can be really handy when you're working on linux/osx terminals. Cheers!