Delete files on ubuntu linux using terminal. In this tutorial, i am going to show you many methods on how to delete files on Ubuntu Linux based system using terminal or command prompt.
Delete/remove files on Ubuntu Linux using Command
Use the rm and unlink command to delete files on Ubuntu Linux using terminal. So, let’s follow the following steps:
- Open the Ubuntu terminal
- Type any one of the following command to delete a file named hello.txt in the current directory
- 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!
Delete multiple files on Ubuntu Linux using Terminal
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 path too. If a file named hello.txt placed in /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 file
Pass the -v option as follows to get verbose output on Ubuntu Linux box:
rm -v fileNameHere rm -v cake-day.jpg
To delete all files in folder or directory in Ubuntu Linux
To delete all files in 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 ‘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 *
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.
Conclusion
In this tutorial, i have shown to you how to delete and remove a file on Ubuntu Linux based system using terminal or command prompt.