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
tgt - Wiktionary, the free dictionary
The 45 Best Healthy Foods to Always Have in Your Kitchen – Free Printable List! – 80 Twenty Nutrition
No Hard Feelings (2023) Tickets & Showtimes
Public Opinion Obituaries Chambersburg Pa
Forozdz
Pieology Nutrition Calculator Mobile
Frank Lloyd Wright, born 150 years ago, still fascinates
Voordelige mode in topkwaliteit shoppen
Brendon Tyler Wharton Height
Davante Adams Wikipedia
Caroline Cps.powerschool.com
Buckaroo Blog
Which aspects are important in sales |#1 Prospection
Select Truck Greensboro
Ap Chem Unit 8 Progress Check Mcq
No Strings Attached 123Movies
سریال رویای شیرین جوانی قسمت 338
Kvta Ventura News
Divina Rapsing
Craigslist Sparta Nj
Race Karts For Sale Near Me
Where Is George The Pet Collector
Ge-Tracker Bond
Craigslist Prescott Az Free Stuff
Busted Campbell County
SuperPay.Me Review 2023 | Legitimate and user-friendly
Gazette Obituary Colorado Springs
Shadbase Get Out Of Jail
Jobs Hiring Near Me Part Time For 15 Year Olds
Walgreens Bunce Rd
55Th And Kedzie Elite Staffing
From This Corner - Chief Glen Brock: A Shawnee Thinker
My Reading Manga Gay
Core Relief Texas
Jeep Cherokee For Sale By Owner Craigslist
60 Second Burger Run Unblocked
Japanese Pokémon Cards vs English Pokémon Cards
Prima Healthcare Columbiana Ohio
Best Weapons For Psyker Darktide
20+ Best Things To Do In Oceanside California
968 woorden beginnen met kruis
Lima Crime Stoppers
Traumasoft Butler
Anderson Tribute Center Hood River
Thothd Download
Cleveland Save 25% - Lighthouse Immersive Studios | Buy Tickets
Studentvue Calexico
Worland Wy Directions
53 Atms Near Me
David Turner Evangelist Net Worth
Volstate Portal
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.