# Output Redirection and Piping

### Output Redirection <a href="#firstheading" id="firstheading"></a>

* Most processes initiated by UNIX commands take their input from the standard input (i.e. the keyboard) and write their output to the standard output (i.e. the terminal screen).
* The "<" sign can be used to redirect the input, that is to specify that the input comes from something other than the keyboard.
* The ">" sign can be used to redirect the output, that is to specify that the output goes to something other than the terminal screen.
* The ">>" sign can be used to append the output to something other than the terminal screen.

```
# list the current files and redirect the output to a file named "mylist.txt"
ls > mylist.txt
# view content of mylist.txt
cat mylist.txt
```

```
# redirect the input to a command 
cat < mylist.txt
```

```
# redirect the output and append 
cat mylist.txt > list1.txt
cat mylist.txt >> list2.txt
# view content 
cat list2.txt
```

### Piping

* A pipe is denoted by "|".
* Several pipes can be used in the same command line to create a "pipeline".
* A pipe takes the output of a command and immediately sends it as input to another command.
* A pipe is often used in conjunction with the command "less" to view the output within the pager.

```
#view users connected
who
#count the number of users connected
who | wc -l
```

```
#display the content of bin
ls -la /usr/local/bin
#display the content of bin within the pager provided by "less"
ls -la /usr/local/bin | less
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://igb.mit.edu/mini-courses/introduction-to-unix-and-ki-computational-resources/basic-unix/output-redirection-and-piping.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
