Linux is an open-source operating system that supports multiple file-creation methods. Just like creating files, it is very easy to delete files as well. We can delete single, multiple files and entire folders with the help of the linux command line terminal.
This guide will give you a detailed description of how to delete single and multiple lines in the Linux terminal.
How do you delete a file in a Linux terminal?
- Using rm command
- Using unlink command
Let’s discuss them one by one.
Method 1: How To Delete a File In Linux Terminal Using rm Command
The rm command enables users to delete files easily through the terminal.
The syntax of the rm command is
$ rm path_to_file/filename
For instance, let’s delete a file named file2.txt present in the home directory.
It can be seen that the file is deleted successfully.
Let’s delete multiple files using the rm command.
The syntax is as follows:
$ sudo rm filename1 filename1 filename3..
It can be seen that the cursor moved to the next line so the files have been deleted successfully.
Method 2: How To Delete a File In Linux Terminal Using unlink command
The syntax of the unlink command is listed below:
$ unlink filename
The file has been deleted successfully.
How To Delete Folders in a Linux Terminal?
To delete empty directories, we use a command known as rmdir. We can also use rm -d to delete empty directories or folders.
The syntax is shown below:
$ rm -r dirname
or
$ rmdir dirname
Method 1: How To Delete an Empty Directory Using rm Command
Let’s delete an empty directory in our Linux system.
It can be seen an empty folder named Files is present in our Linux system.
As the cursor moved to the next line, the command is executed and the folder is deleted successfully.
Method 2: How To Delete a Non-Empty Directory Using rm Command
Let’s delete a non-empty directory using the rm command.
The syntax is listed below:
$ rm -r dirname
Let’s delete multiple folders using the rm command:
The syntax is shown below:
$ rm -r dirname1 dirname2 dirname3
For instance, let’s delete the three folders named Folder1, Folder2, and Folder3.
As the cursor moved to the next line, the command is executed and the folder is deleted successfully.
From the above screenshot, we can see that all three folders are deleted successfully.
Conclusion
To delete a file in a Linux terminal, we can use the rm and unlink commands. To delete a single file, run the rm path_to_filename command, and to delete multiple files, run the sudo rm filename1, filename2… command. To delete folders in a Linux terminal, run the rm -r dirname command.