You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ebook/en/content/023-bash-redirection.md
+48-1
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Redirection in Bash
2
2
3
-
A Linux superuser must have a good knowledge of redirection in Bash. It is an essential component of the system and is often helpful in the field of Linux System Administration.
3
+
A Linux superuser must have a good knowledge of pipes and redirection in Bash. It is an essential component of the system and is often helpful in the field of Linux System Administration.
4
4
5
5
When you run a command like ``ls``, ``cat``, etc, you get some output on the terminal. If you write a wrong command, pass a wrong flag or an wrong command-line argument, you get error output on the terminal.
6
6
In both the cases, you are given some text. It may seem like "just text" to you, but the system treats this text differently. This identifier is known as a File Descriptor (fd).
@@ -11,6 +11,14 @@ In Linux, there are 3 File Descriptors, **STDIN** (0); **STDOUT** (1) and **STDE
11
11
***STDOUT** (fd: 1): Manages the output text in the terminal.
12
12
***STDERR** (fd: 2): Manages the error text in the terminal.
13
13
14
+
# Difference between Pipes and Redirections
15
+
16
+
Both *pipes* and *redidertions* redirect streams `(file descriptor)` of process being executed. The main diffrence is that *redirections* deal with `files stream`, sending the output stream to a file or sending the content of a given file to the input stream of the process.
17
+
18
+
On the otherhand a pipe connects two commands by sending the output stream of the first one to the input stream of the second one. without any redidertions specified.
19
+
20
+
# Redirection in Bash
21
+
14
22
## STDIN (Standard Input)
15
23
When you enter some input text for a command that asks for it, you are actually entering the text to the **STDIN** file descriptor. Run the ``cat`` command without any command-line arguments.
16
24
It may seem that the process has paused but in fact it's ``cat`` asking for **STDIN**. ``cat`` is a simple program and will print the text passed to **STDIN**. However, you can extend the use case by redirecting the input to the commands that take **STDIN**.
@@ -133,3 +141,42 @@ There is also a shorter way to do this.
133
141
```
134
142
./install_package.sh > file.txt 2>&1
135
143
```
144
+
145
+
# Piping
146
+
147
+
So far we have seen how to redirect the **STDOUT**, **STDIN** and **STDOUT** to and from a file.
148
+
To concatenate the output of program *(command)* as the input of another program *(command)* you can use a vertical bar `|`.
149
+
150
+
Example:
151
+
```
152
+
ls | grep ".txt"
153
+
```
154
+
This command will list the files in the current directory and pass output to *`grep`* command which then filter the output to only show the files that contain the string ".txt".
155
+
156
+
Syntax:
157
+
```
158
+
[time [-p]] [!] command1 [ | or |& command2 ] …
159
+
```
160
+
161
+
You can also build arbitrary chains of commands by piping them together to achieve a powerful result.
162
+
163
+
This examble create a listing of every user which owns a file in a given directory as well as how many files and directories they own:
0 commit comments