We have already covered how to search a file in your Debian system. In this article, I am going to demonstrate how you can delete files and directories using a terminal. There are several commands and I’ll explain one by one with examples. I am using Debian 10 here. However, the commands and procedure mentioned below should be almost the same across different Linux distributions.
How to delete files with rm command
Let’s start with the simplest case and we want to delete a single file in our current directory. The syntax of complete command should be as follows.
Suppose you have a file test.txt and you want to delete it. Run the following command on your terminal.
You can delete multiple files with the single rm command. The syntax of complete command should be as follows.
rm <file 1> <file 2> <file 3> …. <file n>
Suppose you have files test1.txt, test2.txt, test3.txt, and test4.txt You want to delete them with single rm command, execute the following on your terminal.
rm test1.txt test2.txt test3.txt test4.txt
If the files are not in your current directory, the above commands should look like the following.
rm <path>/<filename 1 > path/<filename 2> path/<filename 3> …path/<filename n>
Suppose my files are located in Documents/Karim, the above commands will have the following shape.
rm Documents/Karim/test1.txt Documents/Karim/test2.txt Documents/Karim/test3.txt Documents/Karim/test4.txt
wildcards can also be used to delete a group of files. * represents multiple characters and ? represent a single character.
Suppose you want to delete all text files in your current directory. Execute the following command on your terminal.
If your text files are not located in the current directory, provide the path to rm command as follows.
If you have text files named test1.t, test2.t, test3.txt, and text10.txt in your current directory and you want to remove test1.t, and test2.t (single extension), you have to use the placeholder ? in the rm command as follows.
To reduce the risk of accidentally deleting any file, use the -i option in rm commands. That will prompt you for confirmation.
Suppose you want to delete a file test.txt in your current directory, use the -i option in rm command as follows.
rm -i test.txt
If the file you are deleting is a write-protected, you will be asked for a confirmation. You can use the -f option which is the opposite of -i option. This will delete the file without any confirmation even the file is write-protected.
How to delete directories with rm command
Let’s again start with a simple case and delete an empty directory in your current path. You have to use the -d option in rm command as follows.
rm -d <directory name>
Suppose you want to delete a directory named Karim. Execute the following on your terminal.
rm -d Karim
If you want to delete multiple empty directories inside your current path, you can delete them with single rm command as follows.
rm -d <directory 1> <directory 2 > <directory 3>
Suppose you have empty directories named Asif, Ali, Taha and you want to delete them with a single rm command. Execute the following on your terminal.
rm -d Asif Ali Taha
If the directories are not in your current path, you can provide the path along with a directory name.
Suppose we have an empty directory named test located inside Documents, the command should look like the following.
rm -d Documents/test
If you have non-empty directories, you have to use the -r option in rm command as follows. This will delete all the files and sub-directories inside the directory.
If the directory is not in your current path you have to provide it along with directory name as we did in case of removing files.
In case the directory is a write-protected, you will be prompted to confirm its deletion. To suppress the confirmation, use the -f option along with -d or -r option (-rf or -dr).
How to delete directories with rmdir command
You can also delete the directories with rmdir command. However, rmdir command only deletes directories which are empty.
For instance, I have tried to delete the non-empty directory and got the error as shown in the following screenshot.
All the above examples also work with rmdir command. Therefore, we will not go into detail here.