Command line quick tips: Reading files different ways

Fedora is delightful to use as a graphical operating system. You can point and click your way through just about any task easily. But you’ve probably seen there is a powerful command line under the hood. To try it out in a shell, just open the Terminal application in your Fedora system. This article is one in a series that will show you some common command line utilities.

In this installment you’ll learn how to read files in different ways. If you open a Terminal to do some work on your system, chances are good that you’ll need to read a file or two.

The whole enchilada

The cat command is well known to terminal users. When you cat a file, you’re simply displaying the whole file to the screen. Really what’s happening under the hood is the file is read one line at a time, then each line is written to the screen.

Imagine you have a file with one word per line, called myfile. To make this clear, the file will contain the word equivalent for a number on each line, like this:

one
two
three
four
five

So if you cat that file, you’ll see this output:

$ cat myfile
one
two
three
four
five

Nothing too surprising there, right? But here’s an interesting twist. You can also cat that file backward. For this, use the tac command. (Note that Fedora takes no blame for this debatable humor!)

$ tac myfile
five
four
three
two
one

The cat file also lets you ornament the file in different ways, in case that’s helpful. For instance, you can number lines:

$ cat -n myfile
     1 one
     2 two
     3 three
     4 four
     5 five

There are additional options that will show special characters and other features. To learn more, run the command man cat, and when done just hit q to exit back to the shell.

Picking over your food

Often a file is too long to fit on a screen, and you may want to be able to go through it like a document. In that case, try the less command:

$ less myfile

You can use your arrow keys as well as PgUp/PgDn to move around the file. Again, you can use the q key to quit back to the shell.

There’s actually a more command too, based on an older UNIX command. If it’s important to you to still see the file when you’re done, you might want to use it. The less command brings you back to the shell the way you left it, and clears the display of any sign of the file you looked at.

Just the appetizer (or dessert)

Sometimes the output you want is just the beginning of a file. For instance, the file might be so long that when you cat the whole thing, the first few lines scroll past before you can see them. The head command will help you grab just those lines:

$ head -n 2 myfile
one
two

In the same way, you can use tail to just grab the end of a file:

$ tail -n 3 myfile
three
four
five

Of course these are only a few simple commands in this area. But they’ll get you started when it comes to reading files.

Using Software

14 Comments

  1. jun.zhou

    thankyou.The article is very simple,but userfull.

  2. João Rodrigues

    The name cat comes from the word concatenate (and not from the animal).

    Another useful uses of head and tail (that I always mix-up) are the signed “-n” arguments.

    tail -n +NUM makes tail start outputting at line NUM

    So, if you would want to print all the lines except the first one, you’d use

    $ tail -n +2 myfile
    two
    three
    four
    five

    head -n -NUM outputs every line except the last NUM lines.

    So, if you want to output everything but the last line:
    $ head -n -1 myfile
    one
    two
    three
    four

  3. svsv sarma

    Very educative and informative indeed. Different moods of the same command is interesting. With GUI, Terminal has lost its importance. But in Linux, particularly in Fedora, Terminal has its own significance, for use with DNF, APT and Dracut etc. I am beginning and trying hard to understand its power now.
    However, with the command $ cat or $ tac, the Terminal stuns and no output is shown!

    • The reason no output is shown is that, if you don’t include a file name, both cat and tac expect to be fed data from the standard input (usually provided by a pipe). We’ll definitely cover using the pipe in a future article.

  4. ryuuy

    better way is using Ruby
    https://github.com/tombenner/ru
    https://www.sitepoint.com/ru-ruby-shell/

    $ru ‘map(:to_i).sum’ myfile
    sum of numbers in file

  5. peter p

    i also like using sed for printing lines of a file.
    sed 25q
    will print the first 25 lines.
    while
    sed -n 7,10p
    prints lines 7 thru 10.

  6. Declan McNulty

    My favourite is “tail -f “. The f stands for “follow”. As the file changes on disk, any additional lines appended to the end are immediately displayed.
    Very useful for following a log file in real time for troubleshooting.
    For example you could run:
    tail -f /var/log/messages
    …while asking a user to repeat some action that did not complete as expected last time.

  7. Daniel

    a very useful flag for tail is:
    tail -f myfile
    to output appended data as the file grows.

  8. John G

    I had not noticed that

    more

    will keep the file output in the terminal after you exit, where

    less

    will not.

    Thanks for this interesting fact!

  9. Mehdi

    Nice article!
    Also, one useful of tail that I learned from our network guy is the following:

    tail -f filename

    This shows the end content of the file real-time; very useful for observing log files for example on a development system.

  10. Ralph

    You mentioned that less does clear the screen on quit, while more leaves the last viewed screen visible, and recommended to use the more pager if one wanted the last screen still visible on return to the shell prompt.
    Although this is true for less’es default behaviour there is no need to use more any more (no pun intended) and one should ditch it, unless one is forced to view text files on legacy Unices like e.g. AIX, HP-UX or Solaris, where less per se isn’t available.

    For the folks who are only out and about on Linux less should almost always be preferred.

    If you want less to not clear the screen on quit simply use the -X option.

    e.g.

    less -X /etc/fstab

    Now when you press q you will be returned to the shell prompt, but still see above it the last viewed screen page of the file you viewed.

    If you require this behaviour in general and you are tired of always typing extra options that you need (also others besides -X), you could either set an alias (e.g. in your ~/.bashrc and export BASH_ENV=$HOME/.bashrc),
    or export the environment variable LESS and assign it any numer of options that you could read about in man less (e.g. also in .bashrc or .bash_profile).

    Or simply pass LESS=”-X” as environment to the immediately to the issued command.

    e.g.

    LESS=-X man less

  11. João Rodrigues

    A less known fact about man is that you can render man pages in postscript format.

    $ man -t bash > bash.ps
    $ xdg-open bash.ps

    It’s great if you need a printed version of a man page.

    You can also read man pages in html
    $ man –html=firefox bash

    or
    $ export BROWSER=$(which firefox)
    $ man -H bash

  12. fmatejic

    while read line; do printf “$line\n”; done < /etc/fstab
    Without calling extend command 😉

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions

%d bloggers like this: