Exercise 0.5: Viewing and Removing
Managing File Content and Cleanup
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
catortype. - Removed a file using
rmordel. - 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
Navigate to your
cli-practicedirectory.Create a new text file with some content. We can do this with
echoand the>redirection operator.echo "This is the first line." > my-file.txtNow, use the appropriate command for your OS to view its contents.
Use the cat command.
cat my-file.txtUse the type command.
type my-file.txtYou 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
lsUse the del (delete) command.
del my-file.txt
lsAfter 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.
Create a new, empty directory.
mkdir empty-folderNow, remove it.
# This command works on all operating systems rmdir empty-folder ls
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-rstands for “recursive”). - Windows:
rm -r <directory_name>(in PowerShell) orrmdir /s <directory_name>(in CMD).
Be extremely careful with these commands.
Reflect and Review
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?