Command line quick tips: Reading files different ways

Image by Ryan Lerch (CC BY-SA 4.0)

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

19 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

    • Rich

      Launching an entire interpreted language to apply line numbers to text files is not a great use of the Unix commandline; especially when atomic Unix commands provide the functionality that’s sought.

      That becomes particularly important when using shell built-in commands, like “printf” or “read”. One of the goals of scripting should be to do as much as possible in one process, without loading up external commands, or deferring to other interpreters. This is why you’ll often see forum applause when someone solves a tricky regex using only bash built-in string manipulation and matching; or using a minimum of external atomic commands like “tr”, “cat” and “tac”, etc.

  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.

    • Rich

      …or uppercase, “tail -F /path/to/file” which is useful for following a logfile that either doesn’t exist yet, or you expect to be rotated.

  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 😉

  13. It’s interesting to me as someone who has used UNIX (since BSD 4.2) and Linux since the kernel version was less than 1! That newcomers see Linux as a GUI OS with an add-on command line! The history of UNIX/Linux is that it is a command line OS with a bolt-on GUI!
    The power of the command line is incredible and no effort spent learning it is a waste. You can achieve lots, quickly and easily.
    If you use Linux, learn something about the command line, don’t fear it! Its not difficult and could make you more productive.

    As an example imagine you have 1000 photos in a directory with names like IMG0001.PNG and you wanted to rename them to img-.png where was replaced with the creation date… Could you do this from the GUI? I could (given a hot cup of coffee and a few mins) craft a command or script to do this.

  14. Ivan

    Why not using awk? 🙂

    1) Print the line from 3 to end:
    awk ‘{if (NR>=3 ) {print $0}}’ filename

    2) Print the line between 2 and 30:
    awk ‘{if (NR>=2 && NR <=30 ) {print $0}}’ filename

    3) print the lines from 1 to 10:
    awk ‘{if (NR<=10) {print $0}}’ filename

  15. Rahul Karkhanis

    Wonderfully written article …

    My favourite is “nl”

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

    $ nl myfile
    1 one
    2 two
    3 three
    4 four
    5 five

    do refer man page for much more interesting options

Comments are Closed

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