How to Remove Files and Directories on Ubuntu

This quick guide will show you how to remove files and directories on the Ubuntu operating system.

Ubuntu Remove file or Directory

Occasionally, you may want to remove a file or directory from your system. Luckily Ubuntu and Linux, in general, provide a simple tool that we can use to delete files.

The command we will show you how to use to remove files and directories on Ubuntu is the “rm” command. We will only cover basic usage of this command, so be sure to check out our complete guide on using the rm command on Linux to learn more.

This command is a core part of Unix, so it is available on all Linux-based operating systems, including Ubuntu.

If you use a desktop flavor of Ubuntu, you can open the terminal quickly by pressing CTRL + ALT + T on your keyboard.

How to Remove a File on Ubuntu

Let us start by exploring how you can remove a file from the Ubuntu operating system. There are two different commands you can use on Ubuntu to remove a file, these commands being unlink or rm.

In this section, we will focus on using the “rm” command, as “unlink” only allows removing a single file.

Removing a Single File

Removing a single file on Ubuntu is a very straightforward process.

All you need to do is write “rm” followed by the name of the file you want to remove from your system.

rm FILENAME

If the file you are trying to remove on Ubuntu is write-protected, you will be prompted to confirm the deletion.

To confirm the removal of this file, you need to type in “y“, and then press the ENTER key.

rm: remove write-protected regular empty file 'FILENAME'?

Removing Multiple Files from Ubuntu

You can also remove multiple files from Ubuntu using a single rm command.

There are two different ways that you can remove multiple files. The first way is to specify the name of a file one after another, like shown below. You can name as many files as you want.

rm FILENAM1 FILENAME2 ...

The second way is using wildcards using the asterisk symbol (*). This symbol allows you to match multiple files ending or starting with a particular string.

For example, you can run the following command to remove all “.png” image files from the current directory.

rm *.png

Alternatively, if you want to remove all files from the current directory on Ubuntu that start with “pimylifeup“, then you could use the following.

rm pimylifeup*

Require Confirmation Before Deleting Files

you want to be extra careful when removing files from Ubuntu, you can use the “-i” option.

This option tells the remove command to be interactive and prompts whether to remove it on every file. It can be especially useful when using wildcards to remove multiple files simultaneously.

rm -i FILENAME...

With this option enabled, you will be required to confirm whether to delete the specified file, as shown below.

To confirm the deletion of the specified file, type in “Y“, then press the ENTER key.

rm: remove regular empty file 'FILENAME'?

Force Remove a File on Ubuntu

To force remove a file on Ubuntu, you will want to use the “-f” option alongside the “rm” command.

This option tells Ubuntu that it should delete this file regardless of any warnings that might occur, such as if the file is write-protected.

rm -f FILENAME...

How to Remove a Directory on Ubuntu

It is possible to remove a directory from the Ubuntu operating system using two different commands.

The first of these commands is “rmdir“. This command allows you to delete an empty directory. However, we will focus on the second command with the name “rm“.

The “rm” command allows you to remove a directory from Ubuntu regardless of whether it is empty or not. It is also the same command you use to remove files from your system.

Removing an Empty Directory

To remove an empty directory from your Ubuntu system, you only need to use “rm” followed by the “-d” option and finally, the directory’s name.

The “-d” option tells the “rm” command that what you are trying to delete is a directory. If you use the force (“-f“) option, you don’t have to worry about using this option.

rm -d DIRNAME...

You will run into the following error if you attempt to delete a non-empty directory on your Ubuntu device.

rm: cannot remove 'DIRNAME': Directory not empty

How to Remove a Non-Empty Directory on Ubuntu

Removing a non-empty directory from an Ubuntu system is possible by utilizing the recursive option (-r).

This option tells the “rm” command to remove the specified directory and any files or directories within it.

rm -r DIRNNAME

If there is a file or directory within the directory you are deleting that is write-protected, you will be prompted to confirm its deletion. To delete the file/directory, type in “Y” then press the ENTER key.

rm: remove write-protected regular empty file 'DIRNAME/FILENAME'?

Force Removing a Non-Empty Directory

When recursively removing a directory from Ubuntu, you can force remove files. By forcing the removal, you won’t be prompted when rm attempts to delete a write-protected file.

To force remove non-empty directories, you will want to use the rm command followed by the “-r” (recursive) and “-f” (force) options.

rm -rf DIRNAME

Removing Multiple Directories on Ubuntu

Like removing a file on Ubuntu, it is possible to remove multiple directories from your system.

There are two ways that you can do this. First, you can specify multiple directories by specifying them one after another.

rm DIRNAME1 DIRNAME2 DIRNAME3 DIR...

You can also use wildcards to remove directories that contain part of a name by using the asterisk symbol (*).

rm DIR* *DIR

Conclusion

Hopefully, at this point in the guide, you will know how to remove a file or directory on Ubuntu.

Knowing how to remove things from your Ubuntu device is crucial to managing any Linux system.

Please comment below if you have any questions about how to delete a file or directory using the terminal on Ubuntu.

If you found this tutorial helpful, you can check out our many other Ubuntu guides. We also have a wealth of general Linux guides.

Leave a Reply

Your email address will not be published. Required fields are marked *