Exercise 0.5: Viewing and Removing

Managing File Content and Cleanup

Author

Chuck Nelson

Published

October 28, 2025

Purpose

In this exercise, you will learn two final, essential skills for file management: viewing the contents of a file directly in your terminal and, just as importantly, safely removing files and directories that you no longer need. These are powerful commands that complete your basic toolset.

What You’ll Accomplish

By the end of this exercise, you will have successfully:

  • Displayed the contents of a text file using cat or type.
  • Removed a file using rm or del.
  • Removed an empty directory using rmdir.

Part 1: Viewing File Contents

Sometimes you need to quickly see what’s inside a file without opening a full text editor. The cat (short for “concatenate”) command on macOS/Linux and the type command on Windows are perfect for this.

Your Task 1: Create and View a File

  1. Navigate to your cli-practice directory.

  2. Create a new text file with some content. We can do this with echo and the > redirection operator.

    echo "This is the first line." > my-file.txt
  3. Now, use the appropriate command for your OS to view its contents.

Use the cat command.

cat my-file.txt

Use the type command.

type my-file.txt

You should see This is the first line. printed to your terminal.

Part 2: Removing Files and Directories

Cleaning up your workspace is just as important as creating it.

Warning: Deletion is Permanent!

The command line does not have a Recycle Bin or Trash Can. When you delete a file with rm or del, it is gone forever. Always double-check which file you are deleting before you press Enter.

Your Task 2: Remove a File

Let’s remove the file you just created.

Use the rm (remove) command.

rm my-file.txt
ls

Use the del (delete) command.

del my-file.txt
ls

After running ls or dir, you will see that my-file.txt is gone.

Your Task 3: Remove a Directory

The command to remove a directory, rmdir (remove directory), only works if the directory is empty.

  1. Create a new, empty directory.

    mkdir empty-folder
  2. Now, remove it.

    # This command works on all operating systems
    rmdir empty-folder
    ls
TipHow do I delete a directory that isn’t empty?

To delete a directory and all of its contents, you must use a more powerful, and therefore more dangerous, command.

  • macOS/Linux: rm -r <directory_name> (The -r stands for “recursive”).
  • Windows: rm -r <directory_name> (in PowerShell) or rmdir /s <directory_name> (in CMD).

Be extremely careful with these commands.

Reflect and Review

ImportantReflection

In your Microsoft Teams Student Notebook, answer the following:

  • What is the command on your OS to view the contents of a file named config.yml?
  • What is the command to delete a file named old-data.csv?
  • Why is it important to be extra careful when using commands that delete files?
Back to top