How to remove a file in Linux

August 15, 2024 / How-to Guide

In this guide, you will explore how to remove a file in Linux. Removing a file in Linux helps free up disk space, eliminate unnecessary files, and maintain an organised and efficient file system.

The ‘rm’ command (short for remove) is one of the most basic and widely used commands for deleting files or directories, with its behavior varies based on the type of file or directory being removed.

Follow the steps:

  1. To delete a file, open the terminal and enter the following command:
    rm filename
  2. Replace “filename” with the name of the file you want to delete. If the file is in another directory, include the full path to the file in the command, for example:
    rm /home/user/documents/filename
  3. If the file or directory is write-protected, you will be prompted for confirmation. To delete non-empty directories and skip confirmation prompts, use the -r (recursive) and -f (force) options together:
    rm -rf directory
  4. To visualize the directory structure before using the rm -rf command, install the tree command:
    sudo apt-get install tree

    The tree command will display the directory structure and files in an easy-to-read format, helping you understand what will be deleted.

Important Note: It is important to note that the rm command permanently deletes the file, and it cannot be recovered once deleted. Therefore, use this command with caution, especially when deleting critical files.

That is it! Now you know how to remove files in Linux. Apart from this, you also need to manage the directory. You can use specific commands for that. To learn how, check our guide on how to remove directories in Linux, which provides detailed steps for both removing empty and non-empty directories.