How to Delete a File or Directory in Linux

Files and directories in Linux are fundamental components of the operating system. Linux users can quickly delete directories using the distro’s desktop environment (GUI) or the command line.

Note: For this guide, we’re using Zorin OS 16, an Ubuntu-based distro. The Linux commands featured in this article are distro-independent, meaning they will work on any Linux distro.

Deleting Files and Directories from the Linux Command-Line

To delete files or directories from the GUI, select the file(s)/directory(ies) you want to delete, right-click your mouse button, and scroll to and select Move to Trash.

Delete file(s) from the Linux GUI.
Delete file(s) from the Linux GUI.
Note that the folders and files remain resident in the Linux system’s Trash until you select Empty Trash from the Trash folder.

Deleting Files and Directories from the Linux Command-Line

Deleting files from the Linux command line is accomplished using two commands, rm and unlink. Deleting directories can be achieved with the rmdir command (only if the directory is empty). To delete directories that are not empty, you will need to use the rm command with the -r option, which will be explained in detail below.

Warning: Deleting files using the command described in this section does not send them to the Linux system’s Trash folder, rather, it deletes them permanently.

Deleting Files Using the rm Command

Deleting a file using the rm command is simple:

# rm file
Delete a file using the Linux rm command.
Delete a file using the Linux rm command.

Note that the file is immediately deleted without any prompting. Also, note that deleting files using this method affords no safeguards and immediately deletes the files permanently.

A better practice is to use the rm command with the -i option to prompt for deletion.

# rm -i file
Delete a file using the rm -i command.
Delete a file using the rm -i command.

Note the prompts in the above image requesting permission to delete each individual file (i.e., rm: remove regular empty file ‘db_one’). These prompts require a yes (y) or no (n) response.

You can also delete multiple files by separating the individual files with a space.

# rm -i file1 file2
Remove multiple Linux files using the rm command.
Remove multiple Linux files using the rm command.

Note that wildcards (*) can also be used to delete multiple files at once.

Recursively Deleting Files and Directories Using the rm -r Command.

Directories and included files can be deleted from the command line by using the recursive, or -r option with the rm command (in this sense, recursive means that all files and subdirectories contained within the directory being deleted will also be deleted).

# rm -r -i directory
Recursively delete linux directories and files using the rm -r -i command.
Recursively delete Linux directories and files using the rm -r -i command.

Note that we included the -i option to ensure we get a confirmation of the file we wish to delete.

It’s important to note that the rm command is the only command you can use to delete both files and directories.

Deleting Files Using the unlink Command

The Linux unlink command is a rarely-used command-line utility that also deletes files.

# unlink filename
Remove Linux files using the unlink command.
Remove Linux files using the unlink command.

Unlike the rm command, the unlink command does not have an interactive option to verify the deletion of a file, nor can the unlink command delete directories. In fact, unlink has only two command-line options, –help and –verbose. As such, always use the rm command with the -i option to delete files.

Deleting Folders Using the rmdir Command

Linux users can also delete Linux directories using the rmdir, but only if the directory being deleted is empty.

# rmdir directory
Remove an empty Linux directory with the rmdir command.
Remove an empty Linux directory with the rmdir command.

If the directory you’re deleting is not empty, you’ll receive a “failed to remove…Directory not empty” error message.

Attempting to delete a non-empty directory using the rmdir command.
Attempting to delete a non-empty directory using the rmdir command.

In these instances, you’ll have to use the rm command with the -r (recursive) option described earlier or manually delete the directory contents before you can delete it.

Linux Commands Used in This Article

  • rm – remove files or directories.
  • rmdir – remove empty directories.
  • unlink – call the unlink function to remove the specified file.
  • ls – list directory structures.

Final Words

The Linux rm, rmdir, and unlink Linux commands are useful tools for removing files and directories from your system if used correctly and safely, as outlined in the examples above. Be safe, not sorry, use the -i command option to avoid deleting unwanted files and directories.

Leave a Comment