How to remove files and directories from the command line interface.

In this guide we will learn how to remove files and directories with the rm command. This command should be available on any UNIX-like operating systems, such Linux, BSD, and macOS.

The rm permanently deletes files. There is no way to undo this command! It is very powerful. Be very careful!

Command Structure

The rm command structure looks like this rm [OPTION]... [FILE]...

Remove a File

The most basic usage of rm is to remove a single file. rm voltron.txt

Remove a Directory

By default rm will not remove directories. rm decepticons. Will result in an error rm: cannot remove 'decepticons': Is a directory

To remove a directory we need to use the option -r or --recursive. The correct command to remove this directory is rm -r decepticons. This will delete the decepticons directory and all its subdirectories.

Removing Files and Directories Interactively

We remove files interactively with the -i or --interactive option. rm megatron.txtWill ask us for confirmation before deleting the file.

Seeing What is Being Deleted

The rm command silently deletes files and directories unless we use the -v or --verboseoption. When we use this option rm tells us about each file and directory it deletes.

rm Command Tutorial

Deleting Protected Files

If we try to delete a file that write protect rm will ask of for confirmation.

We can skip this confirmation with the -f or --force option.

Review

We have now learned how to use the rm command. Remember to be very careful. This command can every easily delete any and all data on your system.


I hope you found this helpful. If you want more, I have a channel full of Python and Linux video tutorials on Youtube.

Jason Rigden

Written by

You may remember me from such projects as The Seattle Podcasters Guild, The Talking Cryptocurrency Podcast, or some of my popular Python tutorials.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade