Reading Larger Files

The cat command reads small files nicely. But as you learned in Redirecting Streams of Text, some files are too large to read with cat because they scroll off the screen, so you can use the more command to display a file one page at a time. Let’s review that command to explore the contents of the system log, the log that holds messages from various applications and operating system processes.

On Ubuntu, you’ll find this in /var/log/syslog:

 $ ​​more​​ ​​/var/log/syslog

On macOS, the system log is located at /var/log/system.log instead:

 $ ​​more​​ ​​/var/log/system.log

The first page of the file will display on the screen. Press the Enter key to see the next line of the file, and the Spacebar key to jump to the next page. Press q to quit, or page to the end of the file to return to your prompt.

The more command is a legacy program designed to read a file forward only, with no way to go backward. That’s why you have the newer less command.

Less Is More

The less program was introduced to overcome some of the limitations more comes with. With less, you can navigate through the file using arrow keys and even perform some searches. For example, pressing / and typing in a search term followed by the Enter key jumps to the first occurrence of the search term and highlights the other entries.

On many systems, the more command is simply an alias of the less command, which causes some confusion. If you don’t see any difference between these programs on your system, then that’s probably what’s going on.

less has many more features, and as a result it’s also a lot bigger. Some smaller Linux distributions don’t include it at all, so it’s good to know the differences between these commands. But for daily use, your OS includes the less command, so you should be comfortable using that.

Both less and more are handy ways to read through a large amount of text, but sometimes you don’t need to see the whole file. Sometimes you only want to look at the beginning or the end.

Reading the Beginning and End of a File

Sometimes the most interesting information in a file is in the first few lines or in the last few lines. That’s where the head and tail commands come in handy.

The head command reads the first ten lines from a file and displays them to the screen:

 $ ​​head​​ ​​/var/log/syslog

If you want a different number of lines from the file, use the -n argument to specify the number of lines you want to see. To grab the first line in the /var/log/syslog file, use this command:

 $ ​​head​​ ​​-n​​ ​​1​​ ​​/var/log/syslog

The tail command displays the last ten lines from a file by default.

 $ ​​tail​​ ​​/var/log/syslog

Like head, tail also supports specifying the number of lines you want to view by using the -n switch. However, with tail, the count starts from the end of the file. Try it out with the names.txt file you created in Writing Multiple Lines to a File. Use the -n switch to show only the last two names in the file:

 $ ​​tail​​ ​​-n​​ ​​2​​ ​​names.txt
 Lisa
 Maggie

If you specify the number with a plus sign, tail will start counting from that line and display everything until the end of the file. Give it a try. Read the names.txt file but don’t display the first two lines. Instead, tell tail to start with the third line:

 $ ​​tail​​ ​​-n​​ ​​+3​​ ​​names.txt
 Bart
 Lisa
 Maggie

The first two lines are skipped. This is also a handy way of removing lines from a file or splitting a file. Use the > symbol to send the result to a new file instead of to the screen:

 $ ​​tail​​ ​​-n​​ ​​+3​​ ​​names.txt​​ ​​>​​ ​​children.txt
 $ ​​cat​​ ​​children.txt
 Bart
 Lisa
 Maggie

tail has another powerful feature that comes in handy when you’re debugging things. You can use tail to “follow” a file and see its changes on the screen. Give it a try:

 $ ​​tail​​ ​​-f​​ ​​/var/log/syslog

You’ll see the last few lines of the file displayed on your screen, but you won’t be returned to your prompt. As changes happen in the file, your screen will update, displaying the new lines. This is incredibly helpful when viewing the logs for an application or other process in real time.

Press Ctrl+c to stop watching the file and return to your prompt.

You’ll work with less, head, and tail again in Creating Pipelines of Data. But let’s shift focus and look at creating files outside of the home directory.

Creating Files on the Rest of the Filesystem

So far, you’ve worked in your home directory, where you have permission to write files. But sometimes you’ll need to create files in other directories, like /etc or /var.

Try to create a file named mylog.txt in the /var directory:

 $ ​​touch​​ ​​/var/mylog.txt
 touch: cannot touch '/var/mylog.txt': Permission denied

Since you don’t have write permissions to the /var directory, the command fails. You’ll have to run the command as an administrative user.

In Elevating Privileges, you used the sudo prefix, which lets you execute commands with elevated privileges. Rerun the command with sudo:

 $ ​​sudo​​ ​​touch​​ ​​/var/mylog.txt
 [sudo] password for brian:

You’re prompted to enter the password for your user account. This is your password, not the administrator’s password. This is a security precaution; it prevents someone else from doing bad things to your computer if you’ve left it on. Once you enter the password, the file gets created. But if you look at the permissions for the file, you’ll see that it’s owned by the root user:

 $ ​​ls​​ ​​-l​​ ​​/var/mylog.txt
 -rw-r--r-- 1 root root 0 Mar 2 13:03 /var/mylog.txt

To modify the file in the future, you’ll have to either modify the file’s permissions or ownership, which you’ll learn how to do in Managing File and Directory Permissions, or continue to use sudo.

There’s one other catch. You can use touch to create a blank file, but if you wanted to create a file with contents by using the redirection method or cat, it’s not as simple. Try to append some text to the /var/mylog.txt file:

 $ ​​sudo​​ ​​echo​​ ​​"Line one"​​ ​​>>​​ ​​/var/mylog.txt
 bash: /var/mylog.txt: Permission denied

Permission denied? But you used sudo! Why doesn’t it work? Well, you used sudo for the echo command. But the redirection happened as your regular user—it wasn’t part of the sudo process.

One way to get around this is with the tee command. The tee command takes a program’s output and redirects it to the screen and a file. You’ll learn how to do that in Splitting Streams with tee. It’s a bit of a hack, so we’ll move on for now.

You’ve spent a lot of time looking at the various ways to work with files. Let’s look at how to work with directories so you can manage those files.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.120.206