File Contents and Comparing

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn to use some file operation command that allows you to perform actions such as viewing the contents of the file, and operating or filtering the contents of the file.

Achievements

  • cat - print the contents of the file.
  • head - print the top contents of the file.
  • tail - print the last contents of the file.
  • diff - compare the contents of the file.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/head("`File Beginning Display`") linux/BasicFileOperationsGroup -.-> linux/tail("`File End Display`") linux/VersionControlandTextEditorsGroup -.-> linux/diff("`File Comparing`") subgraph Lab Skills linux/cat -.-> lab-270251{{"`File Contents and Comparing`"}} linux/head -.-> lab-270251{{"`File Contents and Comparing`"}} linux/tail -.-> lab-270251{{"`File Contents and Comparing`"}} linux/diff -.-> lab-270251{{"`File Contents and Comparing`"}} end

Get File Contents

cat is a command that gets the file contents. It is useful when you want to preview the file contents.

Just run the following command:

cat /tmp/hello

Now you can see the contents of the test file.

Hi,
I am Labby!

Get Contents with the Line Number

Sometimes, We need to see the contents of a file with the line number. You can also use the cat command to display the contents of a file with the line number.

The cat command has an option -n to display the line number of the file. Just run the following command:

cat -n /tmp/hello

Now you can see the contents of the file with the line number.

     1 Hi,
     2 I am Labby!

Print the Top Lines

the head is a command that allows you to view files top contents, by default it print the top 10 lines of the files.

When you want to view the n top lines of the file, you can use the -n option to specify the number of lines to display.

The following example shows how to view the first row of data under the /tmp/hello.

head -n1 /tmp/hello

Output:

Hi,

Get First n Bytes

Except for viewing the top lines of a file, the head command can also be used to view the first n bytes of a file. The -c option is used to specify the number of bytes to display.

The following example shows how to view the first byte of data under /tmp/hello.

head -c1 /tmp/hello

Output:

H

Print the Last Lines

the tail is a command that allows you to view files last contents, by default it print the last 10 lines of the files. As same as the head command, you can use the -n option to specify the number of lines to display.

The following example shows how to view the first row of data under the /tmp/hello.

tail -n1 /tmp/hello

Output:

I am Labby!

Get Last n Bytes

The following example shows how to view the last byte of data under /tmp/hello.

tail -c1 /tmp/hello

You may be wondering why there is no output from the terminal.

This is because each line of characters is followed by a newline character by default, which is not visible.

Just need change the number:

tail -c2 /tmp/hello

Output:

!

Comparing Files

The diff command in Linux is used to compare two files and display the differences between them.

The basic syntax for using diff is as follows.

Make sure you are in the ~/project directory and run the following command:

diff file1 file2

This will compare file1 and file2 and display any differences between them.

1c1
< this is file1
---
> this is file2

The output above shows that the first line of file1 is different from the first line of file2.

Comparing Directories

You can also use diff to compare entire directories by using the -r option, like this:

diff -r ~/Desktop ~/Code

This will compare the contents of Desktop and Code and display any differences.

Only in /home/labex/Desktop: code.desktop
Only in /home/labex/Desktop: gedit.desktop
Only in /home/labex/Desktop: gvim.desktop
Only in /home/labex/Desktop: xfce4-terminal.desktop

The output above shows that the Desktop directory contains four files that are not in the Code directory.

Summary

Congratulations! You have completed the file operations lab.

In this lab, you learned three basic operations commands ls, cat, tail, head and diff.
These commands are used frequently, so be sure to keep them in mind.

Keep exploring!

Other Linux Tutorials you may like