💻
Application Security Cheat Sheet
  • Application Security Cheat Sheet
  • Android Application
    • Overview
      • Application Data & Files
      • Application Package
      • Application Sandbox
      • Application Signing
      • Deployment
      • Package Manager
    • Intent Vulnerabilities
      • Deep Linking Vulnerabilities
    • WebView Vulnerabilities
      • WebResourceResponse Vulnerabilities
      • WebSettings Vulnerabilities
  • CI/CD
    • Dependency
      • Dependency Confusion
      • Dependency Hijaking
      • Typosquatting
    • GitHub
      • GitHub Actions
      • Code owners
      • Dependabot
      • Redirect
      • Releases
  • Cloud
    • AWS
      • Amazon API Gateway
      • Amazon Cognito
      • Amazon S3
  • Container
    • Overview
      • Container Basics
      • Docker Engine
    • Escaping
      • CVE List
      • Exposed Docker Socket
      • Excessive Capabilities
      • Host Networking Driver
      • PID Namespace Sharing
      • Sensitive Mounts
    • Container Analysis Tools
  • Framework
    • Spring
      • Overview
      • Mass Assignment
      • Routing Abuse
      • SpEL Injection
      • Spring Boot Actuators
      • Spring Data Redis Insecure Deserialization
      • Spring View Manipulation
    • React
      • Overview
      • Security Issues
  • Linux
    • Overview
      • Philosophy
      • File
      • File Descriptor
      • I/O Redirection
      • Process
      • Inter Process Communication
      • Shell
      • Signals
      • Socket
      • User Space vs Kernel Space
    • Bash Tips
  • iOS Application
    • Overview
      • Application Data & Files
      • Application Package
      • Application Sandbox
      • Application Signing
      • Deployment
    • Getting Started
      • IPA Patching
      • Source Code Patching
      • Testing with Objection
  • Resources
    • Lists
      • Payloads
      • Wordlists
    • Researching
      • Web Application
      • Write-ups
    • Software
      • AWS Tools
      • Azure Tools
      • Component Analysis
      • Docker Analysis
      • Dynamic Analysis
      • Fuzzing
      • GCP Tools
      • Reverse Engineering
      • Static Analysis
      • Vulnerability Scanning
    • Training
      • Secure Development
  • Web Application
    • Abusing HTTP hop-by-hop Request Headers
    • Broken Authentication
      • Two-Factor Authentication Vulnerabilities
    • Command Injection
      • Argument Injection
    • Content Security Policy
    • Cookie Security
      • Cookie Bomb
      • Cookie Jar Overflow
      • Cookie Tossing
    • CORS Misconfiguration
    • File Upload Vulnerabilities
    • GraphQL Vulnerabilities
    • HTML Injection
      • base
      • iframe
      • link
      • meta
      • target attribute
    • HTTP Header Security
    • HTTP Request Smuggling
    • Improper Rate Limits
    • JavaScript Prototype Pollution
    • JSON Web Token Vulnerabilities
    • OAuth 2.0 Vulnerabilities
      • OpenID Connect Vulnerabilities
    • Race Condition
    • Server Side Request Forgery
      • Post Exploitation
    • SVG Abuse
    • Weak Random Generation
    • Web Cache Poisoning
Powered by GitBook
On this page
  1. Linux
  2. Overview

I/O Redirection

On UNIX the streams for getting input and writing output are predefined. There are always three default files open:

  • stdin is the input data source (the keyboard)

  • stdout is the output data source (the screen)

  • stderr is the standard error output source (error messages output to the screen)

These, and any other open files, can be redirected. Redirection simply means capturing output from a file, command, program, script, or even code block within a script and sending it as input to another file, command, program, or script.

## command_output >
# Redirect stdout to a file
# Creates the file if not present, otherwise overwrites it.
$ ls -la > ls_result.txt

## : > filename
# The '>' truncates file 'filename; to zero length
# If file not present, creates zero-length file (same effect as 'touch')
# The ':' serves as a dummy placeholder, producing no output
$ : > empty_file.txt

## command_output >>
# Redirect stdout to a file
# Creates the file if not present, otherwise appends to it
$ ls -la >> ls_result.txt
## m>n
# 'm' is a file descriptor, which defaults to 1, if not explicitly set
# 'n' is a filename
# File descriptor 'm' is redirect to file 'n'
$ ls -la > ls_result.txt
$ ls -la 1>ls_result.txt
$ ./script.sh 2>error.log

## m>&n
# 'm' is a file descriptor, which defaults to 1, if not set
# 'n' is another file descriptor
$ ./script.sh 2>&1 > log.txt

## &>filename
# Redirect both stdout and stderr to file 'filename'
$ ./script.sh &>filename

## < filename
# Accept input from a file
$ grep "*some*" <filename
$ grep "*some*" 0<filename

You can ensure none of your redirects clobber an existing file by setting the noclobber option in the shell:

$ set -o noclobber
$ sort file.txt > file.txt
-bash: file.txt: cannot overwrite existing file

References

PreviousFile DescriptorNextProcess

Last updated 3 years ago

Each open file gets assigned a . The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively. For opening additional files, there remain descriptors 3 to 9.

file descriptor
How Unix Works: Become a Better Software Engineer
Advanced Bash-Scripting Guide: I/O Redirection