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!
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.
Then, we will delete two files abc2 and abc3 with rm command.
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.
We can use rm -v option for listing the steps of the remove process.
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.
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.
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.
Linux unlink command is not used to remove a directory because it can not do this.
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.
Leave a Reply