Cadastre-se agora para um orçamento mais personalizado!

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

Aug, 02, 2024 Hi-network.com
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

Thelesscommand is my go-to, and I've been using it since I started using Linux. Less, of course, is the opposite ofmore(which is another command I'll discuss in a moment). Thereason I chooselessovermoreis for one reason and one reason only. Unlike themorecommand, thelesscommand 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 withless, the command looks like this:

lesszdnet.txt

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

2. more

Themorecommand is very much like theless; 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. Themorecommand 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 usemoreoverlessif I'm viewing a small file and want to simply have the contents spit out and get my terminal back immediately. In that regard,moreis a bit moreefficient thanless.

Using more is as simple as:

more zdnet.txt

3. cat

Thecatcommand is similar tomorein that it prints the contents of a file to the terminal and returns your prompt. However, thecatcommand can do somethingmore cannot. Let's say you want to view the content of two files, one after the other, at the same time. Thecatcommand 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

Useless 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 usenlto generate ordered lists, a feature I use regularly. Before I get into creating the ordered list file, usingnlis as simple as:

nl zdnet.txt

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 grepcommand 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:

grep Linux zdnet.txt

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:

grep -n Linux zdnet.txt

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 asman less).

Open Source

5 Linux commands you must know to keep your device running smoothlyThe best Linux laptops you can buy: Expert testedThe best Linux distros for beginnersMy 5 favorite Linux text editors (and why you should be using one)
  • 5 Linux commands you must know to keep your device running smoothly
  • The best Linux laptops you can buy: Expert tested
  • The best Linux distros for beginners
  • My 5 favorite Linux text editors (and why you should be using one)

tag-icon Tags quentes :

Copyright © 2014-2024 Hi-Network.com | HAILIAN TECHNOLOGY CO., LIMITED | All Rights Reserved.