Everything You Need to Know about Linux Input-Output Redirection (2024)

Are you looking for information related to the Linux input-output redirection? Then, read on. So, what’s redirection? Redirection is a Linux feature. With the help of it, you are able to change standard I/O devices. In Linux, when you enter a command as an input, you receive an output. It’s the basic workflow of Linux.

The standard input or stdin device to give commands is the keyboard and the standard output or stdout device is your terminal screen. With redirection, you can change the standard input/output. From this article, let’s find out how Linux input-output redirection works.

Standard Streams in Input-Output Redirection

The bash shell of Linux has three standard streams of input-output redirection, 1) Standard Input or Stdin, 2) Standard Output or Stdout, and 3) Standard Error or Stderr.

The standard input stream is denoted as stdin (0). The bash shell receives input from stdin. The keyboard is used to give input. The standard output stream is denoted as stdout (1). The bash shell sends the output to stdout. The final output goes to the display screen. Here 0, 1, and 2 are called file descriptors (FD). In the following section, we’ll look into file descriptors in detail.

File Descriptors

In Linux, everything is a file. Directories, regular files, and even the devices are considered to be files. Each file has an associated number. This number is called File Descriptor or FD.

Interestingly, your terminal screen also has a definite File Descriptor. Whenever a particular program is executed, its output gets sent to your screen’s File Descriptor. Then, you can see the program output on the display screen. If the program output gets sent to your printer’s FD, the output would be printed.

0, 1, and 2 are used as file descriptors for stdin, stdout, and stderr files respectively.

Input Redirection

The ‘<’ sign is used for the input or stdin redirection. For example, Linux’s mail program sends emails from your Linux terminal.

You can type the email contents with the standard input device, keyboard. However, if you’re willing to attach a file to the email, use Linux’s input redirection feature. Below is a format to use the stdin redirection operator.

Mail -s "Subject" to-address < Filename

This would attach a file with your email, and then the email would be sent to a recipient.

Output Redirection

The ‘>’ sign signifies the output redirection. Below is an example to help you understand its functions.

ls -al > listings

In the above example, the output command ls-al will be redirected to the file “listings” and it won’t go to your screen.

Note: In case, there is already a file present with the same name, the above command deletes that file contents, and the file gets overwritten. If you want a file not to be overwritten and you want to add content to the existing file, use the ‘>>’ operator. Use the “>&” operator to redirect the program output from one file to another.

You can not only redirect standard output to files, but also to the devices.

$ cat music.mp3 > /dev/audio

In the above instance, the cat command is used to read the file music.mp3. It sends the output to the audio device /dev/audio. If you have the correct sound configurations on PC, the above command plays the music file music.mp3.

Error Redirection

Error redirection is a very popular feature in Linux. Several times, you get massive amounts of errors while running your code. Error redirection means sending those errors to a particular file instead of the screen. By default, the error stream gets displayed on your screen.

When you search files, you often get permission denied errors. These errors don’t help in your search. When shell scripts are executed, it’s not desired to see error messages cluttering the normal output. As the solution, redirection of these errors is used.

$ myprogram 2>errorsfile

In the above example, the program named “myprogram” will be executed. “2>” redirects the error output to the particular file named “errorfile”. Thus the program output doesn’t get cluttered with errors. You can also perform error redirection using its File Descriptor 2.

Conclusion

Through this article, we learned about the streams of Linux input-output redirection. We discussed what is input redirection, what is output redirection, and what is error redirection. I hope you find this article useful.

Everything You Need to Know about Linux Input-Output Redirection (2024)
Top Articles
The cybersecurity implications of quantum computing
Four Reasons Successful Businesses Borrow Money
5 Bijwerkingen van zwemmen in een zwembad met te veel chloor - Bereik uw gezondheidsdoelen met praktische hulpmiddelen voor eten en fitness, deskundige bronnen en een betrokken gemeenschap.
Craigslist Dog Sitter
ds. J.C. van Trigt - Lukas 23:42-43 - Preekaantekeningen
Cvs Devoted Catalog
True Statement About A Crown Dependency Crossword
Florida (FL) Powerball - Winning Numbers & Results
Used Wood Cook Stoves For Sale Craigslist
Nonuclub
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Nebraska Furniture Tables
Classic Lotto Payout Calculator
Stihl Km 131 R Parts Diagram
Viha Email Login
Grayling Purnell Net Worth
Epguides Strange New Worlds
Skip The Games Fairbanks Alaska
Craigslist Pearl Ms
Joan M. Wallace - Baker Swan Funeral Home
Yosemite Sam Hood Ornament
Play It Again Sports Norman Photos
Avatar: The Way Of Water Showtimes Near Maya Pittsburg Cinemas
Craigslist Hunting Land For Lease In Ga
800-695-2780
UCLA Study Abroad | International Education Office
Ticket To Paradise Showtimes Near Cinemark Mall Del Norte
Wonder Film Wiki
Is Henry Dicarlo Leaving Ktla
How do you get noble pursuit?
Askhistorians Book List
Ringcentral Background
Desales Field Hockey Schedule
Moonrise Time Tonight Near Me
Smayperu
new haven free stuff - craigslist
Craigslist Lakeside Az
Skip The Games Grand Rapids Mi
Who Is Responsible for Writing Obituaries After Death? | Pottstown Funeral Home & Crematory
Foxxequeen
Pulaski County Ky Mugshots Busted Newspaper
Pink Runtz Strain, The Ultimate Guide
How Big Is 776 000 Acres On A Map
Bekkenpijn: oorzaken en symptomen van pijn in het bekken
Noga Funeral Home Obituaries
El Patron Menu Bardstown Ky
Goosetown Communications Guilford Ct
Houston Primary Care Byron Ga
Kenmore Coldspot Model 106 Light Bulb Replacement
Noelleleyva Leaks
Vrca File Converter
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 6192

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.