Redirect (short)

From Juneday education
Jump to: navigation, search


Description

bash - redirect input/output.

On this page we only give you a short introduction with a few examples. We encourage you to read the manual (man bash).

"In computing, redirection is a form of interprocess communication, and is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations" Redirection_(computing)

>

The > takes the output from a command and redirects it to a file. If the file already exists, its content is overwritten. If the file does not exist, it is created.

Example use: Redirect output of command to a file

Let's say you want to store the current date in a file. To print the date you typically use the command date. Simply printing the date is easily done:

$ date
Fri Sep 22 08:00:33 CEST 2017

And to redirect this command's output to a file, date.txt is done using > date.txt you simply type:

$ ls date.txt
ls: cannot access 'date.txt': No such file or directory
$ date > date.txt
$ cat date.txt 
Fri Sep 22 08:09:32 CEST 2017

Note: the first command is there to show you that the file does not exist, then the redirect and finally we output the content of the file.

>>

Functions much like the previous > but instead of overwriting a file, if it already exists, it appends the output.

Example use: Append output

Let's say you want to store the current date in a file but keep the previous content in the file. To print the date you typically use the command date. Simply printing the date is easily done:

$ date
Fri Sep 22 08:00:33 CEST 2017

And to redirect and append this command's output to a file, date.txt you use >> date.txt like this:

$ ls date.txt 
date.txt
$ wc -l date.txt 
1 date.txt
$ date >> date.txt
$ wc -l date.txt 
2 date.txt

Note: the first command is there to show you that the file already exists, the second command outputs the number of lines in the file, then the redirect/append and finally we check the number of lines in the file which is now 1 line more.

<

The normal setup for a program is to read input from stdin and stdin being "connected" to the keyboard. You can switch stdin to be a file instead, think of it like your program, unknowingly, reading user input from a user typing the content of a file.

Example use: Reading input from file

Assume we want to calculate all the expressions in a file, calculations.txt, with the following content:

$ cat calculations.txt 
1+2
234+456

We could ask a person to start bc and type in the lines one by one, but imagine the time it takes if the file is 10000 lines long. If you still think 10000 lines long file is ok to manually type the content of we ask you to think of a file with 1 million lines in it. If we have such a big file it is easier to fool the program into thinking that a user types line by line. This tricking is done using < like this:

$ bc < calculations.txt 
3
690

Note: the program is not fooled at all. The program can actually check what it reads input from. But for most programs simply reading from stdin this trick works fine.

Example use: Read file and redirect output

We can combine the > and <. Imagine you want to calculate the experessions stored in the file calculations.txt and redirect the output to a file results.txt then you simply type:

$ bc < calculations.txt > results.txt
$ cat results.txt 
3
690

|

The normal setup for a program is to read input from stdin and stdin being "connected" to the keyboard. You can switch stdin to be a stream instead, think of it like your program use input from a user typing the content coming from a file.

Example use: Read stream and redirect output

Let's assume we want to count the java source code files recursively (in all folders and their subfolders etc below). To find the files we use the find command. This command outputs each file on a single line so we can use the command wc to count the lines. We could potentially do the counting like this:

$ find . -name "*.java" > files.txt
$ wc -l files.txt 
14 files.txt

But using a temporary file like this is cumbersome and pointless, however it is easy ti understand given what we've just learned. But let's use a pipe instead:

$ find . -name "*.java" | wc -l
14


Example use: Read stream and redirect output

This means that we can now, for an unknown reason, do the calculations in the < example above like this instead:

$ cat calculations.txt | bc > results.txt
$ cat results.txt 
3
690


Further reading

Read more about redirection: Bash:Bash-Redirection

Read more about standard streams: Bash:Bash-Standard_streams