• Start Here
    • How to Install Linux
    • Which Distro To Choose
    • 10 Best Linux Distributions of 2023
  • Distros
    • Ubuntu
  • Linux Commands
  • Virtualization
  • Open Source
Linux Start
  • Start Here
    • How to Install Linux
    • Which Distro To Choose
    • 10 Best Linux Distributions of 2023
  • Distros
    • Ubuntu
  • Linux Commands
  • Virtualization
  • Open Source
Linux Start
Home»Distros»Ubuntu»How to Delete A File In Ubuntu

How to Delete A File In Ubuntu

Anup ThapaBy Anup ThapaFebruary 27, 2023 Ubuntu 3 Mins Read

If you recently switched over from another platform to Ubuntu, you’re likely still deleting files via the GUI as it feels familiar to your previous OS.

This is fine, but you’ll eventually encounter cases where you can’t delete a file this way (mostly due to permissions issues). It’s also not ideal if you want to delete a lot of files together.

As such, we’ll cover different ways to delete files in Ubuntu in this article. The most popular method is rm, while beginners will find that trash is a better fit for their file deletion needs.   

Rm

Rm is the standard way to remove files and directories in Linux. Let’s start with files. To delete a file named test.txt from the current directory, use

rm test.txt

Or use the full file path like so

rm ~/Downloads/Feb/test.txt

To delete multiple files from the current directory, specify their filenames like so

rm test.txt tast.txt tost.jpg

You can also use the * wildcard to delete multiple files at once. This is useful when dealing with a very large amount of files. Some examples are listed below.

  • rm *.tar – Delete all files with the .tar extension.
  • rm t* – Delete all files starting with the letter t.
  • rm Test* – Delete all files starting with the string ‘Test’.
  • rm *test* – Delete all files with ‘test’ in the filename.

Rm is easy to mess up with, and handling multiple files together with wildcards can make mistakes even more likely. And rm doesn’t issue warnings when deleting non-write-protected files. The -i option can be useful here if you want a confirmation prompt before removing every file.

rm -i *.tar 

If you want to delete a directory, you can specify the path and use the recursive option like so

rm -r /home/anup/Desktop/testfolder

Finally, if any errors prevent you from deleting a file/directory, add the force option as shown below. Do exercise caution with -rf though. You can easily delete the wrong directory as you won’t see any warning prompts.

rm -f /home/anup/Desktop/testimage.png
rm -rf /home/anup/Desktop/testfolder

Unlink

Unlink deletes a link to a file, decreasing the file’s link count by 1. Assuming there are no more hard links to the file and no process is currently accessing the file, the file is deleted. It’s a safe and easy way to delete individual files.

To delete a file named test in the current directory, simply use

unlink test

Or you can specify the full path like so

unlink ~/Desktop/test.txt
unlink-file-in-ubuntu

Trash

As we said, stories of people deleting files, directories, or even entire disks accidentally with rm aren’t uncommon. For beginners, we recommend installing the trash-cli utility. It’ll move deleted files and folders to the trashcan instead of permanently deleting them.

First, install it with

sudo apt install trash-cli

The syntax is similar to the previous two utilities.

trash test
trash ~/Desktop/test.txt

Delete Files via the GUI

If you’d rather not use the terminal, deleting files graphically usually works too. Simply select the file and press the Delete key, or right-click the file and select Move to Trash.

By default, trashed files on Ubuntu are permanently deleted after 30 days. You can right-click the Trash icon and select Empty Trash to delete them immediately. You can also press Shift + Delete when deleting to permanently delete files.

Anup Thapa
  • LinkedIn

Anup Thapa is a Linux enthusiast with an extensive background in computer hardware and networking. At LinuxStart, Anup is an integral part of our editorial team. His goal is to effectively communicate technical concepts in a simplified form understandable by new Linux users. To this end, he mainly writes beginner-friendly tutorials and troubleshooting guides. Outside of work, he enjoys experimenting with different Linux distros and reading up on things like networking and virtualization.

Leave A Reply Cancel Reply

Table of Contents
  • Rm
  • Unlink
  • Trash
  • Delete Files via the GUI
Linux Start
Guides

What is the Linux Kernel?

The Linux Kernel is the core of the Linux operating system and is central to…

How To Install Linux Mint

How To Install Linux on VirtualBox

Getting Started with Linux

How to Install Linux

10 Best Linux Distros of 2023

Which Linux Distro Should You Choose? A Complete Guide to Help You Decide

© 2023 LinuxStart.com

Type above and press Enter to search. Press Esc to cancel.