Redirecting Input and Output with some Simple Commands

From docwiki
Jump to: navigation, search


Motivation

The philosophy behind most of the Linux/Unix tools is that they are simple but can be combined with other tools for more complex tasks. Here you should learn some simple commands and how to redirect their input or output to a file. Later we will learn some more elegant ways to combine commands.

Simple Commands

After the $ is the command you want to type. Afer the # is some comment to explain the command. ( Everything after the # is a comment and ignored so you can even cut-&-paste it)

$ date              # shows the current date
$ echo hello world  # echo the text
$ ls                # list the files in the current directory
$ ls -l             # list the files in long form (more info, one per line)
$ who               # who is logged in
$ sleep 10          # sleep (do nothing) for 10 seconds
$ history           # shows all the commands you typed so far
$ hostname          # prints the name of your machine
$ find .            # lists all files and directories from the current directory (.)
$ :                 # empty command, does nothing and has no output

Redirecting Output to a File

If you use the > then you can redirect the output of a command to a file. If the file already exists it will be overwritten. By convention you want to name your files with the extension .txt but there is no need to do this. The file can have any name. If we use >> instead we can append to an existing file. If the file did not exist it will still be created.

$ date > bla.txt   # would create a file named bla.txt with the current date and time
$ echo hello world >> bla.txt # would append the text "hello world" to the above file
$ :> bli.xt             # would create an empty file. As the : command has no output.
$ hostname  > blo.txt   # creates or overwrites blo.txt with the current hostname
$ cat bla.txt           # print out the content of bla.txt file
$ cat bla.txt bla.txt blo.txt # prints bla.txt 2 times and then blo.txt
$ cat bla.txt bla.txt >bla2.txt # create bla2.txt with twice the content of bla.txt


Some commands expect input. If you run them directly you could type something in and when you are done you can close with Ctrl-d. E.g.:

$ cat - > bla.txt  # read interactively and write it to bla.txt end with ctrl-d
type something
press ctrl-d

$ cat bla.txt   # now see what you have typed

The - sign is often used by programs to tell it to read form input (often called "STDIN" - spoken as "standard in") in places where you would normally put a file name.

In the same way the output of a program is "STDOUT" (speak "standard out").

Redirecting Input to a Command

Those programs that read from STDIN can be told to get their input from a file. The < sign is used for that:

$ cat - < bla.txt   # would print out bla.txt the - reads from STDIN the < sends the bla.txt
$ wc < bla.txt      # wc stands for "word count" but counts words, charactes and lines.
$ ls -l > bla.txt   # creates a file bla.txt with the output of ls -1 (list of files)
$ wc -l < bla.txt   # counts the lines (l) in bla.txt   

Note: Many of those programs also allow to have file names as arguments as well. So

wc -l bla.txt 

would do the same without redirection of input.

Redirecting Error Messages

While the redirecting of output is extremely useful, there would be the danger that if a command fails the error messages would not be shown. So in order to distinguish between normal output and error messages the error messages are usually sent to a different channel. The channels is named "STDERR" (speak: "standard error").

$ fsdsdfadfs > bla.txt
bash: fsdsdfadfs: command not found

The above creates an empty file bla.txt but the error message that tells us that the command fsdsdfadfs does not exist is still shown.

$ fsdsdfadfs > bla.txt 2> bli.txt 

The above creates an empty bla.txt again. But the error message we saw before is now stored in bli.txt.

In some cases we do not want the separation of STDOUT and STDERR and we would prefer to have it all in one file:

$ fsdsdfadfs > bla.txt 2>&1  

In the example above we tell the shell to connect our STDERR to STDOUT (with the 2>&1). So the bla.txt will contain the error message. If it was a regular command with some regular output and some error output, both would end up in bla.txt

Exercises

  • Play around with the commands above and try to understand what happens
  • What is the output of the following sequence of commands? Why?
$ echo test > bla.txt
$ cat bla.txt bla.txt > bli.txt
$ cat - < bli.txt  > bla.txt
$ cat bla.txt bla.txt > bli.txt
$ wc -l < bli.txt