How to Delete Files and Folers in Linux

Clean up your Linux workspace effortlessly with the rm and rmdir commands.

To delete a file, files, or a folder in Linux, the Linux command to use is either rm or rmdir. The rm command stands for “remove” and is used to delete files and directories. By using various options, you can remove files, directories, and even the contents of directories. For example, rm filename will delete a file, while rm -r directoryname will delete a directory and all its contents.

The rmdir command stands for “remove directory” and is specifically used to delete empty directories. If a directory contains any files or subdirectories, the rmdir command will not remove it, and an error message will be displayed. In contrast, the rm command with the -r option can delete non-empty directories. Essentially, rm is more versatile, while rmdir is more specialized for removing empty directories.

In this post, we look at different ways to use rm and rmdir.

How to use rm

1. Delete a file
rm [file]

The rm command removes a single file. To do this, type rm followed by the name of the file you want to delete.


The following command is used to remove a specific file named example.txt located within a directory named myfolder.

rm myfolder/example.txt

Here’s an example of how it works:

  1. Suppose you have a directory named myfolder and inside that directory, there’s a file named example.txt.
  2. You run the command rm myfolder/example.txt.
  3. The file example.txt inside myfolder will be deleted, and there will be no output message displayed in the terminal by default.
  4. If you try to access the file example.txt again, you’ll get an error message like No such file or directory.

Before running the command, if you had:

└── example.txt

After running the command, the structure would be:

2. Remove files without confirmation
rm -f [file]

This option allows users to remove write-protected files without confirmation.


Suppose you have a file named file1.txt in your current directory, and you want to delete it. You can run the following command:

rm -f file1.txt

Since the -f option is used, there will be no confirmation prompt, and the file will be deleted immediately. There will be no output displayed in the terminal if the operation is successful. If you try to view the contents of the directory afterward, you will find that file1.txt is no longer there.

3. Delete multiple files
rm [file1] [file2] [file3]

Use the rm command with filenames as arguments to remove multiple files at once.


Before running the command, let’s say you have the following files in your directory:


You run the command rm file1.txt file2.txt file3.txt.

After running the command, the files file1.txt, file2.txt, and file3.txt are deleted, and your directory now looks like this:

4. Display output message
rm -v [filename]

The -v (verbose) option allows you to get information about what is being removed.


When you execute the command rm -v example.txt, here’s the output you will get:

removed 'example.txt'

In this example, the file example.txt is deleted, and the system prints a message confirming that the file has been removed. If the file does not exist, an error message like rm: cannot remove 'example.txt': No such file or directory would be displayed instead.

5. Prompt for confirmation before deleting a file
rm -i [filename]

This option is used to request confirmation before deleting a file. Typing y (yes) confirms, typing n (no) stops.


Let’s say you have a file named example.txt and you run the command:

rm -i example.txt

The system will prompt you with a message like:

remove regular file 'example.txt'? 

You’ll then need to type y (for yes) or n (for no) to confirm or deny the deletion. If you type y and press Enter, the file example.txt will be deleted. If you type n, the file will remain untouched.

remove regular file 'example.txt'? y

After this, example.txt will be deleted if you confirmed with y.

How to use rmdir

This command removes directory as well as files within the directory. There isn’t significant difference with the rm -r command except that it can not be used to remove a file.

General syntax for rmdir command:

$ rmdir [OPTION...] [DIRECTORY...]
1. Remove a directory
rmdir [directory]

Use this command to remove a directory, but it will only be removed if it is empty.


Suppose you have a directory named myfolder and it’s empty. When you run the command:

rmdir myfolder

The directory myfolder will be deleted, and there will be no output message if the operation is successful.

However, if myfolder is not empty or does not exist, you will receive an error message. For example, if myfolder contains files or subdirectories, you might see:

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

Or if myfolder does not exist:

rmdir: failed to remove 'myfolder': No such file or directory
2. Delete multiple directories
rmdir [folder1] [folder2] [folder3]

This command allows you to delete several directories at once, but they all must be empty.


The rmdir folder1 folder2 folder3 command in Linux attempts to remove the directories named folder1, folder2, and folder3. This command will only succeed if all three of these directories are empty; otherwise, an error message will be displayed.

Here’s an example of how this might work:

If folder1, folder2, and folder3 are all empty directories, the command will remove them, and there will be no output message.

If any of these directories are not empty or do not exist, an error message will be displayed for each problematic directory.

Example of the output if folder2 is not empty:

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

In this case, folder1 and folder3 would still be removed if they were empty, but folder2 would remain untouched.

3. Remove parent directories
rmdir -p [directory-path]

The -p option removes the specified directory and its parent directories.


Here’s an example of how the command rmdir -p folder_a/folder_b would work:

Suppose you have a directory structure like this:

└── folder_b

If both folder_a and folder_b are empty, running the command rmdir -p folder_a/folder_b will remove folder_b first, and then, since folder_a becomes empty, it will remove folder_a as well.

If there were any files or subdirectories inside folder_a or folder_b, the command would not remove them, and you would receive an error message like:

rmdir: failed to remove 'folder_a/folder_b': Directory not empty

In the successful case, there would be no output, and both directories would be deleted.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open