Study guide

6.1 Standard Streams, Redirection & File Descriptors (103.4)

LPIC-1 Linux Administrator — Exam 101 101-500

Track your progress as you read

Finished sections get a check mark.

1 Exam Overview & Study Strategy

1.1 LPIC-1 101-500 Exam Structure & Blueprint 1.2 Command Line & Shell Fundamentals for Exam Success 1.3 Fill-in-the-Blank Strategies & Common Traps

2 Topic 101: System Architecture

2.1 Hardware Discovery, Sysfs, Procfs & Kernel Modules (101.1) 2.2 Udev, Dbus & Device Management (101.1) 2.3 BIOS vs. UEFI & System Boot Process (101.2) 2.4 Kernel Initialization, Initramfs & System Logs (101.2) 2.5 Systemd Boot Targets & Unit Management (101.3) 2.6 SysVinit Runlevels, Telinit & System Shutdown (101.3)

3 Topic 102: Linux Installation & Boot Management

3.1 Hard Disk Layout, Partition Schemes & Swap Design (102.1) 3.2 LVM & Standard Mount Point Allocation (102.1) 3.3 GRUB 2 Configuration & Troubleshooting (102.2) 3.4 Legacy GRUB & Alternative Boot Loaders (102.2) 3.5 Managing Shared Libraries & ldconfig (102.3) 3.6 Linux as a Virtualization Guest & Cloud Instances (102.6)

4 Topic 102: Package Management Systems

4.1 Debian Low-Level Package Management with dpkg (102.4) 4.2 High-Level Debian Package Management: apt, apt-get & apt-cache (102.4) 4.3 RPM Package Management with rpm (102.5) 4.4 YUM, DNF & Zypper Package Management (102.5) 4.5 Package Repositories & Integrity Verification (102.4/102.5) 4.6 Universal Linux Packages: Snap, Flatpak & AppImage (102.4/102.5)

5 Topic 103: Command Line & Text Processing

5.1 Shell Environment, Environment Variables & Aliases (103.1) 5.2 Command History, Quoting & Expansion (103.1) 5.3 Basic Text Filters: cat, head, tail, cut, paste, nl, od (103.2) 5.4 Advanced Text Filters: sort, uniq, wc, tr, split, less & Checksums (103.2) 5.5 Stream Editing with sed (103.2) 5.6 Basic File Management: cp, mv, rm, touch, mkdir, rmdir (103.3) 5.7 Archiving and Compression: tar, gzip, bzip2, xz, cpio & dd (103.3)

6 Topic 103: Streams, Processes, Regex & Vi

6.1 Standard Streams, Redirection & File Descriptors (103.4) 6.2 Pipelines, tee & xargs (103.4) 6.3 Process Monitoring: ps, top, free, watch, pgrep, pstree & uptime (103.5) 6.4 Signals, kill, Job Control, nohup, screen & tmux (103.5) 6.5 Process Priorities: nice & renice (103.6) 6.6 Regular Expressions & grep, egrep, fgrep (103.7) 6.7 vi / vim Modes, plus nano, Emacs & EDITOR Awareness (103.8)

7 Topic 104: Devices, Partitions & Filesystems

7.1 Block Devices, fdisk & gdisk (104.1) 7.2 GNU Parted & Partition Table Management (104.1) 7.3 Creating Filesystems: mkfs, ext4, XFS, VFAT/exFAT & Btrfs (104.1) 7.4 Filesystem Integrity: fsck, e2fsck, xfs_repair, xfs_fsr & xfs_db (104.2) 7.5 Monitoring Disk Usage: df, du & tune2fs (104.2) 7.6 Managing Swap Space: mkswap, swapon & swapoff (104.1/104.2)

8 Topic 104: Mounts, Permissions, Links & FHS

