How to delete and remove files on Ubuntu Linux

I am a new Ubuntu Linux user. How do I delete and remove a file on Ubuntu Linux based system?

You need to use the rm command. It tries to remove the files specified on the command line. Use the rm command to delete files and directories on Ubuntu Linux. This page shows how to delete and remove files on Ubuntu Linux.

Adblock detected ?
My website is made possible by displaying online advertisements to my visitors. I get it! Ads are annoying but they help keep this website running. It is hard to keep the site running and producing new content when so many people block ads. Please consider donating money to the nixCraft via PayPal/Bitcoin, or become a supporter using Patreon.

Command to delete and remove files on Ubuntu Linux

kbd { display: inline-block;
margin: 0 .1em;
padding: .1em .6em;
font-family: Arial,”Helvetica Neue”,Helvetica,sans-serif;
font-size: 11px;
line-height: 1.4;
color: #242729;
text-shadow: 0 1px 0 #FFF;
background-color: #e1e3e5;
border: 1px solid #adb3b9;
border-radius: 3px;
box-shadow: 0 1px 0 rgba(12,13,14,0.2), 0 0 0 2px #FFF inset;
white-space: nowrap;}

The syntax is as follows for the rm and unlink command to remove files on Ubuntu Linux:

  1. Open the Ubuntu terminal application (bash shell)
  2. Type any one of the following command to delete a file named ubuntu.nixcraft.txt in the current directory
  3. rm ubuntu.nixcraft.txt
    unlink ubuntu.nixcraft.txt

Let use see all rm command options to delete and remove files on Ubuntu Linux.

Delete multiple files

Type the following command to delete the file named dellLaptopSerials.txt, tom.txt, and dance.jpg located in the current directory:
[email protected]:~$ rm dellLaptopSerials.txt tom.txt dance.jpg
You can specify path too. If a file named dellLaptopSerials.txt located in /tmp/ directory, you can run:
[email protected]:~$ rm /tmp/dellLaptopSerials.txt
[email protected]:~$ rm /tmp/dellLaptopSerials.txt /home/vivek/dance.jpg /home/vivek/data/tom.txt

Ubuntu Linux delete a file and prompt before every removal

To get confirmation before attempting to remove each file pass the -i option to the rm command on Ubuntu Linux:
[email protected]:~$ rm -i fileNameHere
[email protected]:~$ rm -i dellLaptopSerials.txt

Force rm command on Ubuntu Linux to explain what is being done with file

Pass the -v option as follows to get verbose output on Ubuntu Linux box:
[email protected]:~$ rm -v fileNameHere
[email protected]:~$ rm -v cake-day.jpg

Ubuntu Linux delete all files in folder or directory

You need to pass the following options:
[email protected]:~$ rm -rf dir1
[email protected]:~$ rm -rf /path/to/dir/
[email protected]:~$ rm -rf /home/vivek/oldschoolpics/

It will remove all files and subdirectories from a directory. So be careful. Always keep backups of all important data on Ubuntu Linux.
Delete and remove files on Ubuntu Linux

Ubuntu Linux delete file begins with a dash or hyphen

If the name of a file or directory or folder starts with a dash (- or hyphen --), use the following syntax:
[email protected]:~$ rm -- -fileNameHere
[email protected]:~$ rm -- --fileNameHere
[email protected]:~$ rm -rf --DirectoryNameHere
[email protected]:~$ rm ./-file
[email protected]:~$ rm -rf ./--DirectoryNameHere

Do not run ‘rm -rf /‘ command as an administrator/root or normal Ubuntu Linux user

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Ubuntu Linux disasters. The rm -rf / variant of the command, if run by an administrator, would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands on Ubuntu Linux:
[email protected]:~$ rm -rf /
[email protected]:~$ rm -rf *

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Leave a Reply

Your email address will not be published. Required fields are marked *