How to Delete a File on Linux

How to Delete a File on Linux

In this tutorial, we’re going to show you how to delete a file on Linux. This tutorial is meant for beginners, with step-by-step instructions on how to delete a file in Linux with different examples and use cases. We learned how to move a file before, now we’re going to learn how to delete a file.

First things first, if you’re using a graphical user interface, you can delete a file just by using the Delete button on your keyboard – select the file and hit the Delete button. You can also right-click on the file and delete it via the small popup window. The option will probably say something like “Move to Trash”, “Remove” or “Delete”.

To delete a file via the CLI (terminal), which is useful for servers, we’re going to use the rm command. You may already be familiar with the rm command.

Delete a single file

To delete a single file, just enter the name of the file you want to delete:

rm fileName.txt

If you’re in a different directory than the file you want to delete, you can specify the path to the file:

rm /path/to/fileName.txt

Delete multiple files

You can also delete multiple files at once. You just need to enter their file names separated by a space:

rm fileName.txt anotherFile.txt thirdFile.mp3

Delete multiple files with a similar name or extension

To delete multiple files that contain a certain keyword, you can use the command below:

rm *file*

This command will remove all files that contain the word “file” in them.

To delete multiple files with the same extension, use the command below:

rm *.txt

This command will remove all the files that have the .txt extension.

The * wildcard can be used in different ways too.

Useful flags for the rm command when deleting a file

If you want to get a prompt asking you if you want to remove the file before actually removing it, use the -I flag. Example:

rm -i fileName.txt

By using the -i flag, you’ll have to confirm every deletion of a file by pressing enter when prompted.

To do the opposite of the -i flag (ie. force the removal), use the -f flag. In this case, the file will get deleted and the terminal won’t prompt you with anything. Example:

rm -f fileName.txt

You can use the -v flag to let the terminal show you what’s being done when running the rm command. Example:

rm -v fileName.txt

This command will output:

removed 'fileName.txt'

You can use these flags with all examples in this tutorial.

How to delete a file that starts with –

One specific example that beginners have trouble with is deleting a file start starts with – with the rm command. To delete a file that starts with -, you can use either of the following commands:

rm -- -fileName.txt

rm ./-fileName.txt

These commands will delete the file “-fileName.txt”

Conclusion on deleting a file in Linux

As you can see, deleting a file in Linux is pretty easy, and with the rm command and wildcards, you can do a lot of useful stuff. You can also delete directories with the rm command, but we’ll write another tutorial dedicated to deleting directories soon.

Run the following command to learn more about the rm command:

rm --help

Leave a comment

Your email address will not be published.