Linux LPI 1 Article 10: Viewing and Printing Files

0
36

Preparing a file you want to print
In case you may wish to print a file, then most likely you may also wish to format the header, the footer and the page breaks and other minor details. While there are many commands in Linux, the pr command was made to handle such kinds of tasks.

$ pr text.txt

The above produces text that is formatted for printing assuming an 80-character line length in a monospaced font. Options are listed below

Options What it does
numcols e.g pr -2 file.txt Produces many columns of the file
-d or –double-space Produces double-spaced output from single spaced one
-F, -f 0r –form-feed Produces form-feed output suitable for some printers
-l lines or –length=lines Sets the page length in lines
-h text or –header=text >Sets the header in the file replacing the file title
–header=”My File” To produce a multi-word header
-o chars or –indent=chars This one sets the left margin to the specified chars characters.

 

Commands made for viewing files
There are various ways that the contents of a file can be viewed in Linux. This makes it very flexible and reliable because several circumstances have been conceptualized and their way of viewing them made to befit them all. The following are some of the file viewing commands:
I. Head
Sometimes you may just wish to peruse or rather view just a few contents of a file that you are not familiar with. You may just wish to have a grasp of what it is all about from the contents at the beginning of it. The head command does this so effectively
$head file.txt
If you want to specify the number of lines you want then You can change it with the -n num or –lines=num option.
II. The use of less
less is a command that enables you as an administrator to view the very first few lines of a file instead of having the whole file dumped to your shell. It serves convenience especially if the file being viewed is lengthy. What it further does that s o elegant is that it gives you the liberty to page through it few lines at a time or a page a time depending on what you would wish to do at that moment. You can actually view the file back and forth using a combination of keyboard keys as described below:
The spacebar: if you would wish to move through the file a screen at a time, then just press or hit the spacebar and there you have it.
Moving to a specific line: this can be accomplished by g20 for example to go to line 20 of the file.
The / key: if you would wish to search for a word in the file, just do /project for instance to find the word project in the file. Hitting n during the search process will land you in the next instance of the word being searched. Try it practically good people so that you can fully understand how it works.
The ? Key: tying ?project will search for the word project in the file but in a backward manner.
Hitting Esc and then V: this will enable you to page through the file a screen at a time in reverse. Opposite to pressing the spacebar described earlier.
Up and Down keyboard keys: These keys are used if you want to maneuver in the file a line at a time.
To exit from the less pager, hit q.
III. The use of tail
It is logical to think that if a file’s first few contents can be viewed via the head command, then there has to be a way of viewing the last contents of the file. If you happened to think like that, then you are amazingly right. The tail command has got many options as explained below.
-f or –follow: The -f option enables you as the admin to view incoming updates that are being written in the file in real time. As an example, tail -f /var/log/pacman.log gives you the opportunity to view the log messages being written in the pacman app log file. This is handy if you would wish to find out errors or to see what is taking place in a certain application in real time.
There are other vital commands that come in handy during your beautiful experience of administering systems. I know an example will service your curiosity well in this context. Let us say you want to extract a particular portion of a file to pipe it o another program without the hassle of paging through it all.
What if you wanted data about the file such as the number of words, letters and other data about the contents of the file?
Here comes the tools in your box to do the aforesaid stuff. The former can be accomplished with the cut command while the latter can be done using the wc command standing for “word count.”

cut

Extracting data by field: the option used is -f list or –fields=list
Extracting data by byte: -b list or –bytes=list
Extracting data by character: -c list or –characters=list
As you go into advanced topics such as writing scripts to accomplish tasks, such commands as cut are very important especially if specific parts of a file are being targeted for a certain purpose or use. Those parts can be extracted from the files and used by means of cut command.

wc

The word count command (wc) is used to produce the number of words that a file is made of together with its line and byte counts. The options associated with the commands that we have discussed so far are the basic ones. Their man pages have depth and cover more options that can be added to the commands to access more power and more functionality. Good people, it is highly advised that you consult the man pages of the commands that steal your attention because there is much more than you can imagine.