Deleting A File In Linux

Deleting-a-file-in-Linux

We use files and directories too much in Linux. We can create files and directories, modify them or delete them during Linux administration. In this lesson, we will focus on deleting a file in Linux and we will learn Linux rm command and unlink command with different examples. In another lesson, we will focus how to remove a directory in Linux. You can also check it.

 


For more Linux Command, you can check also Linux Cheat Sheet!


 

Deleting a File With Linux rm command

The first option for deleting a file in Linux is using rm command. With rm command, we can delete a single file, multiple files, any files which has specific extension. Let’s give examples to learn rm command better.

 

We have five files, named abc1, abc2, abc3, abc4 and abc5. We have created these files with Linux touch command.

 

Firstly, we will delete the first file, abc1, with Linux rm command. Here, we will see deleting a file in Linux.

 

┌──(kali㉿kali)-[~]

└─$ ls

abc1  abc2  abc3  abc4  abc5  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos                                                                                                                          

┌──(kali㉿kali)-[~]

└─$ rm abc1                                                                                            

┌──(kali㉿kali)-[~]

└─$ ls    
abc2  abc3  abc4  abc5  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos  

 

Then, we will delete two files abc2 and abc3 with rm command.

┌──(kali㉿kali)-[~]

└─$ rm abc2 abc3                                                                                                                      

┌──(kali㉿kali)-[~]

└─$ ls         
abc4  abc5  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

We can also use rm -i option to be asked at each step for the confirmation. Let’s delete abc4 and abc5 with rm i- option, with a confirmation.

┌──(kali㉿kali)-[~]

└─$ rm -i abc4 abc5

rm: remove regular empty file 'abc4'? yes

rm: remove regular empty file 'abc5'? yes                                                                                                                                                           

┌──(kali㉿kali)-[~]

└─$ ls            

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

We can use rm -v option for listing the steps of the remove process.

──(kali㉿kali)-[~]

└─$ rm -v abc4 abc5

removed 'abc4'

removed 'abc5'   

We can also use rm -f option to force the delete process if the file is write protected.

 

Beside that, we can also delete a specific file type in Linux. To do this, we use asterisk (*) before the extension. Below, we will delete all txt files under the directory.

 

┌──(kali㉿kali)-[~]

└─$ rm *.txt

 

We can also delete all the files under a folder with a specific command. Below, we will delete three files under a folder but not the folder itself with Linux rm command.

┌──(kali㉿kali)-[~]

└─$ tree abc

abc

├── xxx

├── yyy

└── zzz

1 directory, 3 files                                                                                                                                 

┌──(kali㉿kali)-[~]

└─$ rm abc/*

zsh: sure you want to delete all 3 files in /home/kali/abc [yn]? y                                                                                                                                                       

┌──(kali㉿kali)-[~]

└─$ tree abc

abc

0 directories, 0 files

 

Deleting a File With Linux unlink command

We can also use an alternative command for deleting a file in Linux. This alternative way is using Linux unlink command. Like rm command, we can use unlink command with file name. But this time there are limited options.

 

As you can see below, we can delete only one file at a time with Linux unlink command.

┌──(kali㉿kali)-[~]

└─$ unlink abc1 

Linux unlink command is not used to remove a directory because it can not do this.


 

Conclusion

Here, we have learned deleting a file in Linux with two different Linux commands. We have deleted files with rm command and unlink command. You will use these commands in your Linux administration period too much.

 


Full Kali Linux Course!


 

Back to: Kali Linux Course

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Blog Posts