As already we have discussed how to create a file or directory in Linux or Unix systems. Now if you want to remove them then Linux provides a separate command which is known as rm and it stands for remove.
The rm command is used to remove the files, directories, and links from a Linux system. In this article, we will discuss the usage of the rm command along with some examples.
Syntax of the rm command
The syntax of the rm command is given below –
rm [options] [file_name]
You can use the following options with the rm command.
How to delete a file in Linux
By default, the rm command deletes a file when used without any options means it doesn’t remove directories. Now use the following command to delete a file –
rm file_name
For example-
rm testfile.txt
The command will remove testfile.txt
from the current working directory of the user.
sudo
with the command[/alert]Deleting a file using the option -i
with rm command
The option -i
enables the interactive deletion of the file. It will prompt you that “rm: remove regular file ‘testfile.txt’? ” you can confirm it by entering yes or no.
Now use the following command if you want to delete the file in interactive mode –
rm -i testfile.txt
Deleting a file forcefully
You can use the -f
option to delete a file forcefully from your system. This will ignore the nonexistent files and arguments and never prompt. To delete a file forcefully use the following command in your terminal –
rm -f testfile.txt
Deleting multiple files
You can delete multiple files using a single command. You need to pass all the filenames as the argument. Use the command as given below to remove the files from your system –
rm file1.txt file2.txt file3.txt
How to delete a directory from a Linux system
To delete one or more empty directories you need to use the following command in your terminal –
rm -d dir_name(s)
For example –
rm -d dir1
This command will remove the empty directory dir1.
If you want to delete the directory that contains files or directories then you will have to use the following command in your terminal –
rm -r dir1
Where dir1
contains some files or directories in it.
If you want to see more options and usage of the rm command then see its manual page in your terminal by using –
man rm
Conclusion
Now you know how to delete a file or directory from a Linux or Unix system. If you have any query then leave them in the comments below.