Skip to main content

How to Remove Files and Directories in Linux Command Line [Beginner's Tutorial]

Learn how to delete files and remove directories with rm command in Linux.

Abhishek Prakash

Warp Terminal

How to delete a file in Linux? How to delete a directory in Linux? Let’s see how to do both of these tasks with one magical command called rm.

How to delete files in Linux

Let me show you various cases of removing files.

1. Delete a single file

If you want to remove a single file, simply use the rm command with the file name. You may need to add the path if the file is not in your current directory.

rm file.txt

If the file is write protected i.e. you don’t have write permission to the file, you’ll be asked to confirm the deletion of the write-protected file.

rm: remove write-protected regular file 'file.txt'?

You can type yes or y and press enter key to confirm the deletion. Read this article to know more about Linux file permissions.

2. Force delete a file

If you want to remove files without any prompts (like the one you saw above), you can use the force removal option -f.

rm -f file.txt

3. Remove multiple files

To remove multiple files at once, you can provide all the filenames.

rm file1.txt file2.txt file3.txt

You can also use wildcard (*) and regex instead of providing all the files individually to the rm command. For example, if you want to remove all the files ending in .hpp in the current directory, you can use rm command in the following way:

rm *.hpp

4. Remove files interactively

Of course, removing all the matching files at once could be a risky business. This is why rm command has the interactive mode. You can use the interactive mode with the option -i.

rm -i *.txt

It will ask for confirmation for each of the file. You can enter y to delete the file and n for skipping the deletion.

rm: remove regular file 'file1.txt'? y
rm: remove regular file 'file2.txt'? n

You just learned to delete files in the terminal. Let’s see how to remove directories in Linux.

DigitalOcean – The developer cloud
Helping millions of developers easily build, test, manage, and scale applications of any size – faster than ever before.
Get started on DigitalOcean with a $100, 60-day credit for new users.

How to remove directories in Linux

There is a command called rmdir which is short for remove directory. However, this rmdir command can only be used for deleting empty directories.

If you try to delete a non-empty directory with rmdir, you’ll see an error message:

rmdir: failed to remove 'dir': Directory not empty

There is no rmdir force. You cannot force rmdir to delete non-empty directory.

This is why I am going to use the same rm command to delete folders as well. Remembering the rm command is a lot more useful than rmdir which in my opinion is not worth the trouble.

1. Remove an empty directory

To remove an empty directory, you can use the -d option. This is equivalent to the rmdir command and helps you ensure that the directory is empty before deleting it.

rm -d dir

2. Remove directory with content

To remove directory with contents, you can use the recursive option with rm command.

rm -r dir

This will delete all the contents of the directory including its sub-directories. If there are write-protected files and directories, you’ll be asked to confirm the deletion.

3. Force remove a directory and its content

If you want to avoid the confirmation prompt, you can force delete.

rm -rf dir

4. Remove multiple directories

You can also delete multiple directories at once with rm command.

rm -r dir1 dir2 dir3

Awesome! So now you know how to remove directory in Linux terminal.


Here’s a summary of the rm command and its usage for a quick reference.

Purpose Command
Delete a single file rm filename
Delete multiple files rm file1 file2 file3
Force remove files rm -f file1 file2 file3
Remove files interactively rm -i *.txt
Remove an empty directory rm -d dir
Remove a directory with its contents rm -r dir
Remove multiple directories rm -r dir1 dir 2 dir3

I hope you like this tutorial and learned to delete files and remove directories in Linux command line. If you have any questions or suggestions, please leave a comment below.

Abhishek Prakash