Linux+: Linux Shell 13 – Shell Command Redirection

J

Jarret W. Buse

Guest
Linux+: Linux Shell 13 – Shell Command Redirection

With any kind of shell commands, they are more powerful when redirecting to input and output files.

There are three default input and output devices (which in Linux are really just files). The three files can really be thought of as I/O streams which are coming in and out of a program. The three are:

  1. Standard Input (stdin) – defaults to the keyboard
  2. Standard Output (stdout) – defaults to the screen
  3. Standard Error (stderr) – defaults to the screen
NOTE: The defaults can be overridden.

The 'stdin' is where data comes into the program. The 'stdin' can be the keyboard, file, hardware device, etc. The 'stdout' is output from a program such as the screen, file, printer or other hardware device. The 'stderr' are error messages produced by a program which can be sent to the screen, file, printer or other hardware device.

To begin with redirection, there are four types of redirection. Redirection allows you to change the default for the stdin, stdout and stderr.

The four basic redirectors are:

  1. > - out
  2. >> - append out
  3. < - in
  4. | - pipe

Let's look at these in very simple examples. First, let's look at how to redirect the output. If we wish to perform a command and have the output go to a file instead of the screen, we would do the following:

command > file

NOTE: You can imagine the greater-than-symbol as funnel. The information is funneled from the command into the file.

For example, to list the files in a directory, which begin with the letter 't', would require the following command:

ls t*

When in a Terminal, the standard output is the screen. What if you wanted to output the information to a file? This is where redirection comes in and you can change the default stdout. If you wanted the output to go to a file in the current directory called 't-out', you would use the following command:

ls t* > t-out

Now, to input a file into a program. Basically, this works very well for command-line programs and the syntax is:

command < file

In this example, I will use the parted command and pass commands to it from a file. The file has the following lines:

  1. unit mb
  2. print all
  3. quit

The first line is a parted command that switches the default units to megabytes. The second command causes information to be displayed for all drives. The final command quits parted.

I can then input these commands from the file name 'parted-in' in the current directory into the parted command as shown:

parted < ./parted-in

The output for my system is as follows:

GNU Parted 2.3

Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) unit mb


(parted) print all

Model: ATA Hitachi HTS72502 (scsi)
Disk /dev/sda: 250059MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 1.05MB 500MB 499MB bios_grub
2 500MB 45500MB 45000MB ext4
3 45500MB 47492MB 1992MB linux-swap(v1)
4 47492MB 89435MB 41943MB ext4 msftdata
5 89435MB 115650MB 26214MB ext4 msftdata
7 115650MB 173321MB 57672MB ext2
8 173321MB 199536MB 26214MB ext4
9 199536MB 224701MB 25166MB ext4 msftdata
10 224701MB 250059MB 25358MB ext4 msftdata


(parted) quit

Now, to show you a neat little trick. What if you wanted the output to go to a file and not the screen? The nice thing about redirection is that you can combine them as follows:

parted < ./parted-in >~/parted-out

Here, the command is the same, but now the output is sent to the file 'parted-out' in the Home folder.

Be aware that if you run the same command again with the same output file, the file is overwritten. To append to the existing data in the file you would use the append out (>>) redirection as shown:

parted < ./parted-in >>~/parted-out

The last redirection is the pipe (|) located above the backslash (\) key on the keyboard. The syntax is:

command1 | command2

The pipe redirection allows for the output of command1 to be sent to command2.

If you performed the previous examples and your current directory contained the files 'parted-out' and 'parted-in', you could use the following command to list the files:

ls | grep parted

In the example, a file list is generated and sent to the grep command which searches through the list for anything containing the word 'parted'.

Similar to combining input, output and append redirections in one statement, the same can be done with the pipe command as shown:

parted < ./parted-out | grep Model >> ./parted-save

NOTE: In this example, the output or append redirection must go last. Nothing comes out of the output or append except the information to the specified location. You cannot output or pipe from output or append.
 

Attachments

  • slide.jpg
    slide.jpg
    49.4 KB · Views: 145,049



Members online


Latest posts

Top