Learning to utilize the Linux command line is necessary for all Linux users and system administrators. It allows you to perform basic maintenance operations like creating new files, going to certain folders, and relocating existing ones.

Since Linux is a common operating system for hosting virtual servers, becoming familiar with its basic commands is essential for server administration.

This article will demonstrate how to use the Linux command line to delete a file in Linux.

How to delete a file in Linux?

You may quickly delete individual files, collections of files, or entire folders under Linux. However, it functions differently than it does in Windows.  Learn more about various Linux file removal techniques by reading this article.

Deleting files using a file manager

You may have Nautilus, Dolphin, or another Linux file manager on your machine, depending on the distribution and your desktop environment. 

You may delete Linux files in just a few easy steps by using these common file managers:

  • Locate the files you want to delete in the file manager
  • Select the files you want to delete while holding down the left mouse button
  • To access the context menu, right-click on one of the files. You might discover a menu item like "Move to Trash" or "Delete File", depending on your distribution.

Delete a file in Linux using the terminal.

It should come as no surprise that you may remove files in Linux using the command line, given the wide variety of commands accessible in the terminal. To do this, carry out the following actions:

  • After opening it, navigate to the files you wish to delete in the file manager. Alternatively, you can navigate directly to the files in the terminal by using the command "cd/path/to/the/file."
  • Open the context menu by right-clicking on the window and then selecting "Open in Terminal."
  • To remove one or more files, use the command "rm" (short for "remove").

How to use the rm command to delete a file in Linux?

Instead of immediately deleting a non-empty folder, you might wish to use the rm command to remove files manually. This approach is safer since it eliminates the unintentional removal of crucial files.

Use the rm command with the file name to remove a single file from the currently open directory:

rm file.txt

Insert all the file names you want to remove in the command to delete every file in the directory:

rm file1.txt file2.txt file3.txt

The above commands all function if you are in the same directory as the supplied files. 

To remove a file that isn't in your current working directory, you can include the file path in the command line parameter instead.

rm dir/subdir/file.txt

You can add the -i option to the command to have the terminal ask you to confirm each file deletion because the files will be permanently deleted. 

By doing this, unpleasant mistakes are avoided.

rm -i file1.txt file2.txt file3.txt

If you want to remove the file, type Y and hit Enter. If not, type N and press Enter. To only receive one prompt before deleting more than three files, use the -I switch instead. 

Although less secure than the -i option, this offers additional security to prevent the deletion of crucial files.

rm -I file1.txt file2.txt file3.txt

If files are write-protected, the system will prompt you before removing them. Use the -f option when deleting such files if you don't wish to be asked for confirmation.

rm -f file.txt

The commands we've covered up to this point will delete particular files. Wildcards, on the other hand, allow you to remove numerous files with a single command. 

The asterisk (*) and the question mark (?) are examples of wildcards. One of the most frequent uses of the asterisk, which stands for several unidentified characters, is removing files with a particular extension. 

The following command will eliminate every.txt file currently present in the working directory:

rm *.txt

The asterisk can also remove all files that start with a particular letter.

rm a*

The asterisk (*) in the previous example stands in for all unidentified characters that follow the letter a. Regardless of their extensions, files beginning with a, such as aaron.zip, alligator.png, and amazon.txt, will be deleted by this command.

On the other hand, the question mark wildcard only stands for one character. 

You can use it with the asterisk wildcard to remove files with single-character extensions like .S, .O, and .C.

rm *.?

How to delete a file in Linux by unlink command?

The unlink command doesn't receive as much attention compared to some of the other GNU Core Utilities. It just performs the task of deleting a file or symbolic link from the Linux file system.

The unlink command has the following syntax:

unlink filename

The name of the file you wish to delete is the filename. The command doesn't print anything and returns zero if it is successful. Two options are available for the unlink command: —help, which displays the command's help, and —version, which displays version information.

When deleting files with the unlink command, use additional caution since it cannot be fully retrieved once the file is removed.

Unlink can only accept a single parameter; hence it can only delete one file, unlike the more robust rm command. You will encounter the "unlink: additional operand" problem if you attempt to delete multiple files.

The file that a symbolic link points to is not deleted when a symbolic link is removed using unlink.

To remove it, you must have writing permissions on the directory where the specified file is located. You will receive an "Operation not permitted" message if you do not.

Conclusion

Linux includes several tools that can help us remove files. Based on a set of rules, we must constantly remove numerous files and directories. Knowing a few simple commands and their variations can help us do our job fast.

Linux command-line deletion of directories and files requires properly utilising the rm and rmdir commands. Simply put, the rm command removes files and non-empty directories, whereas the rmdir command only removes empty folders.

Remember that Linux does not have a recycle bin or garbage folder. Using the command line, deleting files and folders will result in their permanent deletion. 

Therefore, before deleting the files and directories on your VPS, use these commands carefully or make a backup.

People also read: