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.

Command to delete and remove files on Ubuntu Linux

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
    OR
    unlink ubuntu.nixcraft.txt

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

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!

Delete multiple files on Ubuntu Linux

Type the following command to delete the file named dellLaptopSerials.txt, tom.txt, and dance.jpg located in the current directory:
vivek@nixcraft:~$ 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:
vivek@nixcraft:~$ rm /tmp/dellLaptopSerials.txt
vivek@nixcraft:~$ 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:
vivek@nixcraft:~$ rm -i fileNameHere
vivek@nixcraft:~$ 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:
vivek@nixcraft:~$ rm -v fileNameHere
vivek@nixcraft:~$ rm -v cake-day.jpg

Ubuntu Linux delete all files in folder or directory

You need to pass the following options:
vivek@nixcraft:~$ rm -rf dir1
vivek@nixcraft:~$ rm -rf /path/to/dir/
vivek@nixcraft:~$ 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.

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:
vivek@nixcraft:~$ rm -- -fileNameHere
vivek@nixcraft:~$ rm -- --fileNameHere
vivek@nixcraft:~$ rm -rf --DirectoryNameHere
vivek@nixcraft:~$ rm ./-file
vivek@nixcraft:~$ 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:
vivek@nixcraft:~$ rm -rf /
vivek@nixcraft:~$ rm -rf *

Conclusion

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 being processed/removed from the filesystem.
  • -R OR -r : Removes the given directory along with its sub-directory/folders and all files.
  • -I : Prompts users everytime when an attempt is made to delete for than three files at a time. Also works when deleting files recursively.

See rm command man page by typing the following man command:
% man rm
% rm --help


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz