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:
Open the Ubuntu terminal application (bash shell)
Type any one of the following command to delete a file named ubuntu.nixcraft.txt in the current directory
rm ubuntu.nixcraft.txt OR 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: 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 *
This entry is 4 of 8 in the Linux Delete/Remove Files Tutorial series. Keep reading the rest of the series:
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.