Skip to Content

How to Remove Files & Directories in Ubuntu Linux

In this tutorial, you'll learn how to remove files and directories using the command line in Ubuntu Linux. You'll also learn how to remove specific contents within a directory, including files and folders, both visible and hidden.

We'll be using the rm command in these examples for both actions.

Remove a File

Here is the command for removing a single file:

$ rm [file_name]

Here, you would replace [file_name] with the actual name of the file you want to remove.

Similarly, you could use the unlink command in place of rm:

$ unlink [file_name]

If the file is write-protected, you'll be prompted for confirmation before the file is removed from the system.

Remove a Directory and All Its Contents

This command will remove a directory and all of its contents using the -r switch, which is short for recursive, meaning all the files and folders within the directory will be removed:

$ rm -r /path/to/directory

Remove a Directory's Visible Contents Only

If you need to remove all of the visible contents of a directory, but still want to keep that folder available, you can use the following command:

$ rm -rf /path/to/directory/*

Note that this command will only remove visible files and folders, not hidden ones.

Also, note the addition of the -rf switch in the command, which is used to search for all recursive files only, excluding folders within the directory.

Remove All Files & Folders from a Directory

If you need to remove all files and folders from within a directory, whether they are visible or hidden, you can use the following command to do so:

$ rm -rf /path/to/directory/{*,.*}

Remove Specific File Types

If you need to remove only files with a .txt extension, for example, you can do so with the following command:

$ rm -f *.txt

Where * acts as a wildcard for all file names containing .txt in the file name.

Remove Multiple Files & Directories at Once

To remove multiple files in a single command:

$ rm filename1 filename2 filename3

To remove multiple directories in a single command:

$ rm -r directory1 directory2 directory3


Now that you know how to remove files and directories with the command line in Ubuntu Linux, give it a shot by running some tests in a test directory.

Created: May 23, 2022


There are no comments yet. Start the conversation!

Add A Comment

Comment Etiquette: Wrap code in a <code> and </code>. Please keep comments on-topic, do not post spam, keep the conversation constructive, and be nice to each other.