How to Remove Files and Directories using the Linux Command Line

How to Remove Files and Directories using the Linux Command Line

In this tutorial we will go through how to remove files and directories in Linux using rm, unlink, and rmdir.

These commands also work on macOS or other Unix-like operating systems.

Deleting files using a desktop manager is easy and convenient, and you can usually recover them from the trash if you need them back.

However, with the command line, it’s easier to delete multiple files, based on various patterns that you can set. However, when removing files and directories using the command line, they are not moved into the Trash, and will be more difficult to recover, if at all.

How to Remove Files in Linux

To remove files in Linux, you can use either the rm or unlink command.

Both commands delete files, but rm is preferred because it has more options and can handle multiple arguments at the same time.

[powerkit_alert type=”warning” dismissible=”false” multiline=”true”]Important Note
Make sure that you’re ready to delete the file because once it’s deleted, it’s very difficult to recover.[/powerkit_alert]

Example of how to use rm and unlink commands below:

rm filename
unlink filename

If the file you’re removing is write-protected, you will be asked to confirm the deletion. To go ahead with removing the file, press y and then enter. If the file is not write-protected, it will delete without confirmation.

We’ll focus on the rm command, since it offers various options, while unlink avoids that.

The rm command provides multiple options to the users including:

  • -i (interactive removal): ask users to confirm before removing each file.
  • -f (force removal): removes the confirmation part completely, even for the write-protected files.
  • -r (recursive removal): uses traversing to recursively delete all files and subdirectories in a parent directory.
  • -v (verbose): explains what is being done

[powerkit_alert type=”warning” dismissible=”false” multiline=”true”]--help
You can see the options and explanations yourself by running rm --help.[/powerkit_alert]

Interactive: rm -i

To confirm each time before deleting a file using the rm command, simply append -i to the command. Doing that will reduce the risk of accidentally removing something important when using rm with wildcards.

rm -i *.txt

Example Usage

I have some files in a folder and I think I want to remove most .txt files, but want to keep some.


To quickly go through each .txt file and decide which to remove I run:

rm -i *.txt

Then I’m prompted each time to type in Y if I really want to remove the file. If I leave blank or type in N, and then press Enter (⏎), then the file isn’t removed:

rm: remove regular empty file 'afile.txt'? Y⏎
rm: remove regular empty file 'grocerylist.txt'? Y⏎
rm: remove regular empty file 'somenotes.txt'? Y⏎
rm: remove regular empty file 'superduperimportantfile.txt'? N⏎
rm: remove regular empty file 'temp.txt'? Y⏎

Force Remove: rm -f

On the contrary, if you want to delete any file (even write-protected) without confirmation, then append -f and filename to the rm command:

rm -f filename

Recursive Removal: rm -r

To remove all files and subdirectories in a given directory, switch the directory and use a wildcard character:

rm -r /path/to_the_Dir/*

[powerkit_alert type=”warning” dismissible=”false” multiline=”true”]Important
The combination of -r and -f, when resulting in rm -rf /path/to_the_Dir/* is one of the more risky commands on Linux, because it is very easy to harm your system. It’s important to be aware of this, as this does happen, and there are even memes about it. [/powerkit_alert]

Verbose Removal: -v

Mostly, the rm commands delete files silently and don’t show exactly what it’s deleting. To see each file that is being deleted, use the rm command appended with a -v:

rm -v /path/to_the_Dir/*

Example Usage

I have the files, and thatotherfile.log, and I’ll run the command:

rm -v *


removed 'superduperimportantfile.txt'
removed ''
removed 'thatotherfile.log'

Remove Multiple Files

To remove multiple files in the same directory:

rm file1 file2

Remove All Files in The Current Directory

If you want to delete all files in the current directory, you can use an asterisk (*) alongside the rm command. The asterisk stands for any character and the question mark represents a single character.

The following command will delete all files in the current directory:

rm *

You can also get creative with the commands, and delete a specific file type, multiple files in a row, and so on.

To remove a specific file type (e.g. PNG), type the rm command with the asterisk followed by the file extension:

rm *.png

To remove all files with a single character extension:

rm *.?

[powerkit_alert type=”info” dismissible=”false” multiline=”true”]Pattern Matching
You can read more about the special characters used in this type of Pattern Matching at > Filename Expansion > Pattern Matching[/powerkit_alert]

Remove a File from Another Directory

If the file you want to delete is not in the current directory, then you will need to provide the file path before deleting it.

rm /path/to_the_Dir/filename

Remove Hidden vs. Non-hidden Files

In Linux, the names of the hidden files start with a dot. To see hidden files, type in the ls command and pass -a as an argument. You can also -la argument to list files or directories with extra information alongside the hidden files:

ls -a
ls -la

To remove hidden files, just use their full filename, including the dot:

rm .file
rm /path/to-Dir/.* 
rm -rf /path/to_Dir/.* 
rm .*

To remove all files in a directory except hidden files, you’ll run the commands as usual, and it won’t include hidden files. Examples:

rm /path/to-Dir/*
rm -rf /path/to_Dir/*
rm *

To delete all files in a directory including the hidden files, we can use curly braces, which are a special character (a wildcard) used for commands to perform actions on more than one file at a time, by matching them to a pattern. In the following example we’re

rm -r /path/to_Dir/{*, .*}

Dotglob Option in Linux

If you enable the dotglob option in Linux, Bash includes all files with a dot in the pathname expansion results. To put it simply, you can enable dotglob to delete hidden files without mentioning the . at the beginning of the filename explicitly, as we have been doing in the previous example.

To turn on the dotglob option:

shopt -s dotglob

To remove all files including the hidden files:

rm -r /path/to-Dir/*

To turn off dotglob, simply append -u the command:

shopt -u dotglob

How to Remove Directories in Linux

On most Linux filesystems, deleting a directory requires write privileges on the directory and its contents. Otherwise, you will encounter an Operation Not Permitted error.

In Linux, you can remove a directory using the rm -d or rmdir commands. The rmdir command deletes empty directories whereas the rm command can remove directories and recursively delete all its data.

To delete an empty directory in Linux:

rm -d dirname
rmdir dirname

Delete A Non-empty Directory in Linux

To remove a non-empty directory, you can use the rm command with the -r (recursive) option:

rm -r dirname

To remove multiple non-empty directories, simply list the name of each directory followed by a space after rm -r command:

rm -r dir1 dir2 dir3

To remove a directory/directories without the confirmation prompt, use the rm command followed by -r (recursive) and -f (force) options:

rm -rf dirname

Just like files (discussed above), you can also use wildcards and regular expressions to delete multiple directories.

Combine Options

As you may have noticed in other commands and some of the above examples, you can combine options for rm.

For example we can run the following command to force remove all .php files in the current directory, but we also want to be careful, so we’ll remove them interactively:

rm -fi *.php

Or we may want to see all files that were removed after we run rm -rf on all files in the current directory:

rm -fv *


We hope this article has given you a better understanding of the various ways you can remove files and directories in Linux via rm, unlink and rmdir.

If you have any questions or have encountered any issues feel free to leave a comment or contact us, and we’ll get back to you as soon as we can.

Notify of
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

Inline Feedbacks
View all comments
You May Also Like
Bash Check File If Exists
Read More

Bash Check If File Exists

When working with files in bash, it is essential to know whether the particular file or directory exists.…