Join Scott Simpson for an in-depth discussion in this video Peek at files with cat, head, tail, and less, part of Learning Linux Command Line (2016).
- [Voiceover] Because a lot of what we'll be working with at the command line involves text files or text output, it's important to have a few tools in your tool kit to check out the contents of text files. The first one I want to introduce you to is called cat, and it's short for concatenate. Concatenate means to stick two or more things together, and cat can do that but it's often used just to print the contents of a file to the screen. It's also helpful to get the contents of a text file into a series of piped commands. Let's look at some files with cat.
Depending on the operating system you're using, you'll have different files available to you. Normally, we'd use cat to look at a log file or something like that, but here I'll use some classic poems, because I want you to be able to follow along with me exactly, and not get tied up in the differences between Linux distributions at this point. To just list out the contents of a file, I'll write cat followed by the name of the file. In this case, poems.txt. Then I'll press Enter. Because that file is longer than our screen is tall, we can't see the output of it all at once in this terminal window.
Sure, we can scroll back up, but there's some better ways of working with the output. I'll clear the screen with the clear command. The head and tail commands let us see just the beginning or the end of a file. They work just like cat does. I'll write head poems.txt, and I can see the first 10 lines of the text. The same goes for tail. I'll write tail poems.txt, and I can see the last 10 lines of the file.
I'll clear the screen again. I can pass the -n option to both head or tail to tell it how many lines to show me. For example, I'll write head -n5poems.txt and it shows me just the first five lines. tail -n3poems.txt That will show me the last three lines of the file. Combining these with the cat command can make for some good practice using pipes. You can get a good sense of how the order of commands affects the output.
For example, I'll write cat poems.txt | cat -n With the cat command, the -n option numbers all the lines of text. Then I'll put another pipe and send that to tail -n5 We see the original file has been piped into the cat -n command which numbered it. Then tail showed us the last five lines of that. I'll rewrite that line. cat poems.txt but I'll reverse the last two commands.
So I'll pipe this into tail -n5 and then into cat -n. Now it's clear that the file was piped into tail first, which presented the last five lines. Then those lines were numbered by cat -n. That's a pretty basic example, but it's important to keep in mind that order matters with pipes. I'll clear the screen again. There's another command that's useful for looking at longer text files and it's called less. You can use it by itself, with a file name, or you can pipe output to it.
I'll type less poems.txt and I see the text with a little bit of an interface around it. I can scroll up and down with the arrow keys, go down a line at a time with Enter, or go down a screenful at a time with Space bar. I can navigate forward and backward with the F and B keys. Sound familiar? These are the same commands we used earlier to move around in the manual pages. You can press H to see the help for less, if you'd like to learn some more of the commands available, or press Q to quit.
Peeking inside of text files an important skill to get comfortable with when you're working at the command line.
This course will establish the foundation for more advanced Linux topics. Find other Linux training courses here.
- What is the Linux command line?
- Writing Linux commands at the prompt
- Finding help for Linux commands
- Editing files and folders
- Configuring user roles and file permissions
- Using pipes to connect commands
- Peeking at files
- Searching and editing text
- Finding disk and system information
- Installing and updating software