Episode: 924 Title: HPR0924: LiTS 000: redirection Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr0924/hpr0924.mp3 Transcribed: 2025-10-08 05:01:21 --- Welcome to the inaugural episode of Linux in the Shell. My name is Dan Washco and I will be your host. You might be asking yourself, why is this on Hacker Public Radio? I'll get to that in just a minute. Linux in the Shell aims to be a fortnightly engagement whereby we discuss a Linux command or concept applied in the best shell. I'm going to take a three-prong approach to doing this. First off, there is going to be a write-up on the website, Linux in the Shell.org. That entry is going to discuss the command or concept in detail and it's going to provide some examples. I'll be citing my resources and other links that you could follow to get some more information beyond the what I cover. Secondly, there's going to be this audio that's released on Hacker Public Radio. It's going to summarize the command and not go into detail so much as the examples because nothing makes for drab audio than trying to spell out Linux commands and flags and everything. The audio is going to supplement the entry so that if you've read the entry, the audio will help solidify it in your mind or if you've listened to the audio, you'll be a go over to the website and further understand the commands or concepts that I was talking about. Finally, there's a video component to each engagement, each entry, whereby I will be demonstrating the use of the command or the concepts in the bash shell. That video is going to be available for streaming off of YouTube, it'll be on the website or but I'm also going to make it available for download in AUG Theora format, off of archive.org. That gives you three ways to learn, reading, listening and watching. So let's have some fun. Before we begin, I want to thank the guys over at Hacker Public Radio, the people over there, for giving me the opportunity to put this audio out, but also for hosting the website. When I approached Ken Fallon about this idea, he was very enthusiastic, as always, and he was more than willing to work with me. The episodes will be coming out on a four-nightly basis, that's every two weeks or biweekly for you non-Europeans out there. We're starting on February 16th, on Thursday, February 16th, and we'll be doing releasing episodes every two weeks up until March 22nd. Then starting March 27th, we'll be moving to our usual time slot on a Tuesday, and it will be every other Tuesday from there. I hope we have a lot of fun in doing this, I'm excited, I hope you are too. If you want to send feedback over on the website, there's a contact page, or you can send me my email information in there, Dan at thelinuxlink.net. You can post comments in the entries, I'll be allowing comments on the YouTube videos, I will also be publicizing this on Facebook, Twitter, Google Plus. You can post comments and feedback there. I don't know about status.net yet, the last time I tried to get in there, it would not let me in, so that's still up in the air for that. Again, I hope you enjoy the ride, and without further ado, let's hit on entry number zero. Entry number zero, we are going to talk about redirection today, not an official Linux command, but a concept. I chose this because we are going to be using this a lot in the upcoming episodes, so I wanted to make sure that you had a good understanding of redirection. The Unix philosophy posits simplicity in program design, and it also posits that a program do one thing and one thing very well, and that's where redirection comes in the play that makes these applications a program so versatile. You don't have an application that does it all, instead you have a bunch of smaller applications that do one thing, do it well, and allow you to chain these commands together and redirect their output to do something extremely useful. For instance, the LS command does a great job listing files or directories, but it doesn't really do much anything else. You can list them in different orders, by size, by creation time, you can do all sorts of listings, but it's not used to copy files or move files or looking files for strings. You can use the LS command in conjunction with other commands, like XRs or WC to get word lists and do really interesting things, but the LS command does one thing and does it well, lists files and directories. The LS command does a great job listing, and it allows you to expand upon just the basic outputting of the command to the screen. What will we redirect in many of these cases is standard streams. There are three standard streams we're going to be talking about or redirecting. First is standard stream zero, which is called standard input, typically your mouse or keyboard. Second is standard stream one, which is standard output, typically your screen or maybe a printer. And third is standard error, or standard stream two. And that is error codes being displayed by programs. They usually get sent to the same location as standard out, which is your screen. There are three symbols that we are going to be using for redirecting in the bash shell. The first is the greater than symbol. The greater than symbol found right above the period, which is like a V on its side pointing to the right and opening to the left, allows you to redirect standard out to a file. The second symbol is the less than symbol, which is above the comma, and it points to the left and opens to the right. That allows you to redirect standard in from a file to a command. And third, we will be using the pipe key, which is found usually above the backslash, or maybe somewhere else. I've seen it above the enter key, and I've seen it above in the upper left hand corner of the keyboard, or usually around the one key. It looks like a straight line on some keyboards, or it might look like a colon, but instead of periods, they're two vertical lines bisected by just an empty space. It's a pipe key. That allows you to redirect the output of one command to another. The greater than symbol for redirecting standard out, let's say that you wanted to create a file in your home directory that was a listing of all the other files in your home directory. That's very simple to do. So you use the LS command, and if you just type in LS and hit enter, it'll show it on the screen. But if you type LS, the space greater than symbol, and the file name, it will redirect all the output of LS to the file name. Now let's say we wanted to, instead of just redirecting all the files in our home directory, we only wanted to find JPEG files that are in our home directory. We do LS asteris.jPEG and the greater than symbol to the file name. Now if we were using the same file name that we used before, be aware that a single greater than symbol is an overwrite, it's destructive. So if anything existed in that file, it would overwrite the contents of that file and replace it with the output of the LS command. Let's say you wanted to preserve the contents of a file if they existed, and instead of overwriting you want to append to, well that's simple. Just use two greater than symbols. Two greater than symbols will append. Now in either case, if the file does not exist that you're redirecting the output to, it will create it. But remember, one greater than symbol will overwrite the location with the data, two greater than symbols will append the data to that location. When you are running a command, be aware that there are two potential standard output streams. One is standard out, of course, and that's number one. And two, which is number two, is standard error. So if you run a command and it throws an error, you are trying to grab for a string inside a log file that you don't have permissions to read, you will get an error message on the screen that says you don't have permission to read that file. That's standard error. Now if you were to run a command and output the contents to a file, you would get the output of the command going to a file, but standard error would still show up on your screen. That would not be redirected. By default, unless you're specifying a standard stream number, one or two, it's going to only redirect standard out, and you will still see standard error on the screen. Now if you wanted to redirect both standard out and standard error into a file, you would use the ampersand. So you would have the command followed by a space ampersand greater than symbol, and then the file that you wanted to redirect the output and the standard error, too. Now if you wanted to just redirect standard error, replace the ampersand with the two. So it would be the command space to greater than symbol and location you want to go to. That's very handy that if you only wanted to see the output of a command, like say for instance, you were gripping through those log files, but you only wanted to see the output of the commands that you had access to those files to that you had permissions to read. You can redirect standard error to something like DevNull, which would throw it to the bit bucket in sky. You would not see standard error. Or in this case, let's say you wanted to output the command into a file, but you did not want to see or redirect standard error. You can provide two redirections. One, you would do the command space greater than symbol to a file and then space to greater than symbol slash something like DevNull. So now standard out would go to the file of your choice and standard error would go to DevNull. The other symbol for redirection of a file redirecting standard in from a file to a command, that is using the less than symbol. Again, that is the less than symbol where the opening is to the right and it's pointing to the left. That will allow you to take a file and use it as the input to a command. Now not every command will accept a file as an input. So just be aware of that. I have seen the greatest use I've seen of this and one of the most interesting uses I've seen of redirecting to and from a file is the use of the cat command as a quick and dirty text writer, not necessarily text editor, but a text writer. And I've seen this used a lot in Linux from scratch. The cat command will take whatever you're catting and output it to the screen or you could redirect that to a file. So what you're going to do is you're going to take the output of the cat command and direct it into a file. But instead of just executing it like that, you're then going to append an input append so that would be two less than symbols append the input of standard in which would be your keyboard until you get until you type a string like E O F. So it would be cat space greater than symbol to a file space less than less than symbol, double quotes, capital E O F, double quotes, closing them. And when you hit enter, anything you type in will get sent to that file until you type the string E O F. At which point it'll close the command line, close cat, close the file that was being written into and everything you put in there, you type would appear in that file now. This is a quick and dirty way for creating the necessary configuration files and Linux from scratch when you don't have access to a text editor because you're building Linux from scratch. The final symbol we're going to talk about is the pipe. And the pipe allows you to redirect the output of one command to another. This is extremely versatile in chaining together a bunch of these commands to do something incredibly useful. For instance, let's say you wanted to do a directory listing of all your files and with the dash L and dash T dash L does a long listing. So you see the permissions when it was created, the file size, everything, the dash T orders it from the most recently modified or creative file down to the last modified and creative file. When you run this, if you have a lot of files on your screen, it's going to scroll off. You're not going to see the first dozen or so entries in most cases. If you have a lot of files in your home director or director, you're listing. So what you can do is you can use that command. You can pipe it by putting the pipe in to another command like less. So now you would do L S dash L T space pipe space L S S. And now all the output of L S will go into the less page or allowing you to scroll up and down and view the output of the command until you're done looking at it and hit queue to quit out of there. Another example would do the same thing, but instead of piping it to less, you pipe it to head. Let's say you only wanted to see the first 10. So you just pipe it right to head and would show the first 10 lines of the L S command. Head dash 20 will show you the first 20. Similarly, you could just type tail instead of head and it will show you the last couple of lines. That's what pipe allows you to do is pass the output of one command to the input of another. So we talked about three different redirection tools that we will be using, the greater than for redirecting standard out and standard error to a file, the less than symbol for redirecting standard in from a file to a command and finally the pipe, which allows you to redirect the output of a command to another command. In two weeks, we are going to start our adventures with an actual Linux command that I hope you'll find very useful and very exciting. I hope that you will be back and spread the love here. Again, my name is Dan. Hit the website if you haven't already to read up on redirection and see the videos there. Thank you very much and have a great day. You have been listening to Hacker Public Radio at Hacker Public Radio. We are a community podcast network that releases shows every weekday Monday through Friday. Today's show, like all our shows, was contributed by a HBR listener by yourself. If you ever consider recording a podcast, then visit our website to find out how easy it really is. Hacker Public Radio was founded by the Digital.Pound and the Infonomicum Computer Club. HBR is funded by the binary revolution at binref.com. All binref projects are crowd-responsive by linear pages. From shared hosting to custom private clouds, go to lunarpages.com for all your hosting needs. Unless otherwise stasis, today's show is released under a creative commons, attribution, share a line, free those own license.