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:
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
The file command classifies files into three categories:
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
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
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
Finally, if you don’t want tab characters to appear, use the -T parameter:$ cat -T test1
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.
Shell Scripting Related Interview Questions
|Perl Scripting Interview Questions||Python Interview Questions|
|Linux Interview Questions||Linux Embedded systems Interview Questions|
|AWK Interview Questions||BioPerl Interview Questions|
|Sed (Stream Editor) Interview Questions||Advanced Linux Interview Questions|
|Unix/Linux Interview Questions||Unix Shell Scripting Interview Questions|
Shell Scripting Tutorial
Starting With Linux Shells
Getting To The Shell
Basic Bash Shell Commands
More Bash Shell Commands
Using Linux Environment Variables
Basic Script Building
Understanding Linux File Permissions
Working With Editors
Using Structured Commands
More Structured Commands
Handling User Input
Adding Color To Scripts
Introducing Sed And Gawk
The Ash Shell
The Tcsh Shell
The Korn Shell
The Zsh Shell
Using The Web
Using A Database
Shell Script For Administrators
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.