 Now let's go through a few more operations on files, some very basics of how we can use files and manipulate files and especially text files because a lot of things in Linux are based around text files. Let's consider the LS command. When we run the command it displays some output on the screen. In this case the list of files are directories. Let's go into the vertnet directory, ls-l to show the output. It shows us details about those four items inside the directory, bin, data, readme.txt and source. In this case when I run a command the output is displayed on the screen. One useful thing is to instead of send the output to the screen, send the output of a command into another file. And this is called redirection. And a simple way to do it is you run the command and we add the greater than sign to say take the output of that command and put it inside a file and let's put it in a file in the upper directory called ls-out.txt. So the idea here is to run this command instead of print the output of the command on the screen instead save it to a file and that file is going to be called ls-out.txt and the file is going to be in the directory above the current one. Nothing is printed on the screen, but if we go up a directory we see we now have a file called ls-out.txt. We open that in our text editor or in our text viewer less in this case. I just want to view the output and we see it's the output of the ls. So this is redirection. We redirect the output of a command into a file. When we use a single greater than sign it overwrites what's already in that file if that file actually exists. So if we did that again we quit ls and we go into vertnet and we ls without the minus l option into the same output file. The greater than sign means overwrite this file whatever was there before will be deleted. And you can do ls-output file and we see what was in there before is gone. It's been updated with a new information. If you want to not overwrite but append or concatenate you can use two greater than signs. Currently the file contains the output of the ls and now we're going to add the output of the ls-l into that same file. And we see the output of the first command here and the output of the second command here. So that's very useful for redirecting output of commands into files so you can save the output and process it later. There are other forms of redirection like taking the contents of a file and using as an input to a command using the less than sign and a few others. Often we want to search through the contents of files. So I know my, let's go back into our own directory. We can view the output of ls-out using cat that displays the entire file without breaking the screen. Sometimes we want to search through the contents of a file and search for a string in that file. And one simple way to do that is to use a command called grep where we want to grep a particular string. Let's say I want to find all the lines inside that file which contain read and I want to search the file ls-out.txt. And what grep does is search is through that ls-out.txt file line by line and prints out all the lines that contain the string read. So if you're searching for a particular string inside a file grep is a quick way to do that. One thing that we can do is combine commands as well. So here we did a grep and we got the output two lines. What if we want to take those output and let's combine a command and see the output of a particular two commands combined. Let's explain. One of the common networking commands in Linux is to show your interface configuration which we will see in another topic. And the command is called ifconfig. And it shows the configuration of different interfaces. And one of the interfaces I want and because in this case it shows a lot of information across the multiple pages, ETH0, ETH1, many lines, LO. I want to grab all of the lines in that output which mention an internet address, an inet ADDR. And if we browse through we see there's this line with an inet address, this line. And I want to display them on the screen just those three lines. What I can do is run ifconfig and instead of taking the output and putting it on the screen, I will redirect or pipe the output of that command into another command, in this case grep. And search for any string that has inet ADDR. So the process here is that we'll run ifconfig, it will produce some text output. We will not show that on the screen, instead we'll use that text output as input to the next command grep. So really what the grep will do will search through the output of ifconfig for this particular string. And we see the result is it shows those three lines on the screen. So that's a way of combining commands and it's using this bar, vertical bar operator which is called a pipe. Take the output of one command and use it as the input to the second command. To finish this example then we could take the output of that and redirect to a file. Nothing shown on the screen but we now have a file called addresses.txt. We can view that which is simply those three lines of inet addresses. So we've seen a quick example of searching through a file with grep. We've seen redirection using a greater than character and a pipe. One other useful command in some cases is echo. Echo simply displays something on the screen. We can echo something into a file if we like. Echo displays a string on the screen or into a file if we use redirection. In Linux we often operate on text files so it's useful to know some of the simple commands for manipulating files.