8.1 Mounting and Unmounting Filesystems: mount & umount (104.3) 8.2 Persistent Mounts: /etc/fstab, UUIDs & systemd mount units (104.3) 8.3 Standard File Permissions: chmod, chown, chgrp & umask (104.5) 8.4 Special Permissions: SUID, SGID & Sticky Bit (104.5) 8.5 Hard Links vs. Symbolic Links: ln & Inodes (104.6) 8.6 Filesystem Hierarchy Standard (FHS) & File Location (104.7)

6.1 Standard Streams, Redirection & File Descriptors (103.4)

Key Takeaways Explain This Topic AI

Every POSIX process initializes with three standard I/O streams bound to numeric file descriptors: standard input (stdin, FD 0), standard output (stdout, FD 1), and standard error (stderr, FD 2).

Redirection operators alter stream targets: > truncates/overwrites stdout, >> appends stdout, < reads stdin from a file, and << DELIMITER creates an inline multi-line Here Document.

Diagnostic error streams are redirected via 2> or 2>> , merged into standard output using 2>&1 , or combined in Bash using &> and &>> ; order of evaluation is evaluated strictly left-to-right.

Unwanted data streams are discarded by redirecting to the /dev/null special character device.

Clobber protection is enabled via set -o noclobber (or set -C ) to prevent accidental truncation with > , and can be explicitly overridden using the >| operator.

Last updated: August 2026

6.1 Standard Streams, Redirection & File Descriptors

Quick Summary: Under the POSIX standard and the Unix philosophy, programs perform input and output operations through standardized byte streams. The Linux kernel assigns three default streams to every newly created process, represented by integer file descriptors: 0 for Standard Input ( stdin ), 1 for Standard Output ( stdout ), and 2 for Standard Error ( stderr ). Using shell redirection operators ( < , > , >> , 2> , 2>&1 , &> , << , <<< ), administrators can decouple programs from physical terminal hardware, routing data between files, processes, and virtual devices like /dev/null .

1. POSIX Standard Streams and File Descriptors (FD)

In Linux, the operating system treats virtually all resources—including regular files, directories, sockets, pipes, and hardware devices—as streams of bytes accessible via the filesystem. When a process opens a resource or is launched by the shell, the Linux kernel assigns it an unsigned integer handle known as a File Descriptor (FD) .

By convention, every standard process starts execution with three file descriptors automatically opened and mapped to the controlling terminal session ( /dev/pts/N or /dev/ttyN ):

Stream Name POSIX Symbolic Constant File Descriptor (FD) Default Source / Destination Standard Usage & Operational Role

Standard Input STDIN_FILENO 0 Keyboard / Terminal ( /dev/tty ) Supplies input text, stream payloads, or interactive keystrokes to the process.

Standard Output STDOUT_FILENO 1 Terminal Display Screen Receives standard informational output and program results generated during successful execution.

Standard Error STDERR_FILENO 2 Terminal Display Screen Receives diagnostic warnings, syntax errors, traces, and execution failure messages. Default Process Stream Binding Architecture:

  • ┌────────────────────────┐
  • │ Linux Process │
  • Keyboard ───>│ FD 0 (stdin) │
  • (/dev/tty) │ │
  • │ FD 1 (stdout) ────────>│ Display Screen
  • │ FD 2 (stderr) ────────>│ (/dev/tty)
  • └────────────────────────┘
  • Inspecting File Descriptors via /proc

The kernel exposes the active file descriptor bindings of every running process through the virtual procfs filesystem under /proc/<PID>/fd/ . You can inspect the descriptor mappings of your current interactive shell (represented by the special variable $$ ): $ ls -l /proc/$$/fd

  • total 0
  • lrwx------ 1 admin admin 64 Aug 29 10:00 0 -> /dev/pts/1
  • lrwx------ 1 admin admin 64 Aug 29 10:00 1 -> /dev/pts/1
  • lrwx------ 1 admin admin 64 Aug 29 10:00 2 -> /dev/pts/1
  • lrwx------ 1 admin admin 64 Aug 29 10:00 255 -> /dev/pts/1

