• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

RHEL 7 – RHCSA Notes – input / output redirection

by admin

RHEL 7 – RHCSA Notes (Cheat Sheets)

Three standard file descriptors :

1. stdin 0 - Standard input to the program.
2. stdout 1 - Standard output from the program.
3. stderr 2 - Standard error output from the program.
Purpose Command
redirect std output to filename > filename or 1> filename
append std out to filename >> filename
append std out and std err to filename >> filename 2>&1 or 1>> filename 2>&1
take input from filename < filename or 0 < filename
redirect std error to filename 2> filename
redirect std out and std error to filename 1> filename 2>&1 or > filename 2>&1

Some examples of using I/O redirection

# cat goodfile badfile 1> output 2> errors

This command redirects the normal output (contents of goodfile) to the file output and sends any errors (about badfile not existing, for example) to the file errors.

# mail user_id < textfile 2> errors

This command redirects the input for the mail command to come from file textfile and any errors are redirected to the file errors.

# find / -name xyz -print 1> abc 2>&1

This command redirects the normal output to the file abc. The construct “2>&1” says “send error output to the same place we directed normal output”.

Note that the order is important; command 2>&1 1>file does not do the same as command 1>file 2>&1. This is because the 2>&1 construction means redirect standard error to the place where standard output currently goes. The construction command 2>&1 1>file will first redirect standard error to where standard output goes (probably the terminal, which is where standard error goes by default anyway) then will redirect standard output to file. This is probably not what was intended.
# ( grep Bob filex > out ) 2> err

– any output of the grep command is sent to the file out and any errors are sent to the file err.

# find . -name xyz -print 2>/dev/null

This runs the find command, but sends any error output (due to inaccessible directories, for example), to /dev/null. Use with care, unless error output really is of no interest.

Filed Under: CentOS/RHEL 7, Linux, RHCSA notes

Some more articles you might also be interested in …

  1. debootstrap: command not found
  2. pw-link Command Examples in Linux
  3. How to Configure ACL(Access Control Lists) in Linux FileSystem
  4. mailq Command Examples in Linux
  5. setxkbmap: command not found
  6. “error while loading shared libraries: libjli.so: cannot open shared object file: No such file or directory” ‘java -version’ error on Startup
  7. OpenLDAP Server and Client Utilities list
  8. blender Command Examples (Command-line interface to the Blender 3D computer graphics application)
  9. calibredb: Tool to manipulate the your e-book database
  10. protonvpn-cli connect Command Examples

You May Also Like

Primary Sidebar

Recent Posts

  • “glab issue” Command Examples
  • “glab auth” Command Examples
  • “glab alias” Command Examples
  • gixy Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright