Skip to content
Log In Sign Up info@muvcloud.com

Delete a File

Look before you delete. List the file, then remove it:

ls -l old-report.csv
rm old-report.csv
rm -v draft-1.txt draft-2.txt

rm does not ask for confirmation, except for files you cannot write to. -v prints the name of each file it removes. On every current distribution, Ubuntu 26.04 included, rm is the GNU version.

The Options

OptionWhat it does
-iAsks before each file
-IAsks once for many files
-rDeletes folders and contents
-fNever asks, ignores missing files
-vNames each deleted file
-dDeletes an empty folder

-f is the dangerous one: it removes the last question rm would have asked. Use it in scripts after the path has been checked, not as a habit. -I asks once when you delete more than three files or delete recursively, which is a good default for interactive work.

Check Wildcards with ls First

The shell expands * before rm sees it, so rm cannot warn you about a pattern that matches too much. Run the same pattern with ls; if the list is what you expect, change ls to rm:

ls /var/backups/*.sql.gz
rm /var/backups/*.sql.gz

The classic mistake is a stray space: a pattern meant as "everything ending in .log" becomes two arguments, a lone asterisk that matches every file in the folder, and the rest. Checking with ls first catches it. To delete by age, let find print the list, then repeat the command with -delete:

find /var/backups -name "*.sql.gz" -mtime +30 -print
find /var/backups -name "*.sql.gz" -mtime +30 -delete

Delete a Folder

-r deletes everything below the path you give it. Check the path, its contents, and its size first, and run pwd when you use a relative path:

ls -la /var/www/old-site
du -sh /var/www/old-site
rm -r /var/www/old-site

An empty folder can be removed with rmdir, which refuses anything that still holds files. More cases are in how to remove a directory in Linux.

Names That Start with a Dash or Contain Spaces

ls -l -- -file.txt
rm -- -file.txt
rm ./-file.txt
rm "monthly report.pdf"

-- tells rm that no options follow, and ./ makes the name a path. For names with characters you cannot type, find the inode number with ls -li and delete by number, listing first:

find . -maxdepth 1 -inum 1234567 -print
find . -maxdepth 1 -inum 1234567 -delete

The Root Guard and Its Limits

rm refuses to delete the root folder itself recursively: the --preserve-root behavior is on by default, and only an explicit option turns it off. Do not rely on it. The guard protects only the literal root path. A slash followed by an asterisk is expanded by the shell into the list of top-level folders, and rm deletes those without complaint. The same happens when a relative path is run in the wrong folder.

The Empty-Variable Mistake in Scripts

When a script builds a path from a variable and the variable is empty or unset, the path silently shrinks. If the script adds a slash and an asterisk after the variable, an empty value turns the line into "delete everything at the top of the file system". Guard every path that rm uses:

#!/bin/bash
set -euo pipefail

BUILD_DIR="/srv/app/build"
[ -d "$BUILD_DIR" ] || exit 1
rm -rf -- "${BUILD_DIR:?}/cache"

${BUILD_DIR:?} stops the script with an error if the variable is empty or unset, and set -u does the same for any unset variable. More on scripts is in how to write a Bash script.

There Is No Undo

  • Snapshot first: before a large cleanup on a VPS, take a snapshot in your provider's control panel. See snapshot vs backup.
  • Move, then delete: move doubtful files to a holding folder and delete it a week later.
  • Not for logs, databases, or lock files: use logrotate, journalctl --vacuum-size, or the database's own tools instead of rm.
  • Space not returned: a running program still holds the deleted file open. See no space left on device.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations