 With Linux, most of the commands we run, sometimes they take an input, like an option or a file, and produce some output. Let's look at the concepts of the input and output of programs. And I've got a presentation. It's in the ITS332 directory on your computer. It's called, what is it called? Linux standard interaction. Standard input, output, and error. These are concepts which become quite important when we want to build complex commands on the terminal. We think we run a command. The input to that command is referred to as standard input. STDIN is the short name, the standard input. So often we run a command and it takes some input. We haven't seen many today, but we can use an input to determine what that command will do. The output is what's printed on the screen. You run the command of LS and it shows a set of lines on the screen. That's the output that's referred to as standard output. If you run a command and there are errors, then that's distinguished from the normal output. And it's called standard error. I'll show you an example. LS, the standard output, is these lines of text. That's the standard output. ABCF, I tried to run a command that didn't exist. My terminal shows me ABCF command not found. This is an error message. Although it's shown on the screen, it's referred to as standard error. So when you run commands, the normal output is standard output. Error messages are distinguished and sent to what's called standard error. So we have input to commands, output, normal output and error output. Simple. And the input is typed on the terminal, and the standard output and by default standard error are displayed in the terminal. Normally when we type command the input, we type on the terminal. It's what we type in response to the command and the outputs displayed on the terminal. Now, we can change that. We can redirect the output to a file. So normally the output, the standard output displayed on the terminal, what we can do is send the output to a file and save it in a file. This is called redirection. And to redirect to a file, we use the greater than sign. Let's try it. LS, normally the output of LS is shown on the screen. We redirect using the greater than sign and then a file name. You can choose whatever file name you like. Press Enter and see what's shown on the screen. Nothing. So the output of the command is no longer shown on the terminal. Where is it? It's in the file out.txt. Have a look. So the file now contains what normally would have been printed on the screen. So we redirect the output of the command from the terminal instead from the terminal we send it to a file. Very useful if you want to look at that output later. So that's redirecting the standard output to a file. Use the greater than sign. You can redirect both the output and error messages to a file using this special two characters of ampersand greater than. So note the difference here. The greater than sign shows only the normal output and saves that in a file. Any error messages are still displayed on the screen. I'm not sure if I can find an example. Let's try it. What about our find? I'll just scroll through. Before we searched in our file system for PDF files, let's instead of display the output on the screen let's display it into my file called... I don't care what the name is. So run this find command and every file we find, save it in this file x.txt. What happens? It runs, but the error messages, these permission denied messages are shown on the screen. So the normal output is saved to a file, but the error messages are still shown on the terminal. The tilde character that I typed, the squiggly line, means my directory, my home directory. So the command I executed was saying, I think it wasn't necessary there, but what I executed was find, save the output, the standard output in my home directory, a file called x.txt. If you look in x.txt, using less, you should see the files which were the PDFs, the whole list of PDFs. So note there's a difference between standard output, the normal results, and error results, standard error. If you want to redirect the errors to the file as well, add the ampersand character. I hope it works. Yes. So now if you look in x.txt, you will see the files found plus those error messages. The error messages are caused because I don't have permissions to read some directories. So redirection, we can redirect different things. There are many different ways to do it. Redirect the standard output to a file. Redirect both the standard output and the error messages to a file. We can redirect our files to standard input. I will not show an example now. We'll see one maybe this afternoon. That is, if a command takes input, instead of typing that input, you can take the input from a file. So it's as if you've already typed it into a file, run that command using what you typed into the file as input to that command. So read from the file the input. We'll see one this afternoon of that. Next thing. So this is redirection. Change where the input and output comes from and goes to. Piping. Normally when we run a command, the output is text. Well what we can do is run a command, it produces output, and take that output and send it into another command. And that second command executes using the output of the first command as input. It's called a pipe. And the character we use is the vertical bar. Take the first command, execute it, the output is then used as input to the second command. And the output of the second command is then, in this case, shown on the screen. Let's see an example or a few examples of a pipe. When we do LS, I see the list of files. LS minus L shows you the details. I can pipe the output of the LS, in this case the LS minus L with all the details, and filter it and send it to a new command. And I'll use grep to search for a particular word. So my aim, list all the files, and of that list, print just the lines which contain the word tears. And you see the two lines from the LS which contain the word tears. Now we could have done the same thing just using LS in this case. Just an example of take the output of the first command and then pipe it in to the input of the second command. And then you can have actually multiple commands. And using pipes and redirection is very powerful to build up complex commands based upon very simple ones. As an example, from our introduction to the course, or the poster for the course, this is a command, don't try and run it. Sorry, I've got the slideshow on. This is one long command actually. The slash in the end just means it goes wraps around a line. You'll see this, or actually multiple commands, you'll see these vertical bars in here. Meaning run some command, here run this tail command. You don't have to understand all of it, but run this tail command here and take the output of that and pipe it into this next command TR. And there was a redirection there. A redirection from A redirected into F. So more so this afternoon we'll start to combine some commands using pipes. Let's give some more real examples. Remember your aim today is to understand this command. Understand why did it print those four or five lines on the screen. Now if you run the command today, it may not print the exact same lines, but it'll be close. Let's go back to our terminal for the last few examples. What else can we do? Let's say there are other ways to do it, but my LS minus L showed me all files in detailed long format. GREP tiers shows me those lines which contain the word tiers. And we see all these strings, this one line. Note that each line is space separated. These characters are space one, space student, space student and so on. And it's the same format. Let's say I just want to get the file size. Which field is it if it's space separator? The first field, the second field. We'll see how we can do that. Think of a space-separate field. The first field, the second, the third, fourth. The file size is the fifth field in that output. Let's pipe it into a program called cut, which can cut an input when the delimiter between fields is a space. Minus D means the delimiter between fields. And we want the fifth field. Sorry, it's wrapped around minus F space five. Cut cuts text according to the spec you give it. And the spec I'm giving it is to say the fields are delimited by space. I'll run it and then show it again if people cannot see. And the fifth field. It got one of them. Let's try again. And it prints this big 38, 380 megabytes. What about the second line? It doesn't print anything. Why? Why did it not work? Let's get, say, the first field. Instead of field five, field one. For each line, it prints field one if the fields are separated by a space. The first to the third field. One to three. The first three fields. The first to the fifth field. Something didn't go as I hoped. What went wrong? What went wrong is that there are in fact two spaces here. Okay, so cut separates fields by spaces. There's a single space here, meaning this is the fourth field. This is the fifth field. Whereas student on this line is the fourth field. There's a space. The fifth field is non-existent. There is no value. The sixth field is 83, so on. So cut is only separated by a single space in this case. So if our input has multiple spaces, it doesn't work very well. We can cut, like I said at the top, we can even cut specific characters and pipe that into cut. Cut the first field from our first output and then pipe that into another cut that cuts characters two to four. So the first field was these ten characters. The next cut grabs just characters two, three and four, which is rw-rw-dash. So cut is just a way to split up strings based upon field delimiters, based upon characters and a few other things. We'll make use of it in some further examples later. What else? Redirection. So this is pipes. Of course, we could redirect all that to a file. Almost done. Some other shortcuts. I think everyone should have two terminals open, at least two terminals. One, you see what I type and one is what you type. Of course, you can copy and paste. If you're lazy, you don't want to type something. Copy and paste from one terminal to another. In most Linux systems, you can do a quick copy and paste by select. Use your mouse. Select. Don't right-click. Select. So you're not about to see it with me, but I can select something. If you want to select a word, you can double-click. And now just move and don't right-click. Middle-click. Pastes. Paste what was selected. So just select as you would select anything with your left mouse button. Once you've selected what you want to copy, it's already copied. And then to paste it, middle-click. On your mouse, the middle scroll button. Click that. That paste. What if you don't have a middle-click? We were supposed to hold this lab in the Mac room and we were going to use VirtualBox, but they don't have a middle-click. So you get a different mouse. But most mouses, you can emulate a middle-click with two buttons. So my laptop, for example, has two buttons only. There's no middle button. Pressing both at the same time emulates a middle-click. And it's usually the case on normal mouses. Not on Mac mouses. So middle-click, if there's no middle button, double-click left and right at the same time. So let's just finish with a few more shortcuts. Can I remember some? You have a command. You want to change something at the start. Press Ctrl-A. Moves your cursor to the start of the line. Ctrl-E to the end. Ctrl-A to the start of the line. Ctrl-E to the end. If you need to edit a long line, they are quite useful. If you want to delete something, you can use Ctrl-K. Ctrl-K to cut everything in front of the cursor. Ctrl-Y pastes. I'll do it again. Look where my cursor is. It's at the start of the cut. Ctrl-K deletes it. It actually cuts it. Ctrl-Y yanks or pastes that. So we can do some basic things for copy and paste, cut and paste, either using the keyboard, or just use your mouse. The mouse select and middle click is very powerful, very quick to copy and paste. Any commands that we've missed that we may need this afternoon. Any questions as we head into the lunch break? The last five or 10 minutes, so I think maybe at 12 o'clock we'll go have lunch. So the next 10 minutes, just try some of the things. We'll come around and answer any questions. This afternoon, as we know the basics from this morning, we'll just do some other things like convert some videos, manipulate some PDFs, and create some scripts where we put a set of commands into a file and execute them all at once. Which can be much more powerful than just running them individually. So I'll stop talking now. So the last 10 minutes, just try some different things. We'll come around, ask some questions, and then we'll have a break.