Linux File Removing

LinuxLinuxBeginner
Practice Now

Introduction

In the vastness of the Arabian Desert, there lived an infamous sandstorm predictor known as Zara. Zara's predictions were never wrong, and over time, as her hut became cluttered with old sandstorm charts, she realized that she needed to clear out the old to make way for the new, just like the desert sands. In this lab, you'll take on the role of Zara's apprentice, learning the art of removing files in a Linux environment to help her hut stay organized and prepared for future predictions. Your goal will be to understand and use the rm command with precision, ensuring only the necessary files are removed while preserving important data.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") subgraph Lab Skills linux/rm -.-> lab-271371{{"`Linux File Removing`"}} end

Understanding the Basics of rm

In this step, you'll be introduced to the rm command, which is used for removing files and directories within a Linux environment.

As Zara's apprentice, your first task is to clear out old prediction files that are no longer needed.

Now, you need to remove the single file prediction-01.txt from directory old_predictions and verify it's no longer there:

rm ~/project/old_predictions/prediction-01.txt

To check if prediction-01.txt is successfully removed, list the contents of the directory:

ls ~/project/old_predictions

You should not see prediction-01.txt in the output.

Removing Multiple Files

The next lesson includes removing more than one file at a time. Clear out additional old prediction files by using wildcard characters.

In the old_predictions folder, remove all files with the suffix -02.txt, -03.txt, and -04.txt:

rm ~/project/old_predictions/*-{02..04}.txt

After executing this, use ls again to make sure that correct files are removed.

ls ~/project/old_predictions

You should only see prediction-05.txt left in the output.

Deleting Directories with rm

It's time to learn how to remove directories that are not needed. However, rm needs to be used with the -r (recursive) option to remove a directory and its contents.

Now, you need to remove the entire 2009 directory from directory archive:

rm -r ~/project/archive/2009

Check the archive directory to confirm the removal:

ls ~/project/archive

The output should only list the 2008 and 2010 directories.

Summary

In this lab, you have stepped into the shoes of Zara's apprentice, working in a desert scene, and helped with the critical task of file and directory management in Linux using the rm command. Through hands-on examples, we've explored how to delete single files, multiple files using wildcards, and entire directories using the recursive option.

This lab has been designed to be beginner-friendly, gradually building your command-line confidence in handling files. I hope that you have come to appreciate the simplicity and power of Linux commands, and I am confident your journey with Linux will lead to further command mastery.

Other Linux Tutorials you may like