Linux OS Basics – Part 2

Text Fundamentals

stdout (Standard Out)

We are now familiar with enough commands and their output. Now let us jump right into the pool of text fundamentals. The next subject we are about to look is (input/output) streams.

pineturtle@DESKTOP-ESO0AIU:/tmp$ echo hello wolrd > text.txt

Okay, this command is definitely new to us. Probably you might wonder what the “tech” just happened.

Well, check the directory where you ran that command and you should see a file called text.txt, look inside that file and you should see the text Hello World

 Lots of things just happened in one command so let’s break it down.

pineturtle@DESKTOP-ESO0AIU:/tmp$ echo hello wolrd

Processes use I/O streams to receive input and return output. By default, the echo command takes the input (standard input or stdin) from the keyboard and returns the output (standard output or stdout) to the screen. So that’s why when you type echo Hello World in your shell, you get Hello World on the screen. However, I/O redirection allows us to change this default behaviour giving us greater file flexibility.

The > is a redirection operator that allows us the change where the standard output goes. And that’s basically how stdout redirection works!

Let’s say I didn’t want to overwrite my peanuts.txt, luckily there is a redirection operator for that as well, >>:

pineturtle@DESKTOP-ESO0AIU:/tmp$ echo different hello world >> text.txt

This will append Hello World to the end of the peanuts.txt file, if the file doesn’t already exist it will create it for us like it did with the > redirector!

stdin (Standard in)

We learned that we have different stdout streams we can use, such as a file or the screen. Well, there are also different standard input (stdin) streams we can use as well. 

Let’s use the text.txt file in the previous lesson for this example, remember it had the text Hello World in it.

pineturtle@DESKTOP-ESO0AIU:/tmp$ cat < text.txt > newtext.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ cat newtext.txt
hello wolrd
different hello world
pineturtle@DESKTOP-ESO0AIU:/tmp$

Just like we had > for stdout redirection, we can use < for stdin redirection.

Normally in the cat command, you send a file to it and that file becomes the stdin, in this case, we redirected peanuts.txt to be our stdin. Then the output of cat peanuts.txt which would be Hello World gets redirected to another file called newtext.txt.

stderr (Standard Error)

Now lets us try something different. Try to list the contents of a directory that doesn’t exist on your system and redirect the output to the text.txt file that we used in the previous section.

You should see the below.

pineturtle@DESKTOP-ESO0AIU:/tmp$ ls /tmp/fakedirectory > text.txt
ls: cannot access '/tmp/fakedirectory': No such file or directory

Now, these errors are standard errors and if you want to redirect those errors to a file we are going to use file descriptors. A file descriptor is a non-negative number that is used to access a file or stream.
For now, just know that the file descriptors for stdin, stdout and stderr are 0, 1 and 2.

So now if we want to redirect our stderr to the file we can do this:

pineturtle@DESKTOP-ESO0AIU:/tmp$ ls /tmp/fakedirectory > text.txt 2>&1

What we did is, the “2” denotes stderr “>” redirects output “1” is input. Now you should have an idea of what happened when you run the command. The errors will be streamed to the text.txt. If you read the text.txt file you should see the error is be recorded.

Now, what if I don’t want any of that cruft and want to get rid of stderr messages completely? Well, you can also redirect output to a special file call /dev/null and it will discard any input.

pineturtle@DESKTOP-ESO0AIU:/tmp$ ls /tmp/fakedirectory 2> /dev/null

tail

The tail command lets you see the last 10 lines of a file by default.

pineturtle@DESKTOP-ESO0AIU:/tmp$ tail /var/log/syslog

You can change the number of lines you want to see

pineturtle@DESKTOP-ESO0AIU:/tmp$ tail -n 10 /var/log/syslog

Another great option you can use is the -f (follow) flag, this will follow the file as it grows. Give it a try and see what happens.

pineturtle@DESKTOP-ESO0AIU:/tmp$ tail -f /var/log/syslog

Your Syslog file will be continually changing while you interact with your system and using tail -f you can see everything that is getting added to that file.

grep

The grep command is quite possibly the most common text processing command you will use. It allows you to search files for characters that match a certain pattern. What if you wanted to know if a file existed in a certain directory or if you wanted to see if a string was found in a file? You certainly wouldn’t dig through every line of text, you would use grep!

pineturtle@DESKTOP-ESO0AIU:/tmp$ echo hello world > text.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ grep hello text.txt

You should see that grep found hello in the text file. Go ahead and use “grep –help” and look through the options and try out some of the options with the help of the lftsample.txt we used in part 1.

See you at the next part 🙂

Leave a comment