How to remove a directory in Linux

There is no recycle bin on the command line. Know which command you need, and check the path before you press Enter.

An empty directory

rmdir myfolder

A directory with files in it

rm -r myfolder

Add -i to be asked before every deletion: rm -ri myfolder.

Without any prompts

rm -rf myfolder

-f skips questions and ignores missing files. Use it only when you are sure.

Delete matching folders everywhere

For example, every node_modules folder below the current directory (preview with -print first):

find . -name node_modules -type d -prune -print
find . -name node_modules -type d -prune -exec rm -rf {} +

Warning: Never run rm -rf / or paste a command you do not understand. A stray space, as in rm -rf / home, changes the meaning completely. Run ls on the path first.

Questions

Why do I get “Permission denied”?

You do not own the directory. Check with ls -ld myfolder. Use sudo only if you are certain the path is right.

Can I get the files back?

Usually not. Deleted files do not go to a trash folder. Restore from a backup.