Delete a File or Directory in Linux: It Doesn’t Have to Be Scary

Claudio Sabato
4 min readFeb 10, 2020

You open the Linux shell…and you have to delete a file or a directory…

It can be uncomfortable especially if you are just getting used to work with Linux.

This tutorial will help you get familiar with the command to delete files and directories in Linux.

How to Remove a File

The main command to delete files is rm that allows to delete one or multiple files.

To delete a single file you specify the rm command followed by the filename:

rm filename

Now, let’s say we have a file called filename with the following permissions:

-r--r--r-- 1 ec2-user ec2-user 0 Feb 10 03:47 testfile

If we try to delete it we will see the following in the shell:

rm testfile
rm: remove write-protected regular empty file 'testfile'? y

And we have to confirm to delete the file with the letter y. But why do we see this message?

Because the file doesn’t have write permissions for the ec2-user. So, let’s recreate the file and make sure it has write permissions for the ec2-user:

touch testfile
ls -al testfile
-rw-rw-r-- 1 ec2-user ec2-user 0 Feb 10 03:52 testfile

You can see that by default Linux assigns write permissions to the user and group owner (in this case both are ec2-user).

Let’s take away the write permission for the group owner to see if the write permission for the user owner is enough to delete the file without seeing the write-protected warning:

chmod 644 testfile
-rw-r--r-- 1 ec2-user ec2-user 0 Feb 10 03:52 testfile

To set the write permission just for the user owner we use the chmod 644 command (if you want to learn more about the chmod command have a look here).

How else could you get the same result using chmod?

And if we try to delete the file now:

rm testfile
ls -al testfile
ls: cannot access testfile: No such file or directory

This time the file is deleted without any confirmation being required.

To delete multiple files use the rm command followed by the list of files separated by space:

rm testfile1 testfile2 testfile3

I can also use a wildcard that matches all the three files:

rm testfile*

NOTE: Be very careful when using wildcards because you might accidentally delete files you are not planning to delete.

How to Remove a Directory

The rm command can also be used to delete directories.

Let’s delete a single directory called testdir:

rm testdir
rm: cannot remove 'testdir': Is a directory

For some reason we get a message back that says that we cannot remove it.

In this case we have an empty directory and to remove it we need to add the -d flag:

rm -d testdir
ls -al testdir
ls: cannot access testdir: No such file or directory

And what if the directory is not empty?

Let’s create a directory and a file inside it using the touch command:

mkdir testdir
touch testdir/testfile

If we use the same command above it doesn’t work because it’s only for empty directories:

rm -d testdir
rm: cannot remove 'testdir': Directory not empty

In this case we have to use the -r flag that deletes directories and their contents recursively

rm -r testdir
ls -al testdir
ls: cannot access testdir: No such file or directory

If a directory or a file is write-protected you will be asked to confirm the deletion, to avoid this especially if you are deleting a directory that contains and big number of files, you can add the -f flag, that stands for force:

mkdir testdir
touch testdir/testfile1
touch testdir/testfile2
chmod -w testdir/testfile1
rm -r testdir
rm: remove write-protected regular empty file 'testdir/testfile1'? y

Let’s do the same using the -f flag:

mkdir testdir
touch testdir/testfile1
touch testdir/testfile2
chmod -w testdir/testfile1
rm -fr testdir

So, as expected, in the last example I didn’t have to confirm the deletion of the write-protected file.

In a similar way to what we have seen for files, we can delete multiple directories specifying them in the rm command separating them by space:

rm -fr testdir1 testdir2 testdir3

Conclusion

You should now have a good understanding on how to use the Linux rm command to delete a file or a directory safely.

I have also explained you what a write-protected file is from a file permissions perspective.

Let me know if you have any questions :)

Originally published at http://codefather.tech on February 10, 2020.

--

--

Claudio Sabato

Claudio Sabato is an IT expert with over 15 years of professional experience in Python/Bash programming, Linux Systems Administration and IT Systems Design.