Raspberry Pi

How to Remove Directories on Raspberry Pi

Removing directories on the Raspberry Pi system isn’t straightforward, especially when you have created the directories using the “sudo” command. Because, in that case, you are not allowed to change the directory content or delete it easily. Changing the owner permission still won’t delete the directory and you have no other option than searching for commands to delete the directory in seconds.

In this article, we will show you the easiest way you can follow to delete the directory on your Raspberry Pi system successfully.

How to Remove Directories on Raspberry Pi

When you remove the directory on your Raspberry Pi system, which is created using the “sudo” command, you will get an error that shows you the failure of removing the selected directory from your system.

Deleting them from the GUI won’t be helpful in this case and you are bound to use the Raspberry Pi terminal to remove the directory through command. Here, we will provide you with two different commands to easily remove the directory from your Raspberry Pi system.

1: Remove Directories Using rmdir Command

If you have an empty directory on your Raspberry Pi system, then deleting it using the rmdir command is the easiest way, and you can follow the below-mentioned to remove a specific directory from your Raspberry Pi system.

$ rmdir <directory_name>

You need to replace the “directory_name” with the name of the directory you want to delete. You should also ensure that the directory you are deleting must be empty; otherwise, you will get an error as shown below.

If you want to remove multiple directories, you can use the following syntax:

$ rmdir <directory1> <directory2>

In case the directory path is different, you can apply the following command to remove a specific directory placed at a different location.

2: Remove Directories Using rm Command

Although the above command will only remove the empty directory from your Raspberry Pi system, you can use the rm command to remove a non-empty directory on your Raspberry Pi system. There are several ways to remove the directory through the rm command, but the most followed command on the Raspberry Pi system is as follows:

$ rm -rf <directory_name>

In the above command, the “-r” tag recursively removes the directories and their contents, while the “-f” tag is used to remove the directory contents forcefully. You must provide a flag with the rm command to successfully remove the directory if you don’t use it, you will get the following error.

If there is a space between the directory name like “my folder”, you should follow the below command to remove the specific directory from your system.

$ rm -rf my\ folder

You should need to add “\” and a one-word space in between the folder name to remove the directory from your system successfully.

The same method you can use with the above rmdir command if you have an empty directory with a space between the names.

You can open the help section of the rm command to find out further about the tags you can use with this command.

$ rm --help

Conclusion

Removing sudo-enabled directories on Raspberry Pi isn’t straightforward, as you cannot remove them from the GUI. There are two terminal commands that you can find in the guidelines mentioned above to remove the directories from your Raspberry Pi system successfully. If you have an empty directory, you can choose the “rmdir” command to remove the directory from your system. However, if the directory includes files and subdirectories, you can forcefully remove the directory using the “rm” command.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.