Directories & Files
This course will get you up to speed with the fundamentals of Linux and prepare you for further study.
In this lesson, we'll look at different ways to display the contents of files and how to use the Nano editor. You can use the cat command to display the entire contents of a file. The more command will allow you to browse through a file or page through a file. The less command is like more, except it has more features. Less is more.
When using the commands more or less use the space bar to advance to the next page. Use the inner key to advance one line and type Q to quit viewing the file. The commands are based on the vi editor which we'll be covering later in this lesson. By default head and tail only displayed 10 lines. So, if you wanted to see the last 15 lines of file for example, you could use tail space dash one five space the file name.
The cat command can be a fine way to view files that have static content. However, if you're trying to keep up with changes that are being made to a file in real time, cat's not your choice. You should use tail space dash F followed by the file name. You would use this to look at files that change often, for example, log files. You may want to start a program and look at the program's log file to see exactly what it's doing. Again in this case use tail space dash F and the file you want to follow.
Let's look at this file with a cat command. If we run head against that file we will see the top 10 lines of that file. If we only wanted to see the top two lines we could run dash two And the tail command will display the last 10 lines in that file. Let's say we only want to look at the last line, we could do tail dash one. And that's the last line of the file.
Let's run more against this file here. We can see some information. It's the first page of the file. If we want to get to the next page just hit the space bar. Space bar again for another page. If you want to go down just one line, hit the enter key. So one line, another line, another line and to quit you just type Q. The less command is very similar. Again, space bar goes down a page, the enter key down one line and Q to quit.
Now this file is growing. So let's take a look at this. We'll use dash F with tail to follow the file. And as you can see the file is being written to and the screen gets updated with that information as the file grows. To exit, just type control C. If you need to edit the file right now and don't want to spend any time learning an obscure editor, use nano. Nano is a clone of pico, so if for some reason, the nano command isn't available, try pico. It's very easy to learn, but it's not as advanced as some of the other editors like vi or emacs.
Let's edit this file with nano. Nano file dot text. So when you start nano, you can see the file's contents and a list of commands at the bottom of your screen. The caret symbol represents the control key. So if we look in the bottom left-hand corner we see caret X, which means control X to exit. I'll do that now. Control X. And we are exited out of nano.
Let's go back in. Editing with nano is pretty easy. Arrow keys work as you would expect. Down, right, up, left. If you want to save a file, hit control O and hit enter. Let's make some modifications. So again, just place your cursor wherever you want to go and start typing there. And control X to exit. And it says "do you want to save your changes or not?" In this case, we'll say no. We won't go back in again. And another important thing to point out is control G to get help. And this will show you more information about how to use nano. There are various commands you can use to display the contents of files, including cat, head, tail, more and less. The nano editor may not be extremely powerful but it is easy to use and you can learn it in a very short amount of time.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.