Linux File Handling Commands

LinuxLinuxBeginner
Practice Now

Introduction

In this challenge, you will learn how to use the cat, head, and tail commands in Linux. These commands are used to display the contents of a file, view the beginning or end of a file, and concatenate files.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/head("`File Beginning Display`") linux/BasicFileOperationsGroup -.-> linux/tail("`File End Display`") subgraph Lab Skills linux/cat -.-> lab-7960{{"`Linux File Handling Commands`"}} linux/head -.-> lab-7960{{"`Linux File Handling Commands`"}} linux/tail -.-> lab-7960{{"`Linux File Handling Commands`"}} end

Using Cat Command

The cat command is used to display the contents of a file. In this step, you will learn how to use the cat command to display the contents of example.txt.

Target

Display the contents of example.txt and store the output in a file named step1.txt.

Result Example

This is an example file.
It contains some text for the Linux challenge.
This is line 3.
This is line 4.
This is line 5.

Requirement

  • You need to know how to use the cat command.
  • You need to know about the stream operators > in Linux.

Using Head Command

The head command is used to display the beginning of a file. In this step, you will learn how to use the head command to display the first 2 lines of example.txt.

Target

Display the first 2 lines of example.txt and store the output in a file named step2.txt.

Result Example

This is an example file.
It contains some text for the Linux challenge.

Requirement

  • You need to know how to use the head command.
  • You need to know about the stream operators > in Linux.

Using Tail Command

The tail command is used to display the end of a file. In this step, you will learn how to use the tail command to display the last line of example.txt.

Target

Display the last line of example.txt and store the output in a file named step3.txt.

Result Example

This is line 5.

Requirement

  • You need to know how to use the head command.
  • You need to know about the stream operators > in Linux.

Using Cat and Head Commands

The cat and head commands can be used together to display a range of lines from a file. In this step, you will learn how to use the cat and head commands to display the lines 3 to 4 of example.txt.

Target

Display the lines 3 to 4 of example.txt and store the output in a file named step4.txt.

Result Example

This is line 3.
This is line 4.

Requirement

  • You need to know how to use it in conjunction with the cat command and the head command.
  • You need to know the Linux pipe operators |.
  • You need to know about the stream operators > in Linux.

Using Cat and Tail commands

The cat and tail commands can be used together to display the end of a file. In this step, you will learn how to use the cat and tail commands to display the last 3 lines of example.txt.

Target

Display the last 3 lines of example.txt and store the output in a file named step5.txt.

Result Example

This is line 3.
This is line 4.
This is line 5.

Requirement

  • You need to know how to use it in conjunction with the cat command and the tail command.
  • You need to know the Linux pipe operators |.
  • You need to know about the stream operators > in Linux.

Summary

Congratulations! You have completed the challenge of File Viewing. You should now have a better understanding of how to use the cat, head, and tail commands in Linux. You can use these commands to quickly view and manipulate the contents of files in your Linux environment.

Other Linux Tutorials you may like