We need to delete the files in Linux to free up space. While deleting a file, we have several restrictions, such as deleting files with showing a confirmation prompt, deleting password-protected files, or removing all files from a directory which will be discussed in this article.
How to Delete Files in Linux?
Command Line and Graphical User Interface are used for deleting a file or multiple files in Linux:
- Delete Files in Linux Using Terminal/CLI
- Delete Files in Linux Using File Manager/GUI
Caution: The files removed by CLI cannot be recovered, but the files deleted by GUI can be retrieved from the “Trash Bin” in Linux.
Method 1: Delete Files in Linux Using Terminal/CLI
There are different ways of deleting a file in Linux utilizing the terminal. The command used for deleting the files in Linux is the “rm” command. This section will cover all the methods to delete files with examples. Let’s discuss the general syntax and options for the “rm” command.
General Syntax of rm Command in Linux
To delete/remove a file in Linux, the general syntax of the command is shown below:
$ rm [options] [filename...]
The “options” show the flags/Options available in the “rm” command, and “filename” must be replaced with the specified file/files names.
Options Available for rm Command in Linux
To check the available flags/options in the “rm” command, use the “help” option as shown below:
$ rm --help
Examples
Let’s delete/remove a file with the help of several examples.
How to Delete Single File in Linux?
A file can be deleted with a single “rm” command in Linux; we can list the files in a directory to “verify” the deletion of the file. To delete a file, execute the below command in the terminal:
$ rm testfile1.txt
How to Delete Multiple Files in Linux?
If you want to delete multiple files at once, use the “rm” command with its specified names (In this example, two files named “testfile2.txt” and “testfile3.txt”) as shown below:
$ rm testfile2.txt testfile3.txt
How to Prompt Before Removing File in Linux?
Do you have an important file and want to avoid it deleting accidentally? utilize the “i” flag of the rm command. To show a warning prompt/note before deleting a file “testfile4.txt”, use the below command in the terminal:
$ rm -i testfile4.txt
If you want to delete it permanently, write “yes” otherwise, “no”.
How to Remove a Protected File in Linux?
If you have a write-protected file and want to delete it with the “rm” command, it will show the below prompt (Due to file write-protect):
$ rm testfile5.txt
If you want to delete the write-protected file directly and don’t want to show the prompt, use the “f” option with the “rm” command as performed below:
$ rm -f testfile6.txt
How to Delete Specific Name Files in Linux?
To remove the files of a specific name (used “testfile7.txt”) in Linux, use the *rm [filenames]* command as shown below:
$ rm *testfile7.txt*
How to Delete Specific File Type in Linux?
To delete the specific type of files in a directory (in my case, “txt” file type), utilize the below command:
$ rm *.txt
How to Remove All Files in a Directory in Linux?
To delete all the files in a folder with a single command, use the “rm -r /completepath/*” command with the path of the specified directory as shown below:
$ rm -r /home/ubuntu/Desktop/*
Method 2: How to Delete Files in Linux Using File Manager/GUI
A file can be deleted/removed directly from the graphical User Interface (GUI) by performing the below steps:
1. Select a certain file or multiple files and press the mouse’s right button to show the list of options.
Shortcut Key: Select a certain file or multiple files and press the shortcut key “Delete” to directly delete that file.
2. Select the “Move to Trash” option to remove the selected file.
The output shows the selected file “testfile1.txt” is deleted from the system.
Conclusion
The “rm” command deletes a file in Linux. To remove a single file, use the “rm filename” command and the “rm filename1 filename2” command to delete multiple files. Moreover, different examples of deleting a file or several are discussed in the article.