#readwise # Linux Fundamentals - File Descriptors and Redirections ![rw-book-cover](https://readwise-assets.s3.amazonaws.com/static/images/article4.6bc1851654a0.png) ## Metadata - Author: [[Hack The Box]] - Full Title: File Descriptors and Redirections - URL: https://academy.hackthebox.com/module/18/section/79 ## Summary File descriptors in Unix/Linux are unique identifiers that help manage Input/Output operations. They allow the operating system to track open files and resources, much like a ticket represents a coat in a coatroom. Users can redirect standard output and errors to different files or devices to manage data flow. Understanding file descriptors and redirections helps improve efficiency in handling data and commands. ## Highlights A file descriptor (`FD`) in Unix/Linux operating systems is a reference, maintained by the kernel, that allows the system to manage Input/Output (`I/O`) operations. It acts as a unique identifier for an open file, socket, or any other I/O resource. In Windows-based operating systems, this is known as a file handle. Essentially, the file descriptor is the system's way of keeping track of active `I/O` connections, such as reading from or writing to a file. ([View Highlight](https://read.readwise.io/read/01jm2tgeme8nkd1z56brhsqf4j)) --- ### Standard Input, Output and Error Streams By default, the first three file descriptors in Linux are: 1. Data Stream for Input: `STDIN – 0` 2. Data Stream for Output: `STDOUT – 1` 3. Data Stream for Output that relates to an error occurring: `STDERR – 2` ([View Highlight](https://read.readwise.io/read/01jm2th4kfj9yvxhsvdvpjmr39)) --- #### Redirect STDOUT and Append to a File When we use the greater-than sign (`>`) to redirect our `STDOUT`, a new file is automatically created if it does not already exist. If this file exists, it will be overwritten without asking for confirmation. If we want to append `STDOUT` to our existing file, we can use the double greater-than sign (`>>`). ([View Highlight](https://read.readwise.io/read/01jm2tpzzqc423zzeq0n83ywrf)) --- We can also use the double lower-than characters (`<<`) to add our standard input through a stream. We can use the so-called `End-Of-File` (`EOF`) function of a Linux system file, which defines the input's end. ([View Highlight](https://read.readwise.io/read/01jm2trhjjq3kb7rgwdtmpcqms)) --- ### Pipes Another way to redirect `STDOUT` is to use pipes (`|`). These are useful when we want to use the `STDOUT` from one program to be processed by another. One of the most commonly used tools is `grep`, which we will use in the next example. Grep is used to filter `STDOUT` according to the pattern we define. ([View Highlight](https://read.readwise.io/read/01jm2tx1t7rn3zxc9s1z2pakvn)) --- Now that we have a fundamental understanding of file descriptors, redirections, and pipes, we can structure our commands more efficiently to extract the exact information we need. This knowledge allows us to manipulate how input and output flows between files, processes, and the system, enabling us to handle data more effectively. By leveraging these tools, we can streamline tasks, avoid unnecessary steps, and work with files and system resources in a much more organized and efficient manner, ultimately enhancing our productivity and precision in managing operations. ([View Highlight](https://read.readwise.io/read/01jm2tytsk6fzdrj0gtqtzdfwn)) ---