# Viewing File Contents - Shell Scripting

So far we’ve covered everything there is to know about files, except for how to peek inside of them. There are several commands available for taking a look inside files without having to pull out an editor. This section demonstrates a few of the commands you have available to help you examine files.

Viewing file statistics

You’ve already seen that the ls command can be used to provide lots of useful information about files. However, there’s still more information that you can’t see in the ls command (or at least not all at once).

The stat command provides a complete rundown of the status of a file on the filesystem:

$stat test10 File: "test10" Size: 6 Blocks: 8 Regular File Device: 306h/774d Inode: 1954891 Links: 2 Access: (0644/-rw-r--r--) Uid: ( 501/ rich) Gid: ( 501/ rich) Access: Sat Sep 1 12:10:25 2007 Modify: Sat Sep 1 12:11:17 2007 Change: Sat Sep 1 12:16:42 2007$

The results from the stat command show just about everything you’d want to know about the file being examined, even down the major and minor device numbers of the device where the file is being stored.

Viewing the file type

Despite all of the information the stat command produces, there’s still one piece of information missing — the file type. Before you go charging off trying to list out a 1000-byte file, it’s usually a good idea to get a handle on what type of file it is. If you try listing a binary file, you’ll get lots of gibberish on your monitor, and possibly even lock up your terminal emulator.

The file command is a handy little utility to have around. It has the ability to peek inside of a file and determine just what kind of file it is:

$file test1 test1: ASCII text$ file myscript
myscript: Bourne shell script text executable
$file myprog myprog: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), not stripped$

The file command classifies files into three categories:

• Text files: Files that contain printable characters
• Executable files: Files that you can run on the system
• Data files: Files that contain nonprintable binary characters, but that you can’t run on the system

The first example shows a text file. The file command determined not only that the file contains text but also the character code format of the text. The second example shows a text script file. While the file is text, since it’s a script file you can execute (run) it on the system. The final example is a binary executable program. The file command determines the platform that the program was compiled for and what types of libraries it requires. This is an especially handy feature if you have a binary executable program from an unknown source.

Viewing the whole file

If you have a large text file on your hands, you may want to be able to see what’s inside of it. There are three different commands in Linux that can help you out here.

The cat command

The cat command is a handy tool for displaying all of the data inside a text file:

$cat test1 hello This is a test file. That we’ll use to test the cat command.$

Nothing too exciting, just the contents of the text file. There are a few parameters you can use with the cat command, though, that can help you out.

The -n parameter numbers all of the lines for us:

$cat -n test1 1 hello 2 3 This is a test file. 4 5 6 That we’ll use to test the cat command.$

That feature will come in handy when you’re examining scripts. If you just want to number the lines that have text in them, the -b parameter is for you:

$cat -b test1 1 hello 2 This is a test file. 3 That we’ll use to test the cat command.$
Fancy! If you need to compress multiple blank lines into a single
blank line, use the -s parameter:
$cat -s test1 hello This is a test file. That we’ll use to test the cat command.$

Finally, if you don’t want tab characters to appear, use the -T parameter:

$cat -T test1 hello This is a test file. That we’ll use to^Itest the cat command.$

The -T parameter replaces any tabs in the text with the ^I character combination.

For large files, the cat command can be somewhat annoying. The text in the file will just quickly scroll off of the monitor without stopping. Fortunately, there’s a simple way to solve this problem.

The more command

The main drawback of the cat command is that you can’t control what’s happening once you start it. To solve that problem, developers created the more command. The more command displays a text file, but stops after it displays each page of data. A sample more screen is shown in Figure below.

Using the more command to display a text file

Notice that at the bottom of the screen in Figure above the more command displays a tag showing that you’re still in the more application and how far along in the text file you are. This is the prompt for the more command. At this point you can enter one of several options, shown in Table.

The more command allows some rudimentary movement through the text file. For more advanced features, try the less command.

The less command

Although from its name it sounds like it shouldn’t be as advanced as the more command, the less command is actually a play on words and is an advanced version of the more command (the less command uses the phrase ‘‘less is more’’). It provides several very handy features for scrolling both forward and backward through a text file, as well as some pretty advanced searching capabilities.

The less command also has the feature of being able to display the contents of a file before it finishes reading the entire file. This is a serious drawback for both the cat and more commands when using extremely large files. The less command operates much the same as the more command, displaying one screen of text from a file at a time. Figure below shows the less command in action.

Viewing a file using the less command

Notice that the less command provides additional information in its prompt, showing the total number of lines in the file, and the range of lines currently displayed. The less command supports the same command set as the more command, plus lots more options. To see all of the options available, look at the man pages for the less command. One set of features is that the less command recognizes the up and down arrow keys, as well as the page up and page down keys (assuming that you’re using a properly defined terminal). This gives you full control when viewing a file.

Viewing parts of a file

Often the data you want to view is located either right at the top or buried at the bottom of a text file. If the information is at the top of a large file, you still need to wait for the cat or more commands to load the entire file before you can view it. If the information is located at the bottom of a file (such as a log file), you need to wade through thousands of lines of text just to get to the last few entries. Fortunately, Linux has specialized commands to solve both of these problems.

The tail command

The tail command displays the last group of lines in a file. By default, it’ll show the last 10 lines in the file, but you can change that with command line parameters, shown in Table 8. The -f parameter is a pretty cool feature of the tail command. It allows you to peek inside a file as it’s being used by other processes. The tail command stays active and continues to display new lines as they appear in the text file. This is a great way to monitor the system log file in real-time mode.

The head command

While not as exotic as the tail command, the head command does what you’d expect, it displays the first group of lines at the start of a file. By default, it’ll display the first 10 lines of text. Similar to the tail command, it supports the -c, and -n parameters so that you can alter what’s displayed.

Usually the beginning of a file doesn’t change, so the head command doesn’t support the –f parameter feature. The head command is a handy way to just peek at the beginning of a file if you’re not sure what’s inside, without having to go through the hassle of displaying the entire file.

Login

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd

Shell Scripting Topics