By the end of this tutorial, you will be able to delete a single file, an empty directory, a whole directory tree, and files filtered by name or age, checking the target each time before you act. The Linux command line has no trash can: a deletion is permanent. The method matters as much as the command.
Prerequisites
- Shell access to a Linux machine (local or over SSH), for example a Linux VPS
- A user with write permission on the directory concerned (or
sudo) - A recent backup of anything important
Check the target before deleting
Move into the right directory and list its contents. It takes two seconds and prevents most accidents.
Bashpwd ls -la
For a pattern (extension, prefix), first display what it will match. rm will expand to exactly the same list.
Bashls -d *.log
Delete a file
The basic command is rm.
Bashrm report.txt
Add -i to have it ask for confirmation, and -v to have it print what it removes.
Bashrm -iv report.txt
Then check with ls report.txt: the message "No such file or directory" confirms the file is gone.
Delete several files
Shell wildcards let you target a batch. List first with ls, then delete.
Bashrm -v *.tmp rm -v photo_{01..05}.jpg
Avoid a bare wildcard such as rm * in a directory you are not sure about. If a filename starts with a hyphen, use -- to mark the end of the options:
Bashrm -- -strange-file.txt
Delete an empty directory
rmdir only removes empty directories. That is deliberate: it acts as a safety net.
Bashrmdir old_folder
To also remove parent directories that become empty:
Bashrmdir -p project/cache/tmp
Delete a directory and everything in it
This requires the recursive option -r. First, estimate what you are about to lose.
Bashdu -sh old_project find old_project -type f | wc -l
Then delete, with confirmation:
Bashrm -ri old_project
With thousands of files, prompting for each one becomes impractical. The -f option turns it off:
Bashrm -rf old_project
Never put an unchecked variable in this command. With rm -rf "$DIR"/, if $DIR is empty, you are targeting the root directory. In a script, add set -u or use "${DIR:?}", which aborts execution if the variable is empty.
Delete files by age
find filters on precise criteria (name, type, age, size). The principle: run it without any action first, then add -delete.
Bashfind /var/log/myapp -type f -name "*.log" -mtime +30
If the list matches what you expect:
Bashfind /var/log/myapp -type f -name "*.log" -mtime +30 -delete
-mtime +30 matches files modified more than 30 days ago. Put -delete last: find evaluates expressions from left to right, and a -delete placed too early removes everything.
Erase a sensitive file more securely
rm removes the file's directory entry, not necessarily the data on the disk. shred overwrites the content before deleting it.
Bashshred -u -v secret.key
This approach has limits on SSDs, journaling file systems and volumes with snapshots, where older data may remain. For truly sensitive data, encrypting the disk from the start remains the reliable solution.
Troubleshooting
Permission denied
You do not have write permission on the parent directory (it is the directory that controls deletion, not the file). Check with ls -ld DIRECTORY, then use sudo if that is legitimate:
Bashsudo rm -v /path/to/file
Operation not permitted, even with sudo
The file probably has the immutable attribute. Inspect it, remove the attribute, then delete.
Bashlsattr file.txt sudo chattr -i file.txt sudo rm file.txt
Argument list too long
The wildcard expanded to more arguments than the system accepts. Use find instead, which handles files one at a time:
Bashfind . -maxdepth 1 -type f -name "*.tmp" -delete
Directory not empty or Device or resource busy
With rmdir, the first message is expected: the directory contains files, including hidden ones (ls -A). If a mount point or a process is using it, identify it with lsof +D DIRECTORY or findmnt, then release it before trying again.
To practise safely, a disposable instance is handy: on a VPS, you can create a test directory and make mistakes with no consequences. More guides of this kind are collected in the tutorials section.
FAQ
Can you recover a file deleted with rm?
Rarely, and with no guarantee. Tools such as extundelete or testdisk sometimes find data if you stop writing to the disk immediately. The only reliable protection is a backup.
What is the difference between rm -r and rm -rf?
rm -r deletes recursively but prompts for confirmation on write-protected files. rm -rf forces the deletion and silently ignores nonexistent files. Reserve it for cases where you have already checked the target.
How do you delete a directory whose name contains spaces?
Wrap the name in quotes or escape the spaces: rm -r "My folder" or rm -r My\ folder. Without that, the shell treats each word as a separate item.
Is there a trash can on the command line?
Yes, through the trash-cli package (command trash-put), which moves items to the user's trash instead of erasing them. On a server it is rarely installed by default, so get into the habit of listing before you delete.