How do I delete a file in Linux using command line option? How can I remove a linux directory?

Deleting files and directories is a necessary and inevitable task when working Linux. In this guide, you will learn how to remove files and directories from the command line in Linux using the RM Command.

tutorial on removing linux Files and Directories using the command line


  • A command line / terminal window (Ctrl-Alt-T or Ctrl-Alt-F2)
  • A user account with sudo privileges (optional)

How To Remove Files in Linux With rm Command

The rm command deletes files in a Linux OS. To be precise, the command unlinks the data from the file name, allowing the user to overwrite on that particular storage space.

To delete a single file, entering the following in the command line:

rm filename

The rm command can be used  to delete more than one file at a time:

rm filename_1 filename_2 filename_3

Wildcards can be used with this command.

For example, to delete all files with the .bmp filename, enter:

rm *.bmp

This method is also used to delete all files that contain a string of characters:

rm *sample*.*

This will erase any file that has the word sample in the name.

The system will search the current directory for the file you want to delete.

To delete a file in a different directory, either switch to that directory first:

cd /tmp
rm filename

Or you can specify the file location in a single command directly:

rm /tmp/filename

Note: Once you have used the rm command to delete a file, you will not be able to access it. The only way to retrieve a file would be to restore it from a backup (if ones is available).

Options with the rm Delete Command

You can adjust the way the rm command works by adding options. An option is a hyphen, followed by one or more letters that stand for commands.

If you’re deleting multiple files, add a confirmation prompt. Use the –ioption to use an interactive dialog:

rm –i *.key

Confirm the deletion of files by typing ‘yes’ or ‘no.’

Terminal asks for confirmation before removing file.

To display the progress of the deletion with the v or verbose command:

rm –v *.txt

The output confirms that the file test.txt has been successfully removed.

Confirmation that the test.txt file has been removed.

To force the removal of a file that’s write-protected, use the –f option:

rm –f filename

To use sudo privileges for a file that says Access denied and delete it:

sudo rm filename

How to Remove Directory in Linux

A directory (or folder) can be empty or it can contain files in it. If you need to delete a directory/file you can use one of the following two commands:

  • rmdir command – removes empty directories/folders
  • rm command – removes a directory/folder along with all the files and sub-directories in it

How to Remove Directory in Linux with rm Command

The rm command alone is not used for removing directories and folders. However, by adding the -r (-R) option, you can utilize the command to remove a directory along with all its contents.

To remove a directory (and everything inside of it) use the –r option as in the command:

rm –r dir_name

This will prompt you for confirmation before deleting.

To remove a directory without confirmation:

rm –rf directory

Also, you can delete more than one directory or folder at a time:

rm –r dir_name1 dir_name2 dir_name3

How to Remove Directory in Linux with rmdir Command

As previously mentioned, the rmdir command is used only when deleting empty folders and directories. If a specified directory is not empty, the output displays an error.

The basic syntax used for removing empty folders/directories is:

rmdir [dir_name]

Additionally, you can delete multiple empty directories at once by typing:

rmdir [dir_name1][dir_name2][dir_name3]

If the command finds content in one of the listed directories, it will skip it and move on to the next one.


With this tutorial, deleting files and directories in Linux was made easy. The rm and rmdir commands are flexible with many options available.

Next you should also read