1. To delete a single file, use the rm command followed by the file name:
$ rm filename
2. To delete multiple files at once, use the rm command followed by the file names separated by a space:
$ rm filename1 filename2 filename3
3. To delete all files in a directory, use the rm command followed by the -r option and the directory name:
$ rm -r directoryname
4. To delete all files in the current directory, use the rm command followed by the -f option:
$ rm -f *
5. To delete a directory and all of its contents, use the rm command followed by the -r option and the directory name:
$ rm -r directoryname
Delete and remove files on ubuntu linux using terminal. In this tutorial, you will learn how to delete and remove a file on Ubuntu Linux based system using 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 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 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.
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 terminal:
- 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!
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 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
Use the following command with following options 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 *
Conclusion
Delete and remove files on ubuntu linux using terminal. In this tutorial, you have learned how to delete and remove a file on Ubuntu Linux based system using terminal or command prompt.