Now that we know how to get around in the new neighborhood that is Linux, we can start knocking on some doors. In the previous step we saw that we can see the files in a directory. Reminder:
helper@geronimo:~$ ls
myfirst.txt
helper@geronimo:~$
The echo command
This command can output text to the screen, like this:
echo "Hello world!"
In itself it may seem not very useful, but it can for example also show us the contents of environment variables. Every Linux system I have seen knows environment variables called USER and PATH. Have a look:
gerard@geronimo370:~$ echo "Hello $USER"
Hello gerard
gerard@geronimo370:~$
We now see that the contents of the variable USER is ‘gerard’; surprise! that’s me. The other one is the PATH variable; it establishes the directories that are being searched when we type in a command:
gerard@geronimo370:~$ echo "my serach path is $PATH"
my serach path is /home/gerard/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
gerard@geronimo370:~$
The cat command
So, remember?, there is a file called myfirst.txt. Shall we see what is in that file? For now, we will use the ‘cat‘ command for this:
helper@geronimo:~$ cat myfirst.txt
This is my first text file
Second line of text in my file
helper@geronimo:~$
We can display the contents of files now. But: when a file is very long, the cat command will display all line at a staggering pace, and only stop when it reaches the end of the file. This causes us to only be able to see the last screen full of lines. There’s commands that do things a bit friendlier:
more or less command
The more command shows us the file again, but it stops when it reaches the first screen full. Pressing the space bar advances one screen at a time.
The less command does more or less the same (yes, pun intended). It also shows us the file, and stops when it reaches the first screen full. Pressing the cursor up and down keys allow us to advance one line forward or backward. The space bar still advances us one screen at a time, as does the page down key. We can go back using the page up key.
Both the more and the less command can be gracefully ended by using the q key.
Examples:
more /var/log/auth.log
less /var/log/auth.log
cat /var/log/auth.log | more
cat /var/log/auth.log | less
For the meaning of the upright slash ‘|’ character see the next section about redirection.
Redirection, stdin and stdout
Most Linux programs can read from standard input (stdin) and write to standard output (stdout). They default to keyboard and screen respectively.
But: both stdin and stdout can be diverted (redirected, in Linux terms). This makes for a very useful construct in which the utilities can be used to create new functionality by ‘chaining’ them together. Lets have a look at how this works. We saw that there are only two lines of text in this file. I would like to add a third line. I could of course use an editor to do that, but cat can do this for us as well, with a little help from a Linux trick that is called redirection:
helper@geronimo:~$ cat - >> myfirst.txt
third line of text going into my text file
helper@geronimo:~$
I used a minus sign as the first parameter for the cat command, which indicates that it has to read from it’s standard input (stdin, the keyboard in this case). This would instruct cat to read from the keyboard and type everything I entered to it’s standard output (stdout, normally the screen). Then I used a double greater-than sign (>>). This indicates that what comes out of a program (stdout of cat in this case) will not be written to the screen, but redirected and added to the end of the file we mention (myfirst.txt). Let’s check, did this work?
helper@geronimo:~$ cat myfirst.txt
This is my first text file
Second line of text in my file
third line of text going into my text file
helper@geronimo:~$
Yes, of course it worked. The line we typed is read be cat and written to its stdout; this stdout is now redirected by the ‘>>’ to be added to our file. And as we can see, there’s now three lines of text in our file.
Sidenote: we can use a single greater than sign (>). In that case the output file is overwritten, not extended. Both ‘>’ and ‘>>’ are redirections from a stdout to a file.
Another form is redirection to another utility, so from stdout of one program to stdin of the next program. Lets look at that, using a new command.
grep command
Now we are going to have a look at another utility called grep. This is a utility that can find text in the content of a stream of data. An example:
helper@geronimo:~$ cat myfirst.txt
This is my first text file
Second line of text in my file
third line of text going into my text file
helper@geronimo:~$ cat myfirst.txt | grep Second
Second line of text in my file
helper@geronimo:~$
Here we used the cat command that we saw before, it shows the same three lines that we saw before. The second command does the same, but now the output (stdout) is redirected with a ‘pipe‘ sign, the upright slash, ‘|’, to go to stdin of the next command, in this case grep. The grep command now goes through the lines of the incoming stream and looks for lines in which, in this case, the word ‘Second‘ is present. And that it works is proven by the line that we see.