In this guide, Quantrimang.com will help readers learn how to delete files and folders using the command line on Linux. This guide is compatible with all Linux distributions, so it works the same way on Ubuntu, Debian, CentOS, AlmaLinux, Rocky Linux, etc…
Delete a file on Linux
In Linux, the rm command is used to delete files and directories. Navigate to the specific folder containing the file you want to delete. Specify the location otherwise the command will start searching in the current working directory. For example, the article author has a file in the directory /tmp/ want to delete. To delete the desired file, open Terminal and enter the following command:
# rm file.txt

Be careful while files and folders from Linux because once deleted, they cannot be recovered. So it will ask you to confirm before deleting the file:
# rm –i file.txt

If you don't want the deletion confirmation message, use the following command:
# rm –f file.txt

A confirmation message will not appear.
Delete multiple files on Linux
To delete multiple files on Linux, the same rm command can be used.
# rm file.txt file1.txt file2.txt

Delete folders on Linux
To delete a folder on Linux, the same command is used. But you need more options -r and -f to delete a folder.
# rm –rf /data

But be careful, this will delete the entire folder, including all files and folders inside. You can use the above command without -fas it will not prompt for confirmation. Options -r used to delete directories.
If you just want to delete an empty directory, use this command instead:
rmdir /data
The command will show an error in case the directory is not empty.
Summary
In all Linux distributions, the rm command is used to delete files and folders.
- If -i is used with rm, you will be prompted to confirm before deleting.
- If -r used with rm, the directory will be deleted without confirmation.
See more: