5 Linux Commands You Can Use to Quickly View File Contents


Raimund Linke/Getty Images

One of the best things about Linux is that it offers multiple ways to handle 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 stress because you can select one method and stick to it.

Let’s take visualizing the content of text files as an example. For decades, I’ve used one method, although I know there are other ways to do it. But my brain always falls back on what’s already ingrained.

Also: 5 Reasons Why Linux Will Overtake Windows and MacOS on the Desktop, Eventually

This is the task I want to talk about today… viewing the contents of text files is a function I perform quite frequently. From code and notes to configuration files (and everything in between), I have to look at such files on a regular basis.

But what commands are available for this?

Let me show you five.

1. less

He less The command is my preferred option and I have been using it since I started using Linux. Less, of course, is the opposite of further (which is another command I’ll look at in a moment). The reason I chose less on further is for one reason and one reason only. Unlike the further Command, the less The command does not have to read the entire input file before displaying the output. Because of this, you can scroll through the pages of the file. This is very useful if the file you are viewing is of a longer type. Once the file is open, you can scroll through it one page at a time by pressing the space bar or one line at a time by using the Up/Down arrow keys. There are a few options available, but you most likely won’t need them.

Also: 5 Linux Terminal Apps That Are Better Than The Default Ones

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

Once you are done viewing the contents of the file, press Q or Ctrl+c on your keyboard to escape.

2 more

He further The command is very similar to less; just displays all the contents of the file at once. There is no pagination through the contents, so you may need to widen your terminal window to see all the contents of the file. further The command is quite primitive, and once it prints the contents of the file, it automatically returns the prompt (so no escaping is required). You could use further on less If I’m viewing a small file and I want to just view the content and get my terminal back immediately. In that sense, further It is a little more efficient than less.

Using more is as simple as:

3. cat

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

cat zdnet1.txt zdnet2.txt > zdnet3.txt

Wear less to view the contents of zdnet3.txt and you will see that it contains the contents of both original files.

4. in

What if you want line numbers to be printed throughout the file so you can see the position of each line of text? This feature can be useful when viewing or debugging code. You can even use in to generate ordered lists, a function I use regularly. Before I start creating the ordered list file, I use in It’s as simple as:

This will print each line with a line number on 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 convert them into an ordered list. To do this, we’ll output the contents to a new file like this:

nl zdnet.txt > zdnet_numbers.txt

It’s that easy.

Also: The best Linux laptops you can buy: tested by experts

5. grep

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

The result will show all passages containing Linux and highlight the search term in red. You can also search for a longer string, but you must put it in quotes, like this:

grep "I started using Linux" zdnet.txt

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

With the five commands above, you should have no problem viewing the contents of text files. But something to keep in mind is that these commands will not work with documents created with programs like Word or LibreOffice, because they are binary files. These commands only work with plain text files. For more information on each command, be sure to read the manual page for each one (such as man less).





Source link