How to Delete a File or Folder using the Terminal on Linux Mint

Share
In this tutorial, we'll learn how to delete a file, folder (directory) or symlink with the terminal on Linux Mint using the rm and rmdir commands.

Danger: deleting files using the terminal is a terrible idea in general, as it's error-prone and unforgiving. You'll receive NO CONFIRMATION DIALOG, and deleted files are NOT SENT TO THE TRASH BUT DELETED PERMANENTLY*. Combined, this means you can easily delete the wrong file by accident, and if that happens there is no easy way to recover it as it won't be in a recycle bin.

Note: if you have "permanently" deleted an important file by accident, it may still be recoverable if you shut down your PC immediately and contact a data recovery specialist. However, there is no guarantee that they'll be able to recover it.

Note: if you are learning to use the terminal and you want to try deleting files, you're advised to create a separate user in your operating system and log in as this separate test user to limit the potential damage. Users on Linux can't delete files owned by other users, for example [What Read, Write, and Execute Permissions Mean on Linux].

Deleting a Single File using the Terminal

Let's start with the simplest case: to delete a single file using the terminal, execute the following terminal command [how?]:

rm unwanted-file

Danger: you will not be asked for confirmation before the file is deleted.

Note: rm doesn't print a success message when it successfully deletes a file.

The command above will delete a file in the current working directory called unwanted-file. See [How to Refer to Files and Folders using the Terminal on Linux Mint] for how to write this filename argument.

Observation: the rm command will remove a single hard link to an inode, which means that if you use hard links (you probably don't) and you have multiple hard links to the same file, the file's data won't be deleted from the disk until you delete all the other hard links to that inode.

Deleting a Symlink using the Terminal

The rm command can also be used to delete a symlink. For example: (below, $ represents the shell prompt)

/foo$ touch bar

/foo$ ln -s bar fries

/foo$ ls --file-type
bar fries@

/foo$ rm fries

/foo$ ls --file-type
bar

Above, we symlink fries -> bar. Observe how deleting fries using rm doesn't delete the symlinked bar.

Deleting an Empty Folder using the Terminal

We can delete an empty directory using the command rmdir.

/foo$ mkdir bar fries

/foo$ touch fries/fish

/foo$ ls --file-type
bar/ fries/

/foo$ rmdir bar

/foo$ ls --file-type
fries/

/foo$ rmdir fries
rmdir: failed to remove 'fries': Directory not empty

As we can see above, when rmdir removes a directory, no confirmation is asked, and no success message is shown either, just like what happens when you use rm. Observe that if we try to delete a directory that contains files (or symlinks), rmdir will fail and print an error message.

Deleting a Folder and All Its Files and Subfolders using the Terminal

The command rmdir can only remove empty directories. How do we remove a directory that isn't empty, then? Do we have to delete files one by one? Naturally, there is a command for this. But before that: are you SURE you want to do this? One mistake and you can accidentally delete all your files. You will not get Linux brag points for deleting a directory full of files from the terminal, you know? It takes 5 seconds to open your file manager and do it in a GUI full of confirmation dialogs and proper on-screen indicators that prevents all sorts of mistakes. And it might even send the folder to the trash, too, giving you the opportunity to undo it.

I'll teach you one little trick. You can do this on Linux Mint:

nemo .

And it opens Cinnamon's file manager, Nemo, automatically displaying the current working directory indicated by the dot (.). It can be a bit hard to see but that's nemo followed by a . as the first positional argument: nemo .. Then you can just click on the file and press the Delete key on the keyboard. Nobody is going to judge you for that.

If you really want to do this, the command is rm, and we need the --recursive and --force options, or the -rf flags.

rm -rf important-folder-you-will-accidentally-delete

This will delete important-folder-you-will-accidentally-delete and all its descendants, including all subfolders. It's worth noting that if you execute this command on the root directory, it will delete all files in your operating system, and you won't be able to even boot anymore.

And if you think you're smart enough to not type the wrong folder, just keep in mind that everyone makes mistakes sometimes, as history clearly shows.

Warning: do NOT use rm with Asterisk!

If you know a bit about Bash, you know that you can use an asterisk * to refer to all files in a directory. Do NOT use this with rm, because that's not what asterisk does! The asterisk will simply turn all filenames found in the current working directory into arguments. For example:

/foo$ touch ./-rf bar

/foo$ echo *
-rf bar

This means if you rm *, and there is a file called -rf in the current working directory, you will rm -rf even if you didn't explicitly type those flags.

The solution to this problem is to use ./* instead.

/foo$ touch ./-rf bar

/foo$ echo ./*
./-rf ./bar

Observations

Shred not: it seems there is an alternative command for deleting files called shred that overwrites the file before deleting it, which would prevent it from being recovered. However, due to a hardware-level optimization in SSD's called "wear leveling" the shred method doesn't reliably work since the blocks "overwritten" aren't actually the original blocks in which the file's data resided [https://unix.stackexchange.com/a/593340] (accessed 2025-03-14).

Written by Noel Santos.

About the Author

I'm a self-taught Brazilian programmer graduated in IT from a FATEC. In a world of increasingly complex and essential computers, I decided to use my technical expertise in hardware, desktop applications, and web technologies to create an informative resource to make PC's easier to understand.

View Comments