#4 Deleting files

beginner

Introduction

In this episode we will be continuing on the from episode 3 where we covered moving files. We now take a look at the last in the series, deleting files. Good news again which is if you have understood the prior episodes on copying and moving files you should find deleting files a breeze.

Outcomes

Deleting a single file

To delete files or directories you will need the rm command, short for "remove". This command unlike the previous commands: cp and mv only takes 1 argument. This argument is path to file or directory we want to delete.

Example of deleting a file named "file-v1.txt" in the present working directory

rm file-v1.txt

Example of deleting the file "my-file.txt" in a child directory

rm ./child/my-file.txt

Example of deleting the file "my-file.txt" in the parent directory

rm ../my-file.txt

Deleting multiple files with a regular expression

Deleting multiple files is very similar to deleting one file. The same command rm is used, however you can provide a regular expression that matches a certain pattern. For example you may want to delete all the CSV files in the present working directory. A regular expression is a means to express a pattern that if expanded would list potentially many paths. However for brevity this will not be covered in this episode.

Example of deleting all CSV files in the present directory

rm *.csv

Deleting multiple files with a list

Another way of deleting multiple files without the need for regular expressions is to simply provide a list of all paths that you want deleting.

Example of deleting the listed files "1.csv" and "2.csv" in the present directory

rm 1.csv 2.csv

Deleting a directory and its contents

You may also want to delete a directory and all the containing files and directories within it. Just like the cp command you must also pass the recursive flag -r to acheive this. This is because the command needs to know you also wish to delete everything contained within the specified path.

Directory structure before our remove command

.
├── overview.txt
└── project1
    ├── assets
    │   └── image.png
    └── file1.txt

Example command to remove project1 directory

rm project1

Directory structure after our remove command

.
└── overview.txt

Conclusion

If you also worked through the previous 3 episodes, you should now be able to navigate the file system, create, copy, move and delete files. These are most of actions one would normally perform in a GUI file explorer.

Using the command line gives you the ability to do things that would have been previously very time consuming. For example being able to move files that end in *.jpg to another directory. This would have been a tedious task in the directory also contain lots of other file types in a file explorer. But can be acheived with the command line in several seconds.

Graphical file explorers are still useful and your newly acquired knowledge should not replace them but give you another tool you can use. Allowing you to pick the better tool for the job in hand.