We'll continue our discussion and see how to redirect a command output to a file. A short recap: every 1
every command you run has three data streams connected to it: STDIN (0) or Standard input, STDOUT (1) or or Standard 2
Output and STDERR (2) or Standard error. 3
Normally, when we run a command, we want to see the output on the screen, in the terminal. 4
This is convenient most of the time, but sometimes we may wish to save it to a file, to keep it for 5
the record, feed it into another program or send it to someone else. To redirect the output of a command 6
to a file 7
there's the output reduction operator, which is the greater than a sign; it indicates to the command 8
that we wish the output to be saved in a file instead of being printed on the screen. Let's 9
see an example and we'll start with a simple one. 10
I'm running the ls command, which as expected, has printed out the output on the screen. 11
Now I'm going to use the output redirection to save it to a file. 12
ls-l 13
> ls.txt 14
Let's talk about what has happened: the greater then sign, which is the output reduction operator, 15
has redirected the output to the file instead of printing it on the screen. 16
See how a new file called ls.txt was created in the current working directory and you notice that 17
we didn't have to create the file before saving it. 18
It has been created automatically. 19
If I display its contents, I'll find there the output of the ls command. 20
Now, let's see what will happen if you redirect it again to the same file. I'm redicting the output 21
of another command to the same file. 22
I 'll redirect the output of ifconfig, so ifconfig > and the name of the file 23
and I am listing the file contents cat ls.txt and and notice something very important. 24
The file was overwritten. 25
The output of the first command is not there anymore. 26
So you can draw a conclusion. 27
When using the output redirection will have two case scenarios. 28
The file is created if it doesn't exist or it is overwritten if it already exists. 29
If we don't want to override the file, we can instead append or add to the end of the file by using 30
the double greater than operator ( >> ). Let’s run the same commands again: ls -l > 31
So output redirection output.txt 32
And ifconfig 33
>> 34
output.txt and cat output.txt 35
This is the file's contents. 36
We see the output of both commands. 37
The output of the second one was appended to the file. 38
This is a big difference and it's important. 39
Let's see another example. who 40
>> operator and the users.txt, of course, that file didn't exist. 41
So the double greater than operator creates the file 42
if it doesn't exist or appends to it if it already exists. Up until now in the course, I said many 43
times that everything is a file in Linux or is treated as a file. To emphasize this concept 44
I'll show you how even the terminal is treated as a file. To see the file behind a terminal run the 45
tty command. 46
tty 47
And we notice the file. 48
This is the file. 49
It's called 0 in pts directory, which resides in dev 50
I'm opening another terminal and I'll redirect the output of a commad to the file that represents 51
the other terminal. 52
For example, ifconfig> 53
dev/ pts and zero. 54
Very nice, we see how the output of the command was written to the file that represents the terminal 55
and appeared in the first terminal. 56
This is like a basic instant messaging like this: 57
"Hello, how are you?" 58
The string appeared in the other terminal. 59
By the way, the echo command simply displays a line of text. Let's go ahead and talk about error redirection. 60
The three streams, attached to any Linux command actually have numbers associated with them. 61
You see the numbers between parentheses. STDERR is stream number 2 62
and we use these numbers to identify the streams. 63
If we place a number before the greater then operator, then it will redirect that stream. 64
If we don't use a number, like we have been doing so far, then it defaults to stream one, which is 65
standard output. 66
Let's see some examples. 67
I'm running a command which will return an error. 68
tail -n 3 /etc/shadow 69
Permission denied! A non-privileged user cannot see that file. To redirect the error message to a file 70
I’ll use 2 which is the id of STDERR and >. 71
So 2 72
> and the file 73
error.txt It’s not allowed to put any space between 2 and >. This is not allowed. 74
And cat/error.txt. We noticed how the error was redirected to the file, which was also created because 75
it didn't exist. In fact, the same rules from the output reduction apply to the error redirection 76
as well. If you want to append to the file instead of overwriting use 2 and the >> 77
sign operator. 78
I'm executing the same command again, this time using 2 and >> 79
Like this: cat / error.txt 80
The error message was appended to the end of the file. 81
This is another error 2 >> error.txt It was appended to the file. 82
It's also possible to redirect both the normal output and the error messages of the same command. 83
The same command could produce both normal output and errors. 84
For example, tail -n 3 /etc/password and the second file, the second argument, etc/ shadow. 85
It has printed the last two lines of the first file. 86
These are the lines, but an error for the second file;it is not allowed to read the file. 87
And I'm directing the output to a file called output.txt. 88
And the errors 89
to error.txt 90
The files were created or overwritten. 91
This is the output and this is the error. 92
OK. 93
In the next example, I'll show you how to redirect both standard output and standard error 94
to the same file. 95
What I'll do is redirect standard output to a file and the standard error to standard output. 96
So I'm running the command again. 97
This is the command. I'm retracting the standard output to output.txt and standard error 98
to standard output & 1 99
1 being the idea of standard output. 100
cat output.txt Note that the file contains both the output and the errors. One remark here: we've 101
redirected to a stream by placing an & in front of a stream 102
No. 103
Otherwise it would have redirected to a file called 1. 104
Now, the standard error was redirected to a file called 1. 105
So this & here is very important. You can also combine command reduction with pipes. 106
Let's take a look at an example. 107
Let's filter the line that contains the Mac address using a pipe, so iffconfig | grep ether 108
This is the Mac address, and I want to see the lines that contain the string ether. 109
Great, we are seeing the output and I'll save the output to a file. 110
> mac.txt 111
The output of the command was saved into a file instead of printed in the terminal. 112
By the way, if you just want to get the Mac address without extra strings, you could try. 113
I'm using a second pipe | cut -d" " -f10 114
This is the Mac address, and I'm directing it to a file. 115
By the way, I have introduced a new command, which is cut. It costs out sections from each line using 116
a field delimiter, in this case a white space and the section or the field number to cut. 117
In this example, it was 10, because there are 10 white spaces till the field I wanted to cut, which 118
was the Mac address. Just to get used to the cut command 119
you can try another simple example. 120
Let's consider the password file. 121
And tI want to cut the first field, which represents the users using a colon as a delimiter. 122
So cut -d: -f1 the first field and the file 123
And we see only the users. 124
Or I'll cut the user id, which is the third field. 125
OK, let's go ahead. 126
What can we do if at the same time we want to store and view the output of any command? If I use 127
the right direction command the output will be redirected to a file without displaying it in the terminal. 128
For example, who > users.txt I'm not seeing the output. 129
Here comes into play the tee command. Its name comes from the t-splitter used in plumbing. 130
It basically breaks the output of a program so that it can be both displayed and saved in a file. 131
Let's see an example. 132
ifconfig | grep 133
ether 134
This is displaying the output only on the screen. 135
And this command is saving the output only to a file. 136
I want a combination of these two commands. 137
So instead of using the output reduction, I'll use another pipe and tee; this command with the help of the 138
tee command writes the output both to the screen and to the file. 139
I want you to see that a new file will be created. 140
This is the output on the screen and cat mac.txt the same output was saved into the file. 141
By default, tee command overwrites the file. You can instruct tee to append to the file using the option –a 142
as in the next example: 143
who | tee -a m.txt 144
And cat m.txt the output was appended to the end of the file. 145
And you can also write the output to multiple files. Let's see the kernel version uname -r 146
This is the running kernal version | tee -a mac.txt kernel.txt 147
Oh, sorry, I've forgotten the command 148
So uname -r 149
uname -r 150
And | tee -a and the files 151
The output was displayed on the screen and appended to both files. 152
Thanks! 153
that’s all about the redirection command and piping.