How to Delete a File or Directory in Linux

If you have root access on your Linux PC, you have the power to delete any file or directory you like. This can be dangerous, however, with tools like rm allowing users to carelessly erase crucial system files by accident. That’s why, if you’re trying to delete files or directories in Linux , you need to take some precautions.

There are a few ways you can remove directories or files on Linux, using either your Linux distribution’s file manager or an open terminal (as long as you have the right permissions ). If you want to know how to delete a file or directory in Linux, here’s what you’ll need to do.

Using a File Manager to Remove Files or Directories in Linux

If you’re a Linux beginner , you might be wary about using the terminal to delete files or folders in Linux. After all, there’s no turning back once you set a command like rm off to start deleting files. Rather than risk this, you can use the file manager packaged with your Linux distribution to delete files or folders instead. 

While Linux file managers do vary in design, they should work in much the same way. This guide explores how to do this using Ubuntu’s file manager, but the steps are likely to be similar for the file manager included in other distributions.

  1. To start, open the file manager on your Linux distro. This shouldn’t be too hard to find, with the user icon likely in the shape of a document folder. On Ubuntu, this app is named Files.
  1. In your distro’s file manager, navigate to the directory containing the files or subfolders you wish to delete. First, select the files or folders you wish to delete. Once they’re selected, right-click on them and select Move to Trash, Move to Bin, or Delete, depending on your distribution and locale. You can also select the Delete key on your keyboard to achieve the same effect.
  1. Most distributions operate a trash system that allows you to “store” files before they’re deleted, giving you the chance to restore them. This is typically found on the desktop, as an entry in your file manager, or as an app you can launch in your software menu. To complete the deletion process, enter the trash folder, then select the Empty or Erase option. These options may vary, depending on your distribution.
  1. Alternatively, select your item (or items), then right-click and select Delete from Trash to completely remove them instead.

How to Remove Files or Directories in Linux Using the rm Command

Removing files or folders using your distribution’s file manager app works fine, but there are some limitations. For instance, if you’re not the owner of the file, you’ll need to open your file manager with root access using the terminal to delete it. You also can’t use an app like this on a headless (terminal-only) Linux installation.

That’s where the rm command comes in. This Unix command dates back to 1971 and remains the fastest way to delete files or directories on Linux. Be warned, however—the rm command has great power, especially if you’re running it with sudo or as the root user directly, so you’ll need to take care using it.

  1. To remove a single file using rm, open a new terminal window (or remote SSH connection) and type rm file, replacing file with the correct file name. If you’re not in the same directory, you’ll need to use cd to move to it first, or use the full file path (eg. rm /path/to/file) instead.
  1. To remove multiple files, type rm file1 file2, replacing file1 and file2 with the correct file name and file path. You can add additional files to this command to remove more than two.
  1. If you want to remove an empty directory on Linux, type rm -d directory, replacing directory with the correct directory name and path. If the directory isn’t empty and has sub-folders or files, type rm -r directory, replacing directory with the correct name and path. 
  1. You can also remove multiple directories at once by typing rm -r directory1, directory2, etc.
  1. If you want to remove all files or directories that match a partial file or directory name, you can use wildcards, such as an asterisk (*). To do this, type rm fil* or rm -r dir*, replacing the placeholders fil or dir with your own file names and directory paths.
  1. If the file or directory is protected, you may need to force its deletion. To do this, type rm -rf path, replacing the placeholder path with the correct directory or file path. This command carries extreme risk, so make sure the path you’re using is correct before you proceed.
  1. If you’re worried about using the rm -rf command, you can force rm to ask for confirmation before each file or directory is erased. To do this, use rm -i path, replacing path with your own file name or directory path. For directories, use rm -ir path instead. For each entry, select the Y key on your keyboard, then select Enter to confirm.

If you want to learn more about the possible rm options and arguments, type man shred in the terminal to view the full manual.

Completely Erasing Files Using the shred Command

While commands like rm and unlink are great for deleting files in Linux, they do leave traces. You (or somebody else) could use these traces to recover files from a hard drive or portable storage that you’ve previously deleted. This might not be ideal, especially if you don’t want the files to be retrieved.

To get around this problem, you can overwrite files first using the shred command. This makes sure that all data within the file is overwritten repeatedly before it’s deleted, ensuring no traces of the file are left and preventing it from being retrieved. You may want to use this command to erase sensitive documents, for instance.

  1. To do this, open a terminal window or make a connection remotely using SSH and type shred file, replacing file with your own file name and path. If you don’t want to provide the path to the file, use cd to travel to the directory containing the file or sub-directory you wish to remove first.
  1. The shred command has additional parameters that you can pass to ensure your file is safely deleted. For instance, shred -f will force the deletion of write-protected files, while shred -u will ensure the file is completely removed once shredded, ensuring it no longer appears in your file manager or using the ls command.
  1. If you want to overwrite the file more than the standard 3 times, use shred -n 0, replacing 0 with an alternative numerical value. If you want to hide the shred effect, use shred -z to force it to use a final run that overwrites the file with zeros.
  1. The shred command doesn’t typically output any results to the terminal. If you want to see more detail about what it’s doing as it runs, use shred -v instead.

As with rm, you can learn more by typing man shred in the terminal before using the command.

Protecting Your Files on Linux Systems

When you remove a directory or file in Linux, you’re freeing up space on your hard drive for the files that truly matter, whether it’s important documents or irreplaceable photos. It doesn’t matter whether you’re using Linux Mint or any other major distribution , as the steps above should work for almost any Linux distro you choose to use.

If you’re worried about data loss, there are some steps you can take to protect your files. While Linux systems are typically more secure, a Linux antivirus can help you to keep your files safe from malware that could spread across your local network. You may want to consider automating a file backup using local or cloud storage, too.