Deleting a file in Linux using commands with examples

Deleting a file in Linux based operating system is a tricky task. While in any other operating system like windows or mac os, it will require simple steps to delete a file from the system.

There are so many ways by which you can delete a file in Linux. The first one is, you can delete a file by the graphical user interface. and there is no need for any commands.

Another one is the command-line tool which is more efficient and will give you a real feel of the Linux operating system.

If you delete a file graphically then the operating system will not delete it permanently and the file will go into the trash and someone can recover it.

But if you want to delete a file permanently then you need to use the command-line tool along with some specified command.

There are a couple of commands available in Linux by which you can delete a file with the help of the command-line tool.

With the help of the rm command, you can delete multiple files at once while the unlink command only deletes a single file at once.

Syntax of rm and unlink command :

~$ rm file_name

~$ unlink file_name

If the file you specified earlier is write-protected then you will see a warning prompt like this.

Output :

~$ rm: remove write-protected regular empty file ‘file_name’ ?

In this case, you just need to type y ( for yes ) and press enter then it will delete your file from the system.

Also check :   How to find a file in Linux command with examples

rm command

The rm command will be used for deleting multiple files from the system. You just need to specify the file names separated by space.

The syntax will look like this:

~$ rm file_1 file_2 file_3

If you want to delete more than a hundred same types of files at once then you don’t need to write the names of files a hundred times along with your rm command. You just need to use the power of wildcards(*) or maybe some specified regular expression.

For Example :

Suppose you want to delete all the pdf files stored in the Document directory then your command will look like this.

~$ cd Document
 /Document~$  rm *.pdf

This command will delete all the pdf files at once.

Some options that will be used along with rm command:

  • If you want to confirm each file before deleting it then you need to -i option along with the rm command.
            ~$ rm -i file_name (or multiple files)
  • If your file is write-protected and you don’t want to see the prompt then you have to pass -f option along with the rm command.
            ~$ rm -f file_name
  • You can also combine two or more than two options along with the rm command.
 ~$ rm -fv *.pdf

This command will delete all the pdf files without prompting in verbose mode.

unlink command

The unlink command mainly used for deleting a single file from the current directory.

~$ unlink file_name
  • If you want to confirm each file before deleting it then you need to -i option along with the unlink command.
            ~$ unlink  -i file_name
  • If your file is write-protected and you don’t want to see the prompt then you have to pass -f option along with the unlink command.
            ~$ unlink -f file_name
  • You can also combine two or more than two options along with the unlink command.
 ~$ unlink -fv Example_1.pdf

This command will delete the example_1.pdf file without prompting in verbose mode.

Also check :   How to copy directory in Linux tutorial with examples

Things to consider while deleting a file in Linux

First of all, be extra careful when you are deleting a file from the system. Because the file will delete permanently and no one can recover it.

So make sure that you write the file name correctly because if you do not write the file name correctly then you may end up with deleting a wrong file.

You will get an error message that will indicate to you that you made a typing mistake in writing the wrong directory path or the wrong file name.

Conclusion on deleting a file in Linux

In this article, we have learned how to delete a file or multiple files in Linux using a command-line tool.

Basically both the commands do the same operations but the difference is the rm command will be suitable for deleting multiple files at once using a regular expression or using some wildcards.

You can also delete files using a graphical user interface but this is not the good practice. If you don’t have prior experience of using command-line tools then you may face some difficulty in Linux.

Now you have gained enough knowledge and a good understanding of deleting commands.

Taking precautions while deleting a file is a very important aspect, so make sure you write the file name and the directory path correctly.

Get occasional tutorials & guides in your inbox. No spam ever.


Buy Me A Coffee

Leave a Comment