How to Delete a Directory or File in Linux
Deleting directories and files is one of the most basic yet important functions of any operating system. On Linux, if you have a windowed environment running, you can locate and delete files using a file manager application. Maybe you’re logging in remotely via SSH, your Linux machine doesn’t have a GUI installed, or you want more control over what you’re deleting. As with everything in Linux, there are many reasons why it may be necessary or preferable to delete directories or files via the command line.
In this tutorial, we will examine the rm command that deletes directories and files. We’ll look at the basics of the command and how it can be used to delete both files and directories and we’ll also look at a few other enhancements to get the most out of this command. If you want to change your file location instead, you should check out our article on how to move and rename files in linux.
Of course, as this guide is about deleting directories and files, you have to be very careful to follow the instructions carefully, and while you get used to these commands, it’s good to work with sample files and empty directories. .
All commands in this tutorial will work on most Linux machines. We used an Ubuntu 20.04 install, but you can run this guide on a Raspberry pie. All procedures are performed through the terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt, and you or via the Applications menu.
How to delete a file in Linux
Let’s just start by deleting a single file. The command syntax is structured as follows.
1. Create a new file called test1.txt
2. List directory contents to make sure the file was created. You should now see the test1.txt file.
3. Delete the test file. Deleting a file or folder is permanent. There is no trash can that can save us from a wrong keystroke.
4. Re-list the contents of the directory to verify that the file was deleted. You should see that test1.txt is no longer listed.
How to Delete a Directory in Linux
Let’s walk through a similar example to show that rm can be used to delete a directory containing files. For these examples we will use two additional arguments -r and -I.
1. Create an empty directory. From your home directory, create an empty directory.
2. Move to the new directory.
3. Create two test files in the directory
touch test1.txt test2.txt
4. List directory contents to verify that the files are created. You should see the files test1.txt and test2.txt are now listed.
5. Return to home directory
6. Delete the directory. To delete this directory, we must use the -r argument. the -r The argument acts recursively to enter the directory and find and delete all files in it. Once empty, the command returns to remove the directory it just cleared.
rm -r test_directory
7. Verify that the directory was deleted. you should see this test_directory has been removed from the list.
8. Repeat steps 1-5 above to create the test directory and files.
9. repeat it -r argument but add -I argument to interactively delete files and directory. It can be hard to imagine what -r the argument made. the -I The argument forces the deletion process to ask for (interactive) confirmation of each deletion. Type “y” and click Enter to confirm each step.
rm -ri test_directory
How to Delete a Write Protected File in Linux
Sometimes you may need to delete files in Linux that are write protected. If you are using the standard rm command, it will ask you to confirm whether you want to delete each protected file. For this we can use the -F force the argument to help. This essentially forces the rm command to forcibly delete all directories and files, so you have to be very careful when using these commands because it won’t ask for any confirmation.
1. Create a test file called test1.txt
2. Change file permissions so that only the user and the group they belong to can delete the file.
chmod u-w test1.txt chmod g-w test1.txt
3. Try deleting the file using standard rm order. When using standard rm command, you should be prompted to confirm if you want to delete the test1.txt file. Click on not for “no” this time. If you had a directory with hundreds of protected files, you would confirm each one with a there which would be an incredible waste of time.
4. Delete the file using the -F argument. Run this command with -F The argument forces the command to delete the file and does not ask for confirmation. Use with caution!
rm -f test1.txt
These commands are incredibly powerful, simple to use, and can make removal tasks very easy to perform. You may have seen jokes online about extremely dangerous versions of this command such as sudo rm -rf / and you may get new insight via this article into why it could be so dangerous.
You may notice that we give the command root privileges and use the force and recursive arguments to essentially send the command to remove all files from a system!