Ugacomp

How to display the contents of a file in Linux

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

Whether you are inspecting configuration files, reading logs, or examining scripts, the ability to view file contents efficiently is crucial. This article will guide you through the essential commands and techniques to display the contents of a file in Linux.

Using the Cat Command

The cat command in Linux is a versatile utility that goes beyond its name, which stands for “concatenate.” While its primary purpose is to concatenate and display the content of one or more files, it can be employed in various ways to manipulate and analyze text data.

Displaying File Contents

The most basic use of cat is to display the contents of a file. Simply provide the filename as an argument:

cat filename

This command will output the entire content of the specified file onto the terminal.

Concatenating Multiple Files

As its name suggests, cat can concatenate multiple files into a single output. This can be useful when you want to combine the content of several files:

cat file1 file2 > combined_file

This command concatenates the contents of file1 and file2 and writes the result to combined_file.

Appending to a File

You can use cat to append the content of one file to another. This is done using the append (>>) operator:

cat file1 >> file2

This appends the content of file1 to the end of file2.

Numbering Lines

cat can be combined with other commands to number lines in a file. The nl command, for instance, can be used in conjunction with cat:

cat -n filename

This will display the contents of the file with line numbers.

Displaying Non-Printable Characters

To reveal non-printable characters, such as tabs and line breaks, you can use the -v option with cat:

cat -v filename

This is helpful when troubleshooting and inspecting files with special characters.

Displaying Line Endings

To display line endings explicitly (whether Unix or DOS style), you can use the -e option:

cat -e filename

This is useful when dealing with files that might have been created on different systems.

Creating a New File from Standard Input

You can create a new file by using cat and redirecting standard input:

cat > newfile

Type the content you want for the new file, and press Ctrl + D to save and exit.

Using the Head and Tail Commands

The head and tail commands in Linux provide efficient ways to preview the content of a file by displaying its beginning or end, respectively. Whether you are dealing with large log files or lengthy documents, these commands offer quick insights without overwhelming your terminal.

Head Command: Viewing the Beginning of a File

The head command is used to display the first few lines of a file. Here’s a basic example:

head filename

This command will output the first 10 lines of the specified file by default. You can customize the number of lines displayed using the -n option:

head -n 15 filename

This command will display the first 15 lines of the file. Adjust the number as needed.

Tail Command: Viewing the End of a File

Conversely, the tail command shows the last few lines of a file. To display the last 10 lines:

tail filename

Similarly, you can use the -n option with tail to specify the number of lines to be displayed:

tail -n 20 filename

This command will show the last 20 lines of the file. Adjust the number according to your requirements.

Following Changes in Real-Time with Tail

The tail command becomes even more powerful when used with the -f option. This allows you to follow the changes in a file in real-time, which is especially useful for monitoring log files:

tail -f filename

As new lines are added to the file, they will be continuously displayed on your terminal. Press Ctrl + C to exit.

Combining Head and Tail for Context

To get a snippet from the middle of a file, you can combine head and tail. For instance, to display lines 20 to 30:

head -n 30 filename | tail -n 11

This command uses head to get the first 30 lines and then pipes that output to tail to retrieve the last 11 lines from the result.

Using the Less Command

Unlike simple commands like cat, less enables scrolling, searching, and navigating through files efficiently.

Basic Usage

To use less to display the content of a file, simply provide the filename as an argument:

less filename

This command opens the file in the less interface, where you can navigate using the arrow keys.

Scrolling Through Content

Once inside the less interface, you can scroll through the file using the arrow keys. Press Space to move forward one screen, and press B to move backward one screen.

Searching for Text

Searching within a file is a powerful feature of less. To search, press / followed by the text you want to find. For example:

/ search_text

To find the next occurrence, press n, and to go to the previous occurrence, press N.

Exiting less

To exit the less interface, simply press q. If you’ve made changes to the file, less will prompt you to save them before exiting.

Display Line Numbers

If you want to display line numbers along with the file content, you can use the -N option:

less -N filename

This is particularly useful when you need to reference specific line numbers.

Viewing Multiple Files

You can view multiple files sequentially within the less interface by providing them as arguments:

less file1 file2

Use :n to move to the next file and :p to go back to the previous one.

Jumping to Specific Line Numbers

If you need to jump directly to a specific line number, use the + followed by the line number:

less +42 filename

This command opens the file and jumps directly to line 42.

Using the More Command

The more command in Linux is another pager utility that allows users to view the content of files in a page-by-page manner. While less provides advanced features, more offers a straightforward way to display and navigate through file content.

Basic Usage

To use more to display the content of a file, simply provide the filename as an argument:

more filename

This command opens the file in the more interface, displaying one screen of content at a time.

Navigating Through Content

Once inside the more interface, you can navigate through the file using the following keys:

  • Press Space to move forward one page.
  • Press Enter to move forward one line.
  • Press Q to exit more immediately.

Searching for Text

Unlike less, more does not provide an interactive search feature. However, you can still search for text by piping the output through other commands. For example:

grep "search_text" filename | more

This command uses grep to find lines containing “search_text” and then pipes the output to more for better readability.

Exiting more

To exit the more interface, press Q. Similar to less, if you’ve made changes to the file, more will prompt you to save them before exiting.

Viewing Multiple Files

You can view multiple files sequentially within the more interface by providing them as arguments:

more file1 file2

Use the Space bar to move to the next file and Q to exit.

Displaying Line Numbers

To display line numbers along with the file content, you can use the -n option:

more -n filename

This is helpful when you need to reference specific line numbers.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.