File Contents and Comparing

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to this hands-on lab about working with files in Linux! If you're new to Linux, don't worry. We'll guide you through each step carefully. In this lab, you'll learn how to view the contents of files, look at specific parts of files, and compare files. These are fundamental skills that will help you navigate and understand the Linux file system.

Objectives

By the end of this lab, you will be able to:

  • Use cat to display the entire contents of a file
  • Use head to view the beginning of a file
  • Use tail to view the end of a file
  • Use diff to compare the contents of files and directories

Prerequisites

  • A Linux system (this lab uses Ubuntu, but most Linux distributions will work similarly)
  • Basic familiarity with using a computer

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

Print File Contents

First, please open a terminal on the Desktop OR switch to the terminal tab in the lab environment.

alt text

Once you have a terminal open, you should see a command prompt, typically ending with a $ symbol. This is where we'll enter our commands.

Now, let's use the cat command to display the contents of a file:

  1. In the terminal, type the following command and press Enter:
cat /tmp/hello

Here, /tmp/hello is the path to the file we want to view. /tmp is a directory (folder) on your system, and hello is the name of the file in that directory.

  1. After pressing Enter, you should see the contents of the file:
Hi,
I am Labby!

This is everything that's in the /tmp/hello file. The cat command has displayed it for us in the terminal.

Display File Contents with Line Numbers

Now, let's use the cat command again, but this time we'll add line numbers to the output.

  1. In the terminal, type the following command and press Enter:
cat -n /tmp/hello

The -n here is called an option or a flag. It tells cat to number all output lines.

  1. You should now see the contents of the file with line numbers:
     1 Hi,
     2 I am Labby!

This can be very helpful when you're dealing with longer files and need to refer to specific lines.

Print the Top Lines of a File

Next, we'll use the head command. As its name suggests, head is used to view the beginning or "head" of a file.

  1. In the terminal, type the following command and press Enter:
head -n1 /tmp/hello

Here, -n1 is an option that tells head to show only the first line. The 1 can be changed to any number to show that many lines.

  1. You should see this output:
Hi,

This is just the first line of the file. By default, without the -n1 option, head would show the first 10 lines of the file.

View the First Few Bytes of a File

Now we'll use the head command again, but this time to view a specific number of bytes from the beginning of a file.

  1. In the terminal, type the following command and press Enter:
head -c1 /tmp/hello

The -c1 option tells head to show only the first byte (character) of the file. Like with -n, you can change the 1 to any number to see that many bytes.

  1. You should see this output:
H

This is just the first character of the file. In text files, each character is typically one byte.

Print the Last Lines of a File

Now let's move on to the tail command. As you might guess, tail is the opposite of head - it shows the end of a file.

  1. In the terminal, type the following command and press Enter:
tail -n1 /tmp/hello

Just like with head, the -n1 option tells tail to show only one line, in this case the last line of the file.

  1. You should see this output:
I am Labby!

This is the last line of our file. Without the -n1 option, tail would show the last 10 lines by default.

View the Last Few Bytes of a File

Similar to what we did with head, we can use tail to display a specific number of bytes from the end of a file.

  1. First, let's try viewing the last byte. In the terminal, type the following command and press Enter:
tail -c1 /tmp/hello

You might not see any output. This is because the last character is likely a newline character, which is invisible.

  1. Let's try viewing the last two bytes instead. Type the following command and press Enter:
tail -c2 /tmp/hello
  1. Now you should see:
!

The -c2 option tells tail to show the last 2 bytes (characters) of the file. In this case, it's showing the exclamation mark, which is the last visible character in our file.

Comparing Files

Now we'll learn how to use the diff command to compare two files and see the differences between them.

  1. First, let's make sure we're in the right directory (folder). Type the following command and press Enter:
cd ~/project

This changes our current directory to the "project" folder in our home directory. The ~ symbol is a shortcut for your home directory.

  1. Now, let's compare two files. Type the following command and press Enter:
diff file1 file2

This tells diff to compare the contents of file1 and file2.

  1. You should see output similar to this:
1c1
< this is file1
---
> this is file2

This output is telling us that line 1 of file1 is different from line 1 of file2. The < symbol shows the content from file1, and the > symbol shows the content from file2.

Comparing Directories

Finally, let's use the diff command to compare entire directories.

  1. In the terminal, type the following command and press Enter:
diff -r ~/Desktop ~/Code

The -r option tells diff to recursively compare subdirectories as well. ~/Desktop and ~/Code are the paths to the two directories we're comparing.

  1. You might see output similar to this:
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

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

Summary

Congratulations! You've completed the File Contents and Comparing lab. Let's recap what you've learned:

  1. You used cat to view the entire contents of a file.
  2. You learned how to use cat -n to view file contents with line numbers.
  3. You used head to view the beginning of a file, both by lines and by bytes.
  4. You used tail to view the end of a file, both by lines and by bytes.
  5. You learned how to use diff to compare the contents of files.
  6. Finally, you used diff -r to compare entire directories.

These commands are fundamental tools in Linux. As you continue to work with Linux, you'll find yourself using these commands frequently to inspect and compare files and directories.

Remember, if you ever forget how to use a command, you can always type man followed by the command name (e.g., man cat) to see the manual page for that command. This will give you detailed information about all the options available for each command.

Keep practicing with these commands on different files and directories to become more comfortable with them. The more you use them, the more natural they'll become!

Other Linux Tutorials you may like