Skip to content

how to delete a file in linux command line

October 14, 2023
Linux

Discover step-by-step instructions on how to delete files in Linux using command line commands. Safely manage your file system with these simple techniques.

If you’re new to Linux or just looking to brush up on some essential command line skills, learning how to delete a file is a fundamental task. Whether you want to clear up some disk space or remove unnecessary files, Linux offers powerful commands for file management. In this guide, we’ll walk you through the steps to delete a file using the Linux command line.

Prerequisites

Before you get started, you’ll need:

  • A Linux-based system (e.g., Ubuntu, CentOS, Debian)
  • Access to the terminal or command line interface
  • The file you want to delete (replace “filename” with the actual name of the file)

Method 1: Using the rm Command

The rm (remove) command is the most common way to delete a file in Linux. To delete a file, open your terminal and enter the following command:

rm filename

For example, if you want to delete a file named “example.txt,” you would type:

rm example.txt

Caution: The rm command is powerful and permanent. There’s no “Recycle Bin” in Linux, so use it carefully. Once a file is deleted with rm, it’s gone for good.

Method 2: Using the unlink Command

You can also use the unlink command to delete a file:

unlink filename

This command is similar to rm and is used for file removal

Method 3: Using the trash-cli Command

If you want to move files to a “trash” or “recycle bin” instead of deleting them permanently, you can use the trash-cli utility. This tool moves files to a trash directory, allowing for easy recovery if needed. To use it, you’ll need to install it first:

sudo apt-get install trash-cli  # For Ubuntu and Debian-based systems

Then, delete a file using the trash-put command:

trash-put filename

To restore a file deleted using trash-cli, you can use the trash-restore command.

Conclusion

Deleting files in the Linux command line is a straightforward process. The rm and unlink commands are the most common options for permanent deletion, while the trash-cli tool provides a safer alternative with a “recycle bin” feature.

Remember to use these commands with caution, especially rm, as it permanently removes files. Make sure you’ve backed up important data before performing file deletions. With these skills, you’re well on your way to efficient file management in Linux.

If you have any questions or encounter issues, feel free to ask in the comments below. Happy file management!