Linux Lab – Redirect Operators

This lab is designed to demonstrate the > and >> redirect operators. Actually typing in the commands and seeing the results will engage more of your senses and give you a better chance of fully understanding these operators. Understanding these will give you a better chance of passing the Linux+exam. When done, you should be able to answer the following two questions:

  • What does the > operator do when used within a command?
  • What does the >> operator do when used within a command?

Prerequisites

Launch a terminal within a Linux operating system. This lab was performed on Linux Mint 19.1 Cinnamon. If you are performing the lab on a different distro, your results may be a little different.

Using the > and >> operators

1. Enter the following command to send four pings to the loopback address and display the output:

ping localhost -c 4

2. Enter the following command to send four pings to the loopback address, create the ping.txt file if it doesn’t exist, and send the output to the file:

ping localhost -c 4 > ping.txt

Note that you can use the keyboard up arrow to retrieve previous commands. If you press it until you see this command

ping localhost -c 4

you can then add the rest of the text at the end of command.

ping localhost -c 4 > ping.txt

3. When the ping completes, enter the following command to open the ping.txt file with the nano text reader:

nano ping.txt

Notice that the text file includes about nine lines that are the same as the output sent to the terminal in step 2.

4. Press CTRL+X to exit nano.

5. Enter the following command to display some text:

echo “okey-dokey”

6. Enter the following command to append the ping.txt file with some text:

echo “okey-dokey” >> ping.txt

7. Enter the following command to open the ping.txt file with the nano text reader:

nano ping.txt

Notice that your text “okey-dokey” is appended to the bottom of the file.

8. Press CTRL+X to exit nano.

9. Enter the following command to send two pings to the loopback address, and overwrite the ping.txt file with the output:

ping localhost -c 2 > ping.txt

10. Enter the following command to open the ping.txt file with the nano text reader:

nano ping.txt

Notice that previous text has been overwritten with the two pings created in step 9.

Problems?

While doing this, I opened the ping.txt file at one point before the ping was finished. When opening it with nano, it reported “File ping.txt is being edited….).

If you see this, enter the following command to show all the files (including the hidden files).

ls -a

Hidden files start with a dot (.) and you will see these two files:

.ping.txt.swp

.ping.txt.swo

Enter the following two commands to remove these files:

rm .ping.txt.swp

rm .ping.txt.swo

You can now open the file with this command:

nano ping.txt

Answers 

At this point, you should know the answers to these two questions.

  • What does the > operator do when used within a command?
  • What does the >> operator do when used within a command?

The > operator overwrites all data in a file.

The >> operator appends a file with additional data.

Check out the Linux Study Package here.