5 Linux commands to use for quickly viewing the content of files


Raimund Linke/Getty Images

One of the best things about Linux is that it offers several ways of handling every task. Everything you do in Linux will have an alternative method, from the simplest to the most complex. But don’t worry; there’s no need to get overwhelmed because you can select one method and stick with it. 

Take, for instance, viewing the content of text files. For decades, I’ve used one method, even though I know there are other ways to do it. But my brain always defaults to what’s already ingrained. 

Also: 5 reasons why Linux will overtake Windows and MacOS on the desktop – eventually

This task is what I want to talk about today… viewing the content of text files is a function I find myself doing quite a bit. From code and notes to configuration files (and everything in between), I have to view such files regularly.

But what commands are available for this?

Let me show you five.

1. less

The less command is my go-to, and I’ve been using it since I started using Linux. Less, of course, is the opposite of more (which is another command I’ll discuss in a moment). The reason I choose less over more is for one reason and one reason only. Unlike the more command, the less command doesn’t have to read the entire input file before displaying the output. Because of that, you can actually page through the file. This is very handy if the file you’re viewing is of the longer sort. Once the file is open, you can scroll through it a page at a time by hitting the space bar or one line at a time using the Up/Down arrow keys. There are some options available, but you most likely won’t need them. 

Also: 5 Linux terminal apps that are better than your default

To view the contents of a file with less, the command looks like this:

Once you’re done viewing the contents of the file, hit either Q or Ctrl+c on your keyboard to escape.

2. more

The more command is very much like the less; it only displays the entire content of the file at once. There’s no paging through the content, so you might have to enlarge your terminal window to view the entire contents of the file. The more command is fairly primitive, and once it prints out the content of the file, it automatically returns your prompt (so there’s no need to escape). I might use more over less if I’m viewing a small file and want to simply have the contents spit out and get my terminal back immediately. In that regard, more is a bit more efficient than less.

Using more is as simple as:

3. cat

The cat command is similar to more in that it prints the contents of a file to the terminal and returns your prompt. However, the cat command can do something more cannot. Let’s say you want to view the content of two files, one after the other, at the same time. The cat command can do that. In fact, you can even pipe the output of such a command into a new file that holds the content of both. For example, you have zdnet.txt and zdnet2.text and want to combine them into the file zdnet3.txt. To do that, you would issue the command:

cat zdnet1.txt zdnet2.txt > zdnet3.txt

Use less to view the content of zdnet3.txt and you’ll see that it contains the content from both original files.

4. nl

What if you want to have line numbers printed along the side of the file to can see the position of each line of text? This function can be convenient when viewing/debugging code. You can even use nl to generate ordered lists, a feature I use regularly. Before I get into creating the ordered list file, using nl is as simple as:

This will print out each line with a line number at the left edge. 

Let’s say you have a file of steps for a task, or you’ve created a list of names, and you want to turn them into an ordered list. For that, we’ll pipe the content into a new file like so:

nl zdnet.txt > zdnet_numbers.txt

It’s that simple.

Also: The best Linux laptops you can buy: Expert tested

5. grep

The grep command is unique in that it allows you to search for a string in a file and view only the portions of the file that contain the string in question. For example, I have zdnet.txt and want to see where Linux is referenced. For that, I would issue the command:

The output would display all passages that contain Linux and highlight the search term in red. You can also search for a longer string but you must put it in quotes, like so:

grep "I started using Linux" zdnet.txt

You can also print the line numbers (so you can more easily locate the string in question when editing the file), like so:

With the above five commands, you shouldn’t have any problem viewing the content of text files. But one thing to keep in mind is that these commands won’t work with documents created by the likes of Word or LibreOffice, because those are binary files. These commands only work with flat text files. For more information on each command, make sure to read the man page for each (such as man less).





Source link