All three descriptors point directly to the pseudo-terminal device /dev/pts/1 . Redirection is the mechanism by which the shell alters these target pointers before launching an executable binary.

2. Standard Output and Error Redirection Operators

Redirection operators instruct the shell to intercept a stream and bind its file descriptor to a file or another descriptor instead of the default terminal display.

Output Redirection Operators Reference

Operator Target Stream Behavioral Action Truncate vs. Append

> (or 1> ) stdout (FD 1) Redirects standard output to a file; creates file if nonexistent, overwrites if existing. Truncate (Overwrites existing data)

>> (or 1>> ) stdout (FD 1) Redirects standard output to a file; creates file if nonexistent, appends to end of file. Append (Preserves existing data)

  • 2> stderr (FD 2) Redirects standard error messages to a file; creates or overwrites file. Truncate
  • 2>> stderr (FD 2) Redirects standard error messages to a file; creates or appends to file. Append

2>&1 stderr (FD 2) Duplicates FD 2 to wherever FD 1 currently points (merges stderr into stdout). Inherits target mode

1>&2 stdout (FD 1) Duplicates FD 1 to wherever FD 2 currently points (routes stdout into stderr stream). Inherits target mode

&> (or >& ) stdout + stderr Redirects both stdout (FD 1) and stderr (FD 2) simultaneously to a single file. Truncate (Bash extension)

&>> stdout + stderr Appends both stdout (FD 1) and stderr (FD 2) simultaneously to a single file. Append (Bash extension)

Practical Demonstration: Separating stdout and stderr

Consider running find across /etc/ as an unprivileged user. Permitted directories yield valid paths (stdout), while restricted files produce permission warnings (stderr): # 1. Default behavior: stdout and stderr intermingle on terminal display

  • $ find /etc -name "*.conf"
  • /etc/resolv.conf
  • find: ‘/etc/ssl/private’: Permission denied
  • /etc/sysctl.conf

# 2. Redirect standard output to a file, leaving errors on the screen:

  • $ find /etc -name "*.conf" > conf_files.txt
  • find: ‘/etc/ssl/private’: Permission denied

# 3. Redirect standard error to an error log, displaying successful results on screen:

  • $ find /etc -name "*.conf" 2> errors.log
  • /etc/resolv.conf
  • /etc/sysctl.conf

# 4. Simultaneously route stdout and stderr to two separate files:

  • $ find /etc -name "*.conf" > conf_files.txt 2> errors.log
  • 3. Combining Streams: The Order of Evaluation Rule

A critical LPIC-1 exam objective tests how stdout and stderr are merged into a single destination file. The classic POSIX construct is: command > file.log 2>&1 Step-by-Step Shell Evaluation of `command > file.log 2>&1`:

1. `> file.log` --> Shell redirects FD 1 (stdout) to open `file.log`.

2. `2>&1` --> Shell duplicates FD 2 (stderr) to point to the current target of FD 1 (`file.log`).

Result: Both stdout and stderr write into `file.log`.

⚠️ LPIC-1 Trap — Order of Redirection Evaluation Matters: If you invert the order and write: command 2>&1 > file.log

The shell evaluates from left to right:

2>&1 : FD 2 (stderr) is redirected to where FD 1 points at that moment (the terminal display).

> file.log : FD 1 (stdout) is redirected to file.log . Result: stdout is written to file.log , but stderr continues to print directly to the screen! On the LPIC-1 exam, command > file 2>&1 is the correct portable POSIX syntax.

In modern Bash, the shorthand &> and &>> achieve identical results with fewer keystrokes: # Truncate and write both streams to output.log:

$ systemctl restart nginx &> /var/log/nginx_init.log

# Append both streams to output.log:

  • $ systemctl restart nginx &>> /var/log/nginx_init.log
  • 4. Standard Input Redirection: Files, Here Documents & Here Strings

