Hi guys, welcome back and today we will discuss how to delete files in Linux and what are different ways to delete files and directories in Linux.
To delete files in Linux we use command-line options such as rm and unlink which will permanently delete files. You cannot restore them once you have deleted them unless you have a backup of them.
Table of contents –
- Delete files in Linux using the rm command
- Remove a directory in Linux
- Using unlink command to delete files in Linux
- Frequently Asked Questions
rm (remove) command is a Unix/Linux command which is used to delete files from a filesystem.
The rm command is used to delete files from Linux directories but there are some points to be noted down before using the rm command. Note these points because you may end up losing all your files if rm is used improperly.
- rm deletes each specified file.
- You can delete multiple files using the rm command.
- rm cannot delete the Linux directories and does not work on directories that contain files.
- Deleting a file in Linux using rm command requires the write and execute permission.
- rm never ask for confirmation while deleting a file. Make sure you follow the right steps while deleting a file.
We can also use the unlink command to delete files in Linux and we shall see examples about the same. Here, we are considering two files, file1.txt and file2.txt as an example to show how to delete files in Linux.
Delete files in Linux using the rm command –
The rm command is used in many ways to delete files and we will learn about each of the rm associated commands using the examples.
To use the rm command, open the terminal in that particular directory, and use the following commands as per your needs.
1. Deleting a single file using rm –
To delete a single file using the rm command, simply use rm with the filename.
This will delete the file, file1.txt. You can also specify the path to delete the file.
2. Deleting multiple files using rm –
At times, there are too many files that you want to delete, and at that time you can use rm to delete multiple files.
Here is an example,
$rm file1.txt file2.txt
The above command deletes two files (file1.txt and file2.txt) using the rm command in Linux.
3. Delete all files with the same extension –
To delete all files in that particular directory, open the terminal in that directory and type the following command.
The above command will delete all files which have a txt extension. You can replace “txt” with ZIP, jpg, or any other extension of files available in that directory.
There are many options available with the rm command which makes deleting the file easier in Linux type OS.
4. Using the interactive mode in rm –
We know that rm does not ask for confirmation while deleting a file but we can use interactive mode to confirm before deleting.
To use the interactive mode with rm command, append ‘-i’ with rm command followed by the filename.
Here is the example,
$rm -i file1.txt
The above command will ask for confirmation before deleting the file. This should usually be practiced to prevent accidental deleting of files.
5. Using force mode to remove without prompting –
If you are sure to remove the file and don’t want confirmations then you can use the force command.
To use the force option, append ‘-f’ with rm followed by the filename.
Here is an example,
$rm -f file1.txt
6. Get the report of each file after deleting –
Generally, Linux users are too cautious about what kind of file existed and how much space it acquired, and other additional information. Once the file is deleted, you cannot know this information later. To solve this problem, you can use ‘-v’ to show the report of each file removed.
Here is a quick example,
$rm -v file*.txt
Using the above command you get the report of each file.
You can use the following rm command to explain what is being done with the file.
$ rm -v moiz.list.txt bios-updates.doc
This command will show all the operations which are done on the files while removing them (Just like a log file).
You can access the manual page of rm here
Remove a directory in Linux –
To remove a directory in Linux using the rm command, you need to ensure that the directory is empty. You can remove the directory by using the following command.
$rm -d filenames/
You can also remove an empty directory using the rmdir command instead of the rm command.
The above command will remove an empty directory.
To delete a directory that has files within it, you can use the recursive mode and delete the directory along with the content.
Here is a quick example to remove a directory in Linux which is non-empty,
$rm -r filenames/
The above command will remove the directory along with files within it.
You may find it difficult to confirm prompt every time you want to delete a huge number of files. The rm command can combine the options so it becomes easy to delete files in Linux.
Here is an example of how you can combine rm options to delete files in Linux,
$rm -rf filenames/
The above command can be used to remove a directory in Linux along with its content and without a confirmation prompt.
To remove multiple directories in Linux, use the following command –
$rm -r dirname1 dirname2 dirname3
Here, dirname signifies the name of the directory.
Never use rm -rf as the root user or superuser on your Linux operating system. This command will delete all the writable files on your computer and crash the OS.
Never use these commands –
$rm -rf / $rm -rf *
The unlink command is not that powerful as compared to the rm command. You get only two options with the unlink command, –help option and –version option.
Also, the unlink command cannot delete multiple files and directories like the rm command. You can delete only a single writable file using this command.
To delete files in Linux using unlink command, use the following code in the terminal.
I am sure that with our step-by-step guide you are now able to delete files in Linux and it is much easier now. Prefer using the rm command over unlink, it is more powerful and clean in comparison. Make sure you take care of points noted in the first section while discussing the rm command. You cannot recover the file once you delete it so it is recommended to take a backup copy of your file if it is too important. One last thing about using the rm command is, do not use it as the root user or superuser with ‘/’ or ‘*’, you can end up deleting the whole operating system.
You may also like,
- Best Ethernet Cable for High-Speed Internet Bandwidth
- 7 Best Shortlisted Android Emulators For PC 
- How to Start, Stop and Restart Apache Ubuntu?