How to Delete Files and Directories Using the Terminal in Debian

Delete Files on Debian

We have already covered how to search for a file in Debian. In this article, I am going to demonstrate how you can delete files and directories using the terminal. There are several commands and I’ll explain them one by one with examples. I am using Debian 10 here. However, the commands and procedures mentioned below should be almost the same across different Linux distributions.

How to delete files with the rm command

Let’s start with the simplest case and we want to delete a single file in our current directory. The syntax of the complete command should be as follows.

rm <filename>

Suppose you have a file test.txt and you want to delete it. Run the following command on your terminal.

rm test.txt

Delete file with rm command

You can delete multiple files with the single rm command. The syntax of the 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

Delete multiple files with rm command

If the files are not in your current directory, the above commands should look like the following.

rm <path>/<filename>

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
rm Documents/Karim/test1.txt Documents/Karim/test2.txt Documents/Karim/test3.txt Documents/Karim/test4.txt

Delete several files using rm

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.

rm *.txt

Using wildcards when deleting files

If your text files are not located in the current directory, provide the path to the rm command as follows.

rm Documents/Karim/*.txt

Using wildcards within a path

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.

rm *.?

Single char placeholder

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

prompt user when deleting files

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 the 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

Delete a directory with rm command

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

Delete multiple directories at once

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

Delete a path

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.

Delete non-empty directory

If the directory is not in your current path you have to provide it along with the directory name as we did in case of removing files.

In case the directory is 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 the rmdir command

You can also delete the directories with the rmdir command. However, the rmdir command only deletes directories that are empty.

For instance, I have tried to delete the non-empty directory and got the error as shown in the following screenshot.

 

Delete a directory using rmdir command

All the above examples also work with the rmdir command. Therefore, we will not go into detail here.