Redirection Operators in Linux

You will learn the very useful concept of I/O redirection in this article.

File descriptors

All I/O–including files, pipes, and sockets – are handled by the kernel via a mechanism called the file descriptor. A file descriptor is a small, unsigned integer, which is an index into a file-descriptor table maintained by the kernel, and used by the kernel to reference open files and I/O streams. Each process inherits its own file-descriptor table from its parent. The first three file descriptors are 01, and 2. File descriptor 0 is standard input (stdin), 1 is standard output (stdout), and 2 is standard error (stderr). When you open a file, the next available descriptor is 3, and it will be assigned to the new file.

Redirection

When a file descriptor is assigned to something other than a terminal, this is called I/O redirection. The shell performs the redirection of output to a file by closing the standard output file descriptor 1 (the Terminal) and then assigning that descriptor to the file. When redirecting standard input, the shell closes file descriptor 0 (the Terminal) and assigns that descriptor to a file. The Bash shells handle errors by assigning a file to file descriptor 2.The following command will take input from the sample.txt file:

$ wc  <  sample.txt

The preceding command will take content from the sample.text file. The wc command will print the number of lines, words, and characters in the sample.txt file. This command will redirect output to be saved in the log.txt file:

$ echo "Hello world" > log.txt

This command will append the Hello World text to the log.txt file:

$ echo "Welcome to Shell Scripting" >> log.txt

The single > will overwrite or replace the existing text in the log file, and double >> will append the text to the log file. Let’s see a few more examples:

$ tr '[A-Z]' '[a-z]' < sample.txt

The preceding tr command will read text from the sample.txt file. The tr command will convert all uppercase letters to lowercase letters and will print the converted text on screen:

$ ls > log.txt$ cat log.txt

The output of the command is as follows:

Output:

dir_1sample.txtextra.file

In this example command, ls sends directory content to file log.txt. Whenever we want to store the result of the command in the file, we can use the preceding example.

$ date >> log.txt$ cat log.txt

The output is as follows:

Output:

dir_1dir_2file_1file_2file_3Sun Sept 17 12:57:22 PDT 2004

In the preceding example, we redirect and append the result of the date command to the log.txt file.

$ gcc hello.c 2> error_file

The gcc is a C language compiler program. If an error is encountered during compilation, then it will be redirected to error_file. The > character is used for a success result and 2> is used for error-result redirection. We can use error_file for debugging purposes:

$ find . -name "*.sh" > success_file 2> /dev/null

In the preceding example, we redirect output or success results to success_file, and errors to /dev/null/dev/null is used to destroy the data, which we do not want to be shown on screen.

$ find . -name "*.sh" &> log.txt

The preceding command will redirect both output and errors to log.txt.

$ find . -name "*.sh"  > log.tx 2>&1

The preceding command will redirect the result to log.txt and send errors to where the output goes, such as log.txt.

$ echo "File needs an argument" 1>&2

The preceding command will send standard output to the standard error. This will merge the output with the standard errors. A summary of all I/O redirection commands is as follows:

< sample.txt

The command will take input from sample.txt

> sample.txt

The success result will be stored in sample.txt

>> sample.txt

The successive outputs will be appended to sample.txt

2> sample.txt

The error results will be stored in sample.txt

2>> sample.txt

The successive error outputs will be appended to sample.txt

&> sample.txt

This will store successes and errors, such as in sample.txt

>& sample.txt

This will store successes and errors, such as in sample.txt (the same as the previous example)

2>&1

This will redirect an error to where output goes

1>&2

This will redirect output to where errors go

>|

This overrides noclobber when redirecting the output

<> filename

This uses the file as both standard input and output if a device file (from /dev)

cat xyz > success_file 2> error_file

This stores success and failure in different files

The following is the summary of various meta-characters:

Char

Meaning

Example

Possible output

*

Matches with zero or multiple numbers of any character

$ ls -l *.c file*

Sample.c, hello.c, file1, file_2, filebc

?

Matches any single character

$ ls -l file?

filea, fileb, file1

[..]

Matches with any single character within the bracket

$ ls -l file[abc]

filea, fileb, filec

;

Command separator

$cat filea; date

Displays the content of filea, and displays the current date and time

|

Pipe between two commands

$ cat filea | wc -l

Prints the number of lines in filea

()

Groups commands; is used when the output of the command group has to be redirected

$ (echo "***x.c***";cat x.c)>out

Redirects the content of x.c with a heading ***x.c*** to the file out

Run the following command:

$ touch filea fileb filec fileab filebc filead filebd filead$ touch file{1,2,3}

Try out the following command:

$ ls  s*$ ls  file$ ls  file[abc]$ ls  file[abc][cd]$ ls  file[^bc]$ touch file file1 file2 file3 ... file20$ ls ?????file1file2file3$ ls file*file file1 file10 file2 file3$ ls file[0-9]file1 file2 file3$ ls file[0-9]*file1 file10 file2 file3$ ls file[!1-2]file3

Brace expansion

Curly braces allow you to specify a set of characters from which the shell automatically forms all possible combinations. To make this work, the characters to be combined with the given string must be specified as a comma-separated list with no spaces:

$ touch file{1,2,3}
$ ls

Output:

satish@app:/home/satish$ touch file{1,2,3}
satish@app:/home/satish$ ls
file1  file2  file3
satish@app:/home/satish$
$ mkdir directory{1,2,3}{a,b,c}
$ ls

Output:

satish@app:/home/satish$ mkdir directory{1,2,3}{a,b,c}
satish@app:/home/satish$ ls
directory1a  directory1b  directory1c  directory2a  directory2b  directory2c  directory3a  directory3b  directory3c
satish@app:/home/satish$
$ touch file{a..z}
$ ls

Output:

satish@app:/home/satish$ touch file{a..z}
satish@app:/home/satish$ ls
filea  filec  filee  fileg  filei  filek  filem  fileo  fileq  files  fileu  filew  filey
fileb  filed  filef  fileh  filej  filel  filen  filep  filer  filet  filev  filex  filez
satish@app:/home/satish$

The following is the summary of various I/O-redirection and logical operators:

Char Meaning Example Possible Output
> Output Redirection $ ls > ls.out Output of ls command is redirected (overwritten) to ls.out file
>> Output Redirection (appended) $ ls >> ls.out Output of ls command is redirected (appended) to ls.out file
< Input Redirection $ tr ‘a’ ‘A’ < file1 The tr command read input from file1 instead of keyboard(stdin)
`cmd` or $(cmd) Command Substitution $ echo `date`
or
$ echo $(date)
The command date is substituted with the result and sent to echo for display
|| OR Conditional Execution $ test $x -gt 10 || $x -lt 15 Check whether x value is greater than 10 or less than 15
&& AND Conditional Execution $ test $x -gt 10 && $x -lt 15 Check whether x value is greater than 10 and less than 15

For example:

$ ls || echo "Command un-successful"$ ls a abcd || echo "Command un-successful"

These commands will print Command un-successful if the ls command is unsuccessful.

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

14 − eleven =