Skip to main content
XsiSec.com
HomeReposBlogProjectsPortfolio
© 2026 XsiSec.com
Security rules |security.txt
Updated 2026-08-15 · v1.0.0+2026-08-14.82f92cb · 82f92cb
← Back to overview
Security article

HTB Academy - Fundamentals - File Descriptors and Redirections

HTB Academy - Fundamentals - File Descriptors and Redirections: File Descriptors A file descriptor (FD) in Unix/Linux operating systems is an indicator of connection maintained by the kernel to perform Input/Output (I/O) operations. In Windows-based operating systems, it is called filehandle. It is the connection (generally to a file) from the Operating system to perform I/O operations (Input/Output of... • HTB Academy • STDIN, STDOUT

2022-09-153 tags
Tags

File Descriptors

A file descriptor (FD) in Unix/Linux operating systems is a connection maintained by the kernel to perform input/output (I/O) operations.

In Windows-based operating systems, this is commonly called a file handle.

By default, the first three file descriptors in Linux are:

File DescriptorStreamDescription
0STDINStandard input
1STDOUTStandard output
2STDERRStandard error output

STDIN and STDOUT

The cat command provides a simple example of how standard input and standard output work.

When running cat, the program receives input through standard input:

text
STDIN - FD 0

After entering text and pressing [ENTER], the input is returned to the terminal through standard output:

text
STDOUT - FD 1

STDIN and STDOUT example

In simple terms:

  • STDIN - FD 0 is input.
  • STDOUT - FD 1 is output.
  • STDERR - FD 2 is error output.

STDOUT and STDERR

The find command can produce both standard output and standard error output.

Run the following command:

bash
find /etc/ -name shadow

STDOUT and STDERR example

Valid results are returned through STDOUT, while errors such as Permission denied are returned through STDERR.


Redirect STDERR to /dev/null

We can redirect standard errors to /dev/null using file descriptor 2:

bash
find /etc/ -name shadow 2>/dev/null

Redirect STDERR to dev null

The following part performs the redirection:

text
2>/dev/null
  • 2 represents STDERR.
  • > redirects the stream.
  • /dev/null discards anything written to it.

After redirecting STDERR, errors such as Permission denied are no longer displayed. Only standard output remains visible.


Redirect STDOUT to a File

Standard output can be redirected to a file using the greater-than operator:

bash
find /etc/ -name shadow 2>/dev/null > results.txt

Redirect STDOUT to a file

This command:

  1. Redirects errors to /dev/null.
  2. Redirects standard output to results.txt.

The following two redirections are equivalent:

bash
> results.txt
bash
1> results.txt

File descriptor 1 does not have to be specified because STDOUT is the default stream redirected by >.

If results.txt does not exist, it is created automatically. If it already exists, its contents are overwritten.


Redirect STDOUT and STDERR to Separate Files

Standard output and standard error can be redirected to different files:

bash
find /etc/ -name shadow 2> stderr.txt 1> stdout.txt

Redirect STDOUT and STDERR separately

This command creates two files:

FileContent
stdout.txtStandard output from file descriptor 1
stderr.txtStandard errors from file descriptor 2

Append STDOUT to a File

The single greater-than operator (>) overwrites the destination file:

bash
command > output.txt

The double greater-than operator (>>) appends output to the destination file without deleting its existing contents:

bash
find /etc/ -name passwd >> stdout.txt 2>/dev/null

Append STDOUT to a file

This command appends the results to stdout.txt and redirects errors to /dev/null.


Redirect STDIN from a File

The lower-than operator (<) redirects the contents of a file to the standard input of a command:

bash
cat < stdout.txt

In this example, cat receives the contents of stdout.txt through STDIN.


Redirect an STDIN Stream to a File

The double lower-than operator (<<) can be used to create a here-document.

A here-document allows multiple lines of input to be supplied to a command:

bash
cat << EOF > stream.txt
Hack The Box
EOF

Redirect an STDIN stream

In this example:

  • << EOF starts the input stream.
  • Hack The Box is the content supplied through STDIN.
  • The second EOF ends the input stream.
  • > stream.txt redirects the output to stream.txt.

The word EOF is a delimiter and can be replaced with another identifier.


Pipes

A pipe (|) sends the standard output of one command to the standard input of another command.

The following command searches for files ending in .conf and filters the results to include only lines containing systemd:

bash
find /etc/ -name "*.conf" 2>/dev/null | grep systemd

Pipe find output to grep

The command works as follows:

  1. find searches for .conf files.
  2. 2>/dev/null discards error messages.
  3. | sends the remaining output to grep.
  4. grep systemd displays only lines containing systemd.

Chain Multiple Pipes

Multiple pipes can be chained together.

The following command searches for .conf files, filters results containing systemd, and counts the number of matching lines:

bash
find /etc/ -name "*.conf" 2>/dev/null | grep systemd | wc -l

Chain multiple pipes

The output from each command becomes the input for the next command:

text
find → grep → wc

Exercises

How many .log files exist on the system?

bash
find / -iname "*.log" 2>/dev/null | wc -l

Command breakdown:

CommandDescription
find /Searches from the filesystem root
-iname "*.log"Finds files with the .log extension, ignoring case
2>/dev/nullDiscards error output
wc -lCounts the number of results

How many .bak files exist on the system?

bash
find / -iname "*.bak" 2>/dev/null | wc -l

Output:

text
4

Submit the Full Path of the xxd Binary

bash
whereis -b xxd

Output:

text
xxd: /usr/bin/xxd

The full path is:

text
/usr/bin/xxd
Navigate

In this post

  1. 01File Descriptors
  2. 02STDIN and STDOUT
  3. 03STDOUT and STDERR
  4. 04Redirect STDERR to /dev/null
  5. 05Redirect STDOUT to a File
  6. 06Redirect STDOUT and STDERR to Separate Files
  7. 07Append STDOUT to a File
  8. 08Redirect STDIN from a File
  9. 09Redirect an STDIN Stream to a File
  10. 10Pipes
  11. 11Chain Multiple Pipes
  12. 12Exercises
  13. 13How many .log files exist on the system?
  14. 14How many .bak files exist on the system?
  15. 15Submit the Full Path of the xxd Binary
Search
Explore

Popular tags

Browse all 30 tags

Comments

0 comments

No comments yet — be the first to comment.