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.
- Beginner
- Linux VPS
- 5 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
- SSH access to your Linux VPS. See Connect to your Linux VPS with SSH.
- A recent backup of anything important. See Back up a Linux VPS with rsync and restic.
- Run
pwdbefore you delete anything with a relative path, so you know which folder you are in.
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:
rm old-notes.txt
rm error.log access.log.1If 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:
rm -i *.logAnswer 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:
rm -v old-notes.txtDelete 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:
rmdir old-folderIf 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:
rm -r old-site/Add -i or -I the first few times so you can see what is about to go:
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-sitedeletes one folder.rm -rf / var/www/old-sitetries to delete the entire server. Modernrmrefuses to remove/itself, but a similar slip such asrm -rf /var /www/old-sitestill deletes all of/var. - An empty variable in a script.
rm -rf "$DIR"/*with$DIRempty becomesrm -rf /*. Guard scripts with${DIR:?}, which stops the script if the variable is empty:
rm -rf -- "${DIR:?}"/*- The wrong folder.
rm -rf *in your home folder instead of a temp folder.
Safer habits:
- Use an absolute path and check it with
lsfirst. - Prefer
rm -rIoverrm -rfwhen typing by hand. - Never run
rm -rfas 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:
ls *.tmp
rm *.tmpWatch 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:
find /var/backups/app -name '*.tar.gz' -mtime +30 -print
find /var/backups/app -name '*.tar.gz' -mtime +30 -deleteFiles with awkward names
Spaces: quote the name or escape each space:
rm "monthly report.pdf"
rm monthly\ report.pdfA leading dash: rm -file.txt fails because rm reads -file.txt as options. Use -- to mark the end of options, or give a path:
rm -- -file.txt
rm ./-file.txtStrange characters you cannot type: delete by inode number. List inodes with ls -li, then:
find . -maxdepth 1 -inum 1234567 -deleteReplace 1234567 with the number shown in the first column.
Check it works
Confirm the file or folder is gone:
ls -la /path/to/parentIf you freed space, check the disk:
df -hIf 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 usermdirfor an empty folder. - `Permission denied`: you do not have write permission on the parent folder. Use
sudoif the deletion is intended. - `Operation not permitted` even as root: the file may have the immutable attribute. Check with
lsattr fileand remove it withsudo chattr -i fileonly 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
- Learn wildcards properly before you use them with
rm: Wildcards in Linux. - Find what is using space: Free up disk space on Linux.
- Keep a way back: Back up a Linux VPS with rsync and restic.
- Need more disk space? Compare Linux VPS plans.
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Getting started
Edit files with nano: a beginner's guide
Open, edit, search and save files with the nano text editor on a Linux server, including cutting and pasting lines, showing line numbers and safely editing system config files with sudo.
- Beginner
- Linux VPS
- 5 min read
Getting started
Your first hour with a new RunRacks server
A step-by-step roadmap for the first hour after your RunRacks server is delivered, from finding your login details to changing the password, updating the OS and picking your next guides.
- Beginner
- All
- 5 min read
Getting started
How to choose a server location
Pick a data center location based on where your users, broker or team are, confirm it with ping tests, and weigh the legal and latency points that come with each choice.
- Beginner
- All
- 4 min read