How to remove a directory in Linux?

Published July 17, 2021

To remove a directory in Linux, you can use the rm command followed by a space and the -r flag and the name of the directory in the Linux terminal. This will also remove any other directories and their files inside the directory along with it.

For example, let's say you have a directory called myDirectory and wants to delete it. You can use the rm command like this in the terminal,

# Delete a directory with any files
# inside in Linux
rm -r myDirectory

Executing the above command will delete the myDirectory directory from the system.

See the execution of the above command live in repl.it.

Sometimes when using the above command you will be prompted to confirm the deletion of some directories that are write-protected.

So to avoid that, and delete all the write-protected files without being prompted you can use the -f flag with the above command, so now the command will look like this,

# Delete a directory with any files without being prompted
# inside in Linux
rm -rf myDirectory

That's it 😃!

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter