Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Remove Files and Folders in the Linux Terminal

This guide teaches the safe way to delete files and folders using the command line.

Web Hosting2 min read6 steps

What is the terminal?

The terminal is a window where you type commands to a computer. A directory is another word for a folder. You connect to a hosting server with SSH, which is a safe way to open a terminal on a remote computer.

Warning: There is no recycle bin in the terminal. Deleted files are gone. Take a backup first, and read each command twice before pressing Enter.

Check where you are

  1. Type this to see your current folder:
pwd

This prints the path of the folder you are in.

  1. List the files:
ls -l

This shows the files with details.

Remove one file

rm notes.txt

This deletes the file named notes.txt.

To be asked before each deletion, add -i:

rm -i notes.txt

This asks "remove?" and waits for you to type y or n.

Remove many files

rm *.log

This deletes every file in the folder that ends in .log. The star means "anything".

Remove an empty folder

rmdir oldfolder

This removes the folder only if it has nothing inside.

Remove a folder with files in it

rm -r oldfolder

The -r means recursive. It deletes the folder and everything inside it.

To skip questions, add -f, which means force:

rm -rf oldfolder

This deletes the folder and all its contents without asking.

Safety habits

  1. Run ls on the folder first to see what is inside.
  2. Never run rm -rf / or rm -rf * unless you are fully sure of your location. These can wipe a whole system or site.
  3. Use the full path if in doubt, for example rm -r /home/user/old-backup.
  4. Use rm -i while you are learning.

Common errors

  • No such file or directory: the name is wrong. Check spelling and case.
  • Permission denied: you do not own the file. On a VPS with root access, you can use sudo rm, but be extra careful.
  • Is a directory: use -r for folders.

Quick recap

  • rm file deletes a file.
  • rmdir deletes an empty folder.
  • rm -r folder deletes a folder and its contents.
  • There is no undo, so back up and double-check.