This article is all about deleting a file and folder in the Linux operating system using the commands in the terminal. In this article, we will learn about how to delete a file and group of files using a single command and how to delete an empty directory and non-empty directory using a single command in the Linux operating system. Here you will study about the different type of methods to delete a file and directory in Linux operating system using rm and rmdir commands which are used in the deleting files and folders.
In the previous articles, we learn about how to manage files and folders in Linux operating system using different types of commands and graphical methods. This article is important for beginners who just switched to Linux from Windows. So let’s start now how to use rm and rmdir command in Linux operating system for deleting files and folders.
The Linux rm command
The rm is a Linux command which is used to delete files from a file system. And this command does not require write permission to delete the files and folder and when rm command used with the filename then it will delete all given files without confirmation by the user. The syntax to delete the particular files and directories is as follows:
rm filename rm [options] filename unlink filename rm -f -r filename
Here in the last syntax,
- -f is used to forcefully remove the file
- -r is used to remove the contents of the directories recursively
Uses of rmdir command in Linux operatin system
The rmdir command deletes an empty directory. The rmdir command only deletes the empty directory in the Linux operating system. And if you will try to remove a non-empty file using rmdir command then you will receive a prompt such as “rmdir:’dir’: Directory not empty”. The syntax of the rmdir command is as follows:
To remove a non-empty directory, you will use the following command syntax:
rm -r mydirectory
Here, you can replace mydirectory with your directory name. For example, if your directory name is satish then you would type the following command:
rm -r student
Above command would only delete all files and directories within the directory named(student) in the command. And it also shows a prompt for approval to delete each of the files. If you do not want to receive these prompt for each file then you can use the following command:
rm -rf student
Now you will study about the different type of method to delete the files in the Linux operating system.
♣ Command to delete a file named as student.txt
♣ Command to delete three files simultaneously which is named as a.txt, b.mp4, c.pdf.
rm a.txt b.mp4 c.pdf
♣ Command to request confirmation before attempting to delete each file
rm -i filename
♣ Dangerous rm command line
rm -rf / rm -rf *
The above command is very dangerous and if these command run by an administrator then it will delete the every file from your filesystem on the computer.
♣ Command to delete a file which name is started from dash, such as -a.txt
rm -- -a.txt
♣ Command to delete a directory which name is started from dash such as -student
rm -r -f -- -student
In the above two commands two dashes(–) tells rm command that the end of the option and the rest of the part is nothing but a file or directory name which begins with a dash.
♣ Command to delete a file which is stored within path/to folder
♣ For example, you have a file name as cat in the /home/document/animal folder then you would type the following command:
♣ Command to delete a specific extension file. For example, you want to delete all the files with extension .mp4 then you will type the following command:
These are the commands by which you can delete a file and folder in the Linux operating system. I think this article is helpful for you. If you have any queries and suggestions regarding this then simply share with us using the comment section. Also, provide feedback to us because your feedback is valuable for us. Follow our blog for further updates in the field of technology.