How to delete files and directories using Terminal in Debian 10

s2 0

We have already covered how to search for a file in Debian. In this article I am going to show you how to delete files and directories using 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 listed below should be almost the same for 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 full command should be as follows.

rm

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

rm test.txt

You can delete multiple files with the single rm command. The syntax of the full command should be as follows.

rm ….

Assuming you have the files test1.txt, test2.txt, test3.txt, and test4.txt you want to delete them with a single rm command, do the following on your terminal.

rm test1.txt test2.txt test3.txt test4.txt

Delete multiple files with the rm command

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

rm /

rm / Path/ Path/ …Path/advertising

Assuming my files are in Documents / Karim, the commands above have the following form.

rm Documents/Karim/test1.txt
rm Documents/Karim/test1.txt Documents/Karim/test2.txt Documents/Karim/test3.txt Documents/Karim/test4.txt

Multiple files with rm.  Clear

Wildcards can also be used to delete a group of files. * stands for several characters and? represent a single character.

Suppose you want to delete all text files in your current directory. Run the following command on your terminal.

rm *.txt

Using wildcards when deleting files

If your text files are not 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 need to use the wildcard ? in the rm command as follows.

rm *.?

Placeholder for a single character

To reduce the risk of accidentally deleting a file, use the -i option with the rm commands. This will ask you to confirm.

Assuming you want to delete a test.txt file in your current directory, use the -i option with the rm command as follows.

rm -i test.txt

Prompt user when deleting files

If the file to be deleted is write-protected, you will be asked for confirmation. You can use the -f option, which is the opposite of the -i option. This will delete the file without confirmation, even if the file is read-only.

How to delete directories with the rm command

Let’s start again with a simple case and delete an empty directory in your current path. You must use the -d option with the rm command as follows.

rm -d

Suppose you want to delete a directory called Karim. Follow the steps below on your terminal.

rm -d Karim

Delete a directory with the rm command

If you want to delete multiple empty directories in your current path, you can delete them with a single rm command as follows.

rm -d

Suppose you have empty directories named Asif, Ali, Taha and you want to delete them with a single rm command. Follow the steps below on your terminal.

rm -d Asif Ali Taha

Delete several directories at the same time

If the directories are not in your current path, you can include the path along with a directory name.

Assuming we have an empty directory called test in Documents, the command should look like this.

rm -d Documents/test

Delete a path

If you have non-empty directories, you must use the -r option with the rm command as follows. This will delete all files and subdirectories within the directory.

Remove non-empty directory

If the directory is not in your current path, you will need to provide it along with the directory name as we did when removing files.

If the directory is write-protected, you will be asked to confirm the deletion. To suppress confirmation, use the -f option with the -d or -r (-rf or -dr) option.

How to delete directories with the rmdir command

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

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

Delete a directory with the rmdir command

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

How to delete files and directories using Terminal in Debian 10