Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

How to Delete and Remove Files on Ubuntu Linux Terminal

Home  »  Ubuntu • How to • SSH   »   How to Delete and Remove Files on Ubuntu Linux Terminal
Posted on September 13, 2022September 17, 2022
998

Delete and remove files on ubuntu Linux using the terminal. In this tutorial, you will learn how to delete and remove a file on Ubuntu Linux-based system using a terminal or command prompt.

This tutorial will 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 tutorial will guide you on how to delete and remove files on Ubuntu Linux with a terminal using the rm command.

The rm command removes files on Ubuntu Linux. The rm command options are as follows:

  • -f: Remove read-only files immediately without any confirmation.
  • -i: Prompts end-users for confirmation before deleting every file.
  • -v: Shows the file names on the screen as they are processed/removed from the filesystem.
  • -R OR -r : Removes the given directory along with its sub-directory/folders and all files.
  • -I: Prompts users every time when an attempt is made to delete for than three files at a time. It also works when deleting files recursively.

This tutorial will explain all the options for the rm command one by one below.

Commands to delete and remove files on Ubuntu Linux

Just follow the rm and unlink command to remove files on Ubuntu Linux using the terminal:

  1. Open the Ubuntu terminal
  2. Type any one of the following commands to delete a file named hello.txt in the current directory
  3. rm hello.txt
    OR
    unlink hello.txt

WARNING: Do not type sudo rm -R / or sudo rm -r / or sudo rm -f /* or sudo rm --no-preserve-root -rf / as it removes all the data in the root directory. Avoid data loss and you should not execute them!

Command to delete multiple files on Ubuntu Linux

Use the following command to delete the file named hello.txt, my.txt, and abc.jpg placed in the current directory:

rm hello.txt my.txt abc.jpg

You can specify the path too. If a file named hello.txt is placed in the /tmp/ directory, you can run:

rm /tmp/hello.txt
rm /tmp/hello.txt /home/html/my.txt/home/html/data/abc.jpg

To delete a file and prompt before every removal in Ubuntu Linux

To get confirmation before attempting to remove each file pass the -i option to the rm command on Ubuntu Linux:

 rm -i fileNameHere
 rm -i hello.txt

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

Pass the -v option as follows to get verbose output on the Ubuntu Linux box:

 rm -v fileNameHere
 rm -v cake-day.jpg

To delete all files in a folder or directory in Ubuntu Linux

Use the following command with the following options to delete all files in the folder or directory in Ubuntu Linux:

 rm -rf dir1
 rm -rf /path/to/dir/
 rm -rf /home/html/oldimages/


The above-given commands will remove all files and subdirectories from a directory. So be careful. Always keep backups of all important data 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:

 rm -- -fileNameHere
 rm -- --fileNameHere
 rm -rf --DirectoryNameHere
 rm ./-file
 rm -rf ./--DirectoryNameHere

Do not run the ‘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:

 rm -rf /
 rm -rf *

Conclusion

Delete and remove files on ubuntu Linux using the terminal. In this tutorial, you have learned how to delete and remove a file on Ubuntu Linux-based system using a terminal or command prompt.

Ubuntu, How to, SSH

Post navigation

Previous Post: Enable or Disable SSH Root Login Access in Linux
Next Post: How to Delete Unused Database Tables in WordPress

Related Posts

  • How to install Git on Linux
  • How to change password in the CodeIgniter framework
  • How to fix getFullyear() is not a function with JavaScript?
  • How to use :hover to modify the CSS of another class?
  • How to Recover Deleted WhatsApp Messages
  • How to Delete Files in Ubuntu Command Line

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

How to display the last updated date of a post in WordPress
4 Methods to Search an Array
Dealing with deprecations
How to Recover Deleted WhatsApp Messages
How to change the link color of the current page with CSS?

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved