Linux is one of the best known and the most widely used open-source operating system. It works completely different from Windows and Mac. It also includes deleting a file or directory. If you are wondering how to delete a file or directory in Linux OS, here are the steps to do it. To delete a file in Linux, you can use two different ways.
- Using GUI
- Using Commands.
Contents
- Delete a File Using GUI
- Delete File/Directory Using Commands
- Syntax to Delete a File in Linux
- Delete a Single file
- Delete Multiple Files
- Delete Files in the Directory
- Delete a Directory
- Delete a Directory and its Contents
- Delete an Empty Directory
- Delete Multiple Directories
- Delete Files and Directories (non-empty)
- Delete Write Protected file
Delete a File Using GUI
It is the most common and simple method of deleting files in all operating systems. Graphical User Interface (GUI) tools are used to delete any files and directories from the system directly. You can delete a file by either clicking the file and then press the delete button on the keyboard or right-click the file and select Delete.
The advantage of using Graphical User Interface (GUI) is that the deleted files and directories will move to the trash folder and can be recovered any time unless you permanently delete them.
Delete File/Directory Using Commands
In Linux, you can also delete files using the command in the Linux terminal or shell. With commands, you can delete files that are not possible via the graphical method. The files or directories that are deleted using the command cannot be recovered easily.
Syntax to Delete a File in Linux
There are various types of syntax available in order to delete files and directories in Linux. Here are some command expressions you need to know.
- rm (or) unlink – used to delete a file
- .txt – Any text written after dot are considered as file format like .jpg, .png, .pdf, etc.
- *– to denote all directory files and it will select all the files or formats enter after it.
- -r – Recursive
- -f – force delete
- -d (or) rmdir – empty directory
- Dir 1,2,3,… – name of the Directory
- Filename(s) 1,2,3,… – name of the file
Delete a Single file
The command to delete a single file from the current directory is
rm filename.txt
unlink filename
Command to delete a single file from a different directory
rm ./path/to/the/file/file_1.txt
Delete Multiple Files
To delete more than one file at a time, use the following command.
rm filename1.txt filename2.txt filename3.txt
Delete Files in the Directory
You need to use * to delete all files in the directory. For instance, if you want to delete all .txt files in a directory, type
rm *.txt
Instead of .txt, you can use any file format like .jpg, .png, .pdf, etc to delete the respective files.
Techowns Tip – Simple ways to create a file in Linux using the command line.
Delete a Directory
rm -d filenames/ is the command line to delete a directory.
rm -d filenames/
Delete a Directory and its Contents
The command line to delete the non-empty directories and their files recursively
rm -r filenames/
Delete an Empty Directory
This command line is used to delete an empty directory file.
rm -d Dir1
rmdir filenames/
Delete Multiple Directories
The command line to delete multiple directories recursively is
rm -rf Dir1 Dir2 Dir3
Delete Files and Directories (non-empty)
The command line to delete the file and directories even if it is non-empty.
rm -rf filenames/
Delete Write Protected file
While performing the deletion, if you see the “rm: remove write-protected regular file (along with the command)” message, respond to that by entering the “y” letter and then hit the enter key to delete the file.
These are some of the ways to delete a file in Linux. By default, Linux will not show a prompt for confirmation before deleting the files. While using the line command for deletion, you need to be careful while entering the filename. If you are still hanging on with any queries, make use of the comment section.
Visit our Facebook and Twitter profiles to read our guides instantly.