Getting started

Delete files and folders in Linux safely (rm, rmdir)

Delete files and folders on a Linux server with rm and rmdir, understand why rm -rf and wildcards are dangerous, and remove files with awkward names such as spaces or leading dashes.

BeginnerLinux server5 min read

Linux has no recycle bin. When you delete a file with rm, it is gone, and the only way back is a backup. That makes deleting the one everyday task where a small typo can do real damage. This guide shows how to remove files and folders, how to slow yourself down when it matters, and how to handle file names that confuse the shell.

Before you start

Warning: Everything in this guide permanently deletes data. Read each command twice before pressing Enter, especially when you use sudo.

Delete a file with rm

rm removes one or more files:

Bash
rm old-notes.txt
rm error.log access.log.1

If the file is write-protected, rm asks for confirmation. If you do not own the file, you need sudo.

To be asked before each deletion, add -i:

Bash
rm -i *.log

Answer y to delete or n to keep each file. -I (capital) asks once when you delete more than three files or delete recursively, which is less tedious but still gives you a moment to stop.

Add -v to see each file as it is removed:

Bash
rm -v old-notes.txt

Delete a folder

rmdir removes a folder only if it is empty. That makes it a safe choice when you expect the folder to be empty:

Bash
rmdir old-folder

If the folder still contains something, you get Directory not empty and nothing is deleted.

To delete a folder and everything inside it, use rm -r:

Bash
rm -r old-site/

Add -i or -I the first few times so you can see what is about to go:

Bash
rm -rI old-site/

Why rm -rf needs respect

-f (force) tells rm never to ask and to ignore files that do not exist. Combined with -r, it removes a whole tree without a single question. It is useful in scripts, and dangerous when typed by hand.

The classic mistakes:

  • A stray space. rm -rf /var/www/old-site deletes one folder. rm -rf / var/www/old-site tries to delete the entire server. Modern rm refuses to remove / itself, but a similar slip such as rm -rf /var /www/old-site still deletes all of /var.
  • An empty variable in a script. rm -rf "$DIR"/* with $DIR empty becomes rm -rf /*. Guard scripts with ${DIR:?}, which stops the script if the variable is empty:
Bash
  rm -rf -- "${DIR:?}"/*
  • The wrong folder. rm -rf * in your home folder instead of a temp folder.

Safer habits:

  1. Use an absolute path and check it with ls first.
  2. Prefer rm -rI over rm -rf when typing by hand.
  3. Never run rm -rf as root unless you have checked the path character by character.

Wildcards: test before you delete

Wildcards such as * match many files at once. Before you delete with a pattern, run the same pattern with ls to see exactly what it matches:

Bash
ls *.tmp
rm *.tmp

Watch out for a space in the wrong place. rm *.tmp deletes temp files; rm *.tmp deletes everything in the folder and then complains that .tmp does not exist.

Plain * does not match hidden files (those starting with a dot), so rm -r folder/* leaves .htaccess and other dotfiles behind. That is usually what you want.

To delete files by age rather than by name, use find and review the list before adding -delete:

Bash
find /var/backups/app -name '*.tar.gz' -mtime +30 -print
find /var/backups/app -name '*.tar.gz' -mtime +30 -delete

Files with awkward names

Spaces: quote the name or escape each space:

Bash
rm "monthly report.pdf"
rm monthly\ report.pdf

A leading dash: rm -file.txt fails because rm reads -file.txt as options. Use -- to mark the end of options, or give a path:

Bash
rm -- -file.txt
rm ./-file.txt

Strange characters you cannot type: delete by inode number. List inodes with ls -li, then:

Bash
find . -maxdepth 1 -inum 1234567 -delete

Replace 1234567 with the number shown in the first column.

Check it works

Confirm the file or folder is gone:

Bash
ls -la /path/to/parent

If you freed space, check the disk:

Bash
df -h

If df does not show the space coming back, a running program may still hold the deleted file open. Restart that service, or see the troubleshooting list below.

Troubleshooting

  • rm: cannot remove 'x': Is a directory: add -r, or use rmdir for an empty folder.
  • Permission denied: you do not have write permission on the parent folder. Use sudo if the deletion is intended.
  • Operation not permitted even as root: the file may have the immutable attribute. Check with lsattr file and remove it with sudo chattr -i file only if you know why it was set.
  • Disk space did not come back: a process still has the file open, often a log. Find it with sudo lsof +L1, then restart that service.
  • You deleted the wrong thing: stop writing to that disk and restore from your backup or a snapshot. There is no reliable undelete on a running server.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.