Learn how to read and search the contents of Linux files from a command-line interface, including cat, head, tail, less, find, and grep.
- [Narrator] We started by learning how to navigate the file system. Let's take a look at the content of some of these files. The cat Command, which is short for concatenate, sends the contents of a file, to the standard output, like the display. One of the files in the log directory is kern.log, and it contains detailed logs of the messages shown from the Ubuntu Linux kernel, which we saw during boot. Let's display the contents of the kern.log now by typing cat space, then the name of the file, kern.log, and press Enter.
That's a lot of information, but that's everything that was seen during boot. What if I don't want to see the entire file? Often, there's no need to see the entire file, only the beginning or the end. With log files, the end is usually the most important part. There are two commands to do this. The first is head, which accesses the beginning of a file, and tail, which accesses the end of the file. The easiest way to remember the commands is to think of a cat, which has a head at the start, and a tail at the end.
Therefore, if you just want to see the first part of a file, you'd use the head command. Let's see the start of kern.log. So we'll type "head space kern.log." This shows the first few operations when Linux started up. By default, it shows ten lines. Next, let's take a look at the end of the file. So we'll type tail, for the end, and then kern.log. Similar to head, the last ten lines were shown. Seeing the beginning and the end of the file is good, but what if we want to actually scroll through the entire file? The less command is a simple and fast method of allowing a console user to page through the contents of a file, one screen or line at a time.
There are a couple keyboard controls to be aware of. The first is Return, which moves the head forward one line. Next is Space, or f, which will scroll forward one entire page. Conversely, b goes backwards one page. Finally, press q to quit when you're done. Let's take another look at kern.log, but this time, using less. So type "less space kern.log." Press space a couple times to skip pages, then b to go backwards.
When you're done, press q to quit. With all these files, it's not practical to remember where everyone is. To assist, the find command searches for files in a particular directory hierarchy, defaulting to the current directory. Any files that match the optional search pattern will be listed with the relative path and filename. Let's experiment with the find command in the current directory, using the special name "dot." So "find space dot." That's effective, but it's overwhelming.
How can we filter this type of output? The command grep is used for displaying lines that match a given pattern. Patterns can be just text, like a particular word or phrase, or as complex as regular expressions for programmatic matching. Something to note, grep matching is by default case sensitive, meaning capital A won't match lowercase a. This can be turned off with the dash i option, but it's still something to be aware of. On its own, grep can be very useful, but it can be combined with a pipe command, which is just the vertical bar character.
Pipe passes the output from one command to another. You can typically find the pipe key above the return key. Press Shift and the pipe key to make the right character. As an example, let's perform the find command again, but this time pipe the result to grep. So "find space dot space," then "pipe," and then "grep error." This time, we only see log files that have the name "error" in it. Grep can also search a particular file.
By default, grep matches case sensitive, so I'll use the dash i option to perform case insensitive searches. For example, if I want to see all the authentication attempts in the authorization log for a particular username, I'll type the username, jpeck, and then the name of the file. Auth.log. This will match all authorization attempts from jpeck. Speaking of authorization, how do we perform administrative commands?
Note: The demonstrations use the Ubuntu distribution of Linux, but the skills are applicable to other Linux distributions.
- What is Linux and why should you use it?
- Choosing a Linux distribution
- Creating a virtual machine (VM)
- Managing Linux from the command line
- Configuring a Linux server
- Managing Apache sites and modules
- Configuring the MySQL database
- Configuring PHP for development
- Debugging PHP code in Linux
- Installing PHP tools such as Composer and CodeIgniter
- Managing VMs with VirtualBox
- Troubleshooting LAMP