We have already covered how to search for a file in Debian. In this article, I’m going to demonstrate how you can delete files and directories using the terminal. There are several commands and I will explain them one by one with examples. I’m using Debian 10 here. However, the commands and procedures mentioned below should be pretty much the same for different Linux distributions.
How to delete files using rm command
Let’s start with the simplest case, and we want to delete one file in our current directory. The syntax for the complete command should be as follows.
rm <имя файла>
Suppose you have a test.txt file and want to delete it. Run the following command on your terminal.
You can delete multiple files with a single rm command. The syntax for the complete command should be as follows.
rm <файл 1> <файл 2> <файл 3>…. <файл n>
Suppose you have files test1.txt, test2.txt, test3.txt, and test4.txt. You want to remove them with a single rm command, execute the following in 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 this.
rm <путь> / <имя файла>
rm <путь> / <имя файла 1> way / <имя файла 2> way / <имя файла 3>… way / <имя файла n> Ad
Suppose my files are in Documents / Karim, the above commands will have the following form.
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? are a single character.
Let’s say you want to delete all text files in the current directory. Run the following command on your terminal.
If your text files are not in the current directory, specify the path to the 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 (one extension), you must use the placeholder ? in the rm command as follows.
To reduce the risk of accidentally deleting any file, use the -i option to rm commands. You will be prompted for confirmation.
Suppose you want to delete the test.txt file in the current directory, use the -i parameter to the rm command as follows.
rm -i test.txt
If the file you are deleting is write-protected, you will be prompted for confirmation. You can use the -f option, which is the opposite of the -i option. This will delete the file without any confirmation, even if the file is write-protected.
How to remove directories using rm command
Let’s start with a simple case again and remove the empty directory in your current path. You must use the -d option in the rm command as follows.
rm -d <имя каталога>
Let’s say you want to delete a directory named Karim. Run the following on your terminal.
rm -d Karim
If you want to remove multiple empty directories within your current path, you can remove them with a single rm command as follows.
rm -d <каталог 1> <каталог 2> <каталог 3>
Suppose you have empty directories named Asif, Ali, Taha and you want to delete them with a single rm command. Run the following on your terminal.
rm -d Asif Ali Taha
If the directories are not in your current path, you can include the path along with the directory name.
Suppose we have an empty directory named test located inside Documents, the command should look like this.
rm -d Documents/test
If you have non-empty directories, you must use the -r parameter to the rm command as follows. This will remove all files and subdirectories inside the directory.
If the directory is not in your current path, you must include it along with the directory name, as we did for deleting files.
If the directory is write-protected, you will be prompted to confirm deletion. To suppress confirmation, use the -f option in conjunction with the -d or -r option (-rf or -dr).
How to remove directories using rmdir command
You can also remove directories using the rmdir command. However, the rmdir command only removes empty directories.
For example, I tried to delete a non-empty directory and got an error as shown in the following screenshot.
All of the above examples also work with the rmdir command. Therefore, we will not go into details here.
How to delete files and directories using the terminal in Debian 10