DEV Community

loading...
Cover image for How to remove a directory in Linux?

How to remove a directory in Linux?

MELVIN GEORGE
Hi, I'm Melvin George, A web developer, and a blogger.
Originally published at melvingeorge.me ・1 min read

Originally posted here!

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

That's it 😃!

Feel free to share if you found this useful 😃.


Discussion (0)