While output redirection extracts data from programs, input redirection feeds data into a program's stdin (FD 0).

1. File Input Redirection ( < )

Instead of reading keystrokes from the terminal keyboard, < binds FD 0 to an existing file on disk: # Send email content from a file

$ mail -s "Daily Backup Report" admin@example.com < /var/log/backup.log

  • # Count lines using stdin redirection rather than passing an argument
  • $ wc -l < /etc/passwd
  • 42
  • 2. Here Documents ( << DELIMITER )

A Here Document (Heredoc) provides multi-line input directly within a shell script or interactive prompt until a designated delimiter token is encountered on a line by itself: $ cat << EOF > /etc/motd

  • ========================================
  • Welcome to Enterprise Linux Server 9

Authorized Access Only!

  • ========================================
  • EOF
  • The <<- Tab Stripping Variant

When indenting shell scripts, leading tab characters can break formatting. Using <<- instructs the shell to strip all leading tab characters (not spaces) from each line of input and from the closing delimiter: if [ -f /etc/config.json ]; then

  • cat <<- 'CONFIG_EOF' > /tmp/parsed.json
  • {
  • "status": "active",
  • "environment": "production"
  • }
  • CONFIG_EOF
  • fi

Exam Tip — Quoting the Heredoc Delimiter: If the delimiter is unquoted ( << EOF ), variable expansion ( $VAR ) and command substitution ( $(date) ) are evaluated before writing. If the delimiter is quoted with single or double quotes ( << 'EOF' or << "EOF" ), all text inside the heredoc is treated literally, suppressing all parameter and command expansions.

3. Here Strings ( <<< "string" )

Introduced in GNU Bash, a Here String feeds a single string variable or literal directly into stdin without requiring an external echo or printf pipe: # Filter a string using bc arithmetic via Here String

  • $ bc <<< "scale=4; 355/113"
  • 3.1415
  • # Pass variable text to tr
  • $ tr '[:lower:]' '[:upper:]' <<< "linux professional institute"
  • LINUX PROFESSIONAL INSTITUTE
  • 5. Output Discarding and Clobber Protection
  • Discarding Output via /dev/null

/dev/null is a special character device in Linux known as the "bit bucket" or black hole. Any data written to /dev/null is immediately discarded by the kernel, and read operations return an immediate End-Of-File (EOF). # Suppress both standard output and error messages silently:

$ ping -c 3 192.168.1.1 > /dev/null 2>&1

# Modern Bash equivalent:

$ ping -c 3 192.168.1.1 &> /dev/null

# Check only the exit status ($?) without cluttering terminal output:

  • $ if grep -q "root" /etc/passwd > /dev/null 2>&1; then echo "User exists"; fi
  • Clobber Protection: set -o noclobber and >|

By default, the > operator will overwrite and truncate an existing file without prompting, potentially causing catastrophic data loss if an administrator accidentally redirects output to a critical system configuration.

To prevent this, the shell provides the noclobber option:

set -o noclobber (or set -C ): Enables overwrite protection. If target file exists, redirection with > aborts with an error.

  • set +o noclobber (or set +C ): Disables protection (default shell state). $ set -o noclobber
  • $ echo "Important Configuration" > /etc/important.conf
  • $ echo "New Line" > /etc/important.conf
  • bash: /etc/important.conf: cannot overwrite existing file
  • The Force Override Operator: >|

When noclobber is active, an administrator can explicitly force the truncation and overwrite of an existing file using the >| operator: $ echo "Forced New Configuration" >| /etc/important.conf

# Successfully overwrites the file despite noclobber being active

💡 LPIC-1 Exam Fill-in-the-Blank Alert: What shell operator forces output redirection to truncate and overwrite an existing file when the noclobber shell option is enabled? Answer: >|

Loading diagram... Linux Standard Stream Redirection Architecture