In the previous chapters of the Terminal Basics series, you learned to create new files and directories (folders).
Now let’s see how you can delete files and folders in Linux terminal.
To delete files, you can use the rm command as follows:
You will not see any output if the file is deleted successfully.
Here is an example where I deleted one of the files named
new_file. When I list the contents of the directory you can see that
new_file does not exist anymore.
You can also delete multiple files in the same command:
rm file1 file2 file3
Let me show an example of deleting two files in one command.
🏋️Exercise file deletion
Let’s put into practice what you have just learned. Create a directory named practice_delete and switch to it:
mkdir practice_delete && cd practice_delete
Now create some empty files:
touch file1 file2 file3
Now let’s do something more. Run this command and change the permission on file2:
chmod u-w file1 file2
Try removing file2 now:
Do you see a message ‘delete write-protected file‘? This is because you have removed write (modify) permission from this file.
You can press Y or the Enter key to confirm the deletion or N to reject the deletion.
If you don’t want to see this message and still delete it, you can use the force delete option
-f. Try removing
rm -f file1
Here is a replay of all the examples above to help you:
There is no recycle bin in the Linux command line. Once the file is deleted, you cannot undo the action to bring it back from the trash like you do in the graphical file manager. For this reason, be very careful while deleting files.
Remove but with caution
The absence of a trash can makes deleting a permanent job of sorting. This is why you need to be careful about the files you delete.
There is an interactive mode with option
-i. With this, you will be asked to confirm the deletion.
rm -i filename
This is useful when you delete multiple files based on a certain pattern.
Here is an example where I interactively delete all files that match file_pattern in their name. I delete some of them and keep others in interactive mode.
I advise changing to the directory where the files are located and then deleting them. This helps reduce any potential caused by a typo in the file path.
There is a dedicated rmdir command for deleting directories in Linux.
However, it can only remove empty directories. If the directory contains files or subdirectories, the rmdir command generates an error.
[email protected]:~/practice_delete$ rmdir dir2
rmdir: failed to remove 'dir2': Directory not empty
And that makes it less useful in most cases.
So how do you delete a non-empty folder then? Well, you’re using the same rm command you used earlier to delete files.
Yes, the same rm command but with the recursive option
rm -r dir_name
🏋️Exercise folder deletion
Let’s put what you’ve learned into practice.
Switch to the practice_delete folder if you are not already there. Now create two directories dir1 and dir2.
mkdir dir1 dir2
Create a file in dir2:
Now try removing the directories using the rmdir command:
Since dir2 is not empty, the rmdir command will fail. Instead, use the rm command with the recursive option:
rm -r dir2
Here’s a replay of all the example commands above to help you:
Interactive delete mode is even more useful when deleting a directory with the recursive option of the rm command:
So, you have learned how to delete files and folders using Linux commands. It’s time to practice some more.
Test your knowledge
Prepare a directory tree that looks like this:
│ ├── file1
│ ├── file2
│ └── file3
Basically, you create a file named file and three directories dir1, dir2 and dir3 in the current directory (practice_delete). And then you create files file1, file2 and file3 in dir1.
Now do the following:
- Go to
dir3and force deletion of the file named
filein the top directory.
- Delete all contents of dir1 but not the directory itself.
- List the contents of the
I encourage you to discuss practical matters in the This is the FOSS community forum.
It’s going well. You learned several basic things like changing directory, checking directory contents, creating and deleting files and directories. In the next chapter, you will learn how to copy files and folders in the terminal. Stay tuned!