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!
Leave a Reply