Video summary
The Linux `rm` command is a powerful tool for deleting files, but it requires extreme caution because once a file is removed, it is effectively gone forever. While technical methods to recover deleted data exist, they are often complex and unreliable, so users should always assume deletion is permanent and maintain regular backups of their original files. The video demonstrates this safety practice by showing a hidden `.backup` directory that stores copies of essential files, allowing the user to easily restore the system to its previous state after accidental deletions or testing.
When removing files, users can encounter error messages if they mistype a filename, such as writing "credit.text" instead of "credit.txt". To handle this gracefully, one can use the `-f` or `--force` option to ignore errors and delete non-existent files without interruption. Conversely, if a user prefers not to have files deleted silently, the `-i` or `--interactive` flag forces the shell to prompt for confirmation before removing each file, providing an extra layer of protection against accidental data loss.
Removing directories presents additional challenges because the standard `rm` command cannot delete folders by default; instead, it requires the `-r` or `--recursive` option to remove a directory along with all its contents. The video also highlights a critical pitfall involving wildcard patterns and spaces, where an accidental space between the asterisk and a file extension can cause the shell to interpret the command as deleting every file matching the pattern plus a literal file named `.gif`. This mistake could lead to catastrophic data loss if combined with recursive deletion and error suppression flags.
To prevent such disasters, the recommended best practice is to always run `ls` before executing `rm` with wildcards to verify exactly which files will be affected. By listing the target files first, users can visually confirm that only the intended items are selected and correct any accidental spaces in their commands. This simple verification step ensures that the powerful destructive capabilities of the `rm` command are used safely and accurately, preserving important data while allowing for efficient file management on Linux systems.
Read the full video transcript
Here's what we'll cover in this video
about the RM remove command.
We'll talk about removing files,
ignoring error messages,
using the interactive option,
removing directories,
and being careful with RM.
Here's the simplest use, removing one or
more files.
Before we go any further, you need to
know one important thing.
Once you remove a file on Linux, it is
gone.
Okay, technically there are ways to
recover deleted files,
but it's non-trivial and not always
reliable.
You should presume that when a file is
deleted, it's gone forever.
So, be careful, and we'll talk about
that more later.
That's why it's always good to have a
backup of your original files.
In fact, if I do ls -a to show all my
files, including hidden files and
directories,
you'll see that I have a hidden
directory called .backup that contains a
copy of all my original files,
so that I can restore this directory to
its original state.
At the left, you can see our file
structure,
or we can use the tree command with dot,
meaning our current directory,
to see the files in the directory.
Let's remove those first three text
files. We'll remove cash.txt,
credit.txt,
and debit.txt.
And when I press enter,
just like that, they're gone,
as you can see from the ls command,
now let me use the backup directory to
reset everything the way it was.
And we're back.
What happens if I type one of the names
wrong?
This time I'm going to say credit.text,
not txt,
and debit.txt.
The two files that I typed correctly go
away.
The one I typed incorrectly gives me an
error message.
Cannot remove credit.text, no such file
or directory.
Time for a reset.
If I want to remove files, but not get
error messages for the files that don't
exist, I can use the {dash} f short
option, or the long option
{dash}{dash}force,
to get that effect. Let's again remove
cash.txt,
credit.text, which doesn't exist, and
debit.txt.
And you'll see that credit.txt still
remains, but I did not get an error
message
for the incorrectly typed file name.
Time for a reset.
If I'm not comfortable with having the
shell silently remove files,
instead of using {dash} f for force, I
can say {dash} i, which is short for
{dash}{dash}interactive,
and then the shell will ask me if I want
to remove files. I can say Y for yes or
anything else for no.
In this case, I'm going to type all the
file names correctly. I want to get rid
of cash.txt,
credit.txt, and debit.txt.
And it'll ask me for each one whether I
want to remove it or not.
And I'll say yes, yes, and no.
And then only debit.txt remains.
And just for good measure, let's get rid
of it as well.
And now if I ls,
that's what I have at the top level of
my directory.
And again, tree. will show me
everything.
Let me clear out my screen,
and let's look a little bit more closely
at the humor
subdirectory.
And let's say I want to remove it.
Now, remember we have mkdir to make a
directory, and there is a command called
rmdir for removing empty directories.
But the humor directory is not empty, so
if I try to use rmdir, I'll get a
message that tells me
exactly that. The directory is not
empty.
I can't use rm either because without a
special option,
rm will not remove directories.
To remove a directory, you need to use
the -r
or the long form --recursive
option.
This option removes the directory and
all of its contents.
Let's remove the Venn diagrams
subfolder inside of the humor folder.
And when I do that,
it's gone.
And we'll see that here.
Let's clear the screen.
And then let's list what we have at the
moment.
You have to be very careful when
removing files using wild cards.
For example, if I want to remove all the
files that are ending in {dot}gif,
I might try something like this.
And watch what happens.
Oops.
What happened?
If you look carefully at the RM command,
you'll see that there's a space between
the star and the {dot}gif.
The shell interpreted that to mean
remove all files
and then remove a file called {dot}gif.
It couldn't remove humor because that's
a directory
and there was no such file as {dot}gif,
but it got rid of everything else.
Let's reset.
Now that was bad, but it could have been
worse. If I had said, let's remove
everything recursively
and ignore any error messages, and then
star, and then an accidental space, and
then {dot}gif,
that would have gotten rid of absolutely
everything in the directory.
Luckily, this does not get rid of hidden
directories, so I still have my backup
directory available to me.
And I can use that to restore
everything.
So, the question is how do I avoid
errors like this when I'm using wild
cards?
If you're not entirely sure that you've
typed a wildcard expression for RM
correctly, do what the book suggests.
Instead of saying RM, do LS.
Now, if I put the space in there
accidentally on purpose in this case,
it's going to show me everything that
would have been deleted. I can say, "Oh
my goodness, that is absolutely not what
I want."
I can retype the LS command
star.gif
without the space.
Ah, perfect. Those are the only two
files I want to get rid of.
I can use the up arrow to bring back the
command.
And then replace the LS with RM.
And now, I get rid of only the files
that I want to get rid of.
And those are the basics of removing
files on Linux.