Delete/Remove File In Linux

Files can be removed or deleted by using different commands in Linux. The removal of the file deletes related file system information from the file system table. While deleting a file there are different ways like prompt deletion, display verbose output, etc.

Delete/Remove File with rm Command

The rm command is a defacto command to delete and remove files in Linux and Unix operating systems. The rm command syntax is like below.

rm FILE
  • FILE is the file name we want to delete.

In the following example, we remove the file named “main.txt” file.

$ rm main.txt

Delete/Remove File with unlink Command

Another less known command to delete or remove a file is the unlink . It simply removes the inode descriptor and data on the file system. The unlink command syntax is like below.

unlink FILE
  • FILE is the file name we want to remove.

In the following example we remove the file named “main.txt”.

$ unlink main.txt

Delete/Remove File Forcibly

In some cases, the file we want to remove can be read-only, or some restrictions to remove in a regular way. We can remove the file forcibly by using the -f option.

$ rm -f main.txt

Delete/Remove File Interactively

By default, the rm command deletes the provided file. But we can make things more interactive by using the -i option. This option request confirmation for the deletion of the specified file.

$ rm -i main.txt

Delete/Remove Multiple Files

The rm command can be also used to remove multiple files. Just put file names after the rm command and options if exist. In the following example, we remove files named “main.txt”,”cities.txt”,”users.txt”.

$ rm main.txt cities.txt users.txt

Delete/Remove Files with Specific Extension

The rm command can be also used to remove files with specific extensions. In the following example, we delete all files with *.txt extension.

$ rm *.txt

Alternatively, we can provide multiple extensions to remove.

$ rm *.txt *.db

Delete/Remove Files Verbosely

By default, the rm command does not display any output unless there is not an error. We can print verbose output about the successful removal of the files. The -v option is used to verbose deletion.

$ rm -v *.txt
Delete/Remove Files Verbosely

Leave a Comment