Deleting Safely
Deleting from the command line is fast and permanent: del, Remove-Item and rm don't use the Recycle Bin or the Trash. There is no undo. This lesson shows how to delete files and folders in each shell, and, just as important, the habits that keep you from deleting the wrong thing.
| Task | CMD | PowerShell | bash / zsh |
|---|---|---|---|
| Delete a file | del a.txt (erase) |
Remove-Item a.txt (del, rm, ri) |
rm a.txt |
| Delete several files | del *.tmp |
Remove-Item *.tmp |
rm *.tmp |
| Delete an empty folder | rd old (rmdir) |
Remove-Item old |
rmdir old |
| Delete a folder and everything in it | rd /s /q old |
Remove-Item old -Recurse -Force |
rm -r old (rm -rf old) |
| Ask before each file | del /p *.tmp |
Remove-Item *.tmp -Confirm |
rm -i *.tmp |
| Preview only | dir /b *.tmp first |
-WhatIf |
ls *.tmp / echo rm *.tmp first |
| Delete in all sub-folders | del /s /q *.tmp |
Get-ChildItem -Recurse *.tmp | Remove-Item |
find . -name '*.tmp' -delete |
Windows (CMD)
del removes files; rd (or rmdir) removes folders. rd only removes empty folders unless you add /s (sub-folders and files), and /q (quiet) skips the Are you sure (Y/N)? question:
@echo off
mkdir cleanup\cache\old
cd cleanup
echo a> notes.txt
echo b> temp1.tmp
echo c> temp2.tmp
echo d> cache\data.bin
del temp1.tmp
del /q *.tmp
dir /b /o:n
echo ---
rd /s /q cache
dir /b /o:ncache
notes.txt
---
notes.txt
Things to watch out for in CMD:
delon a folder name deletes the files inside it (after asking Are you sure?) but keeps the folder. Userd /sto remove the folder itself.delskips read-only files unless you add/f, and it doesn't touch hidden files unless you use/a.rd /s /qasks nothing at all. Double-check the path, especially when it comes from a variable: if%target%is empty,rd /s /q "%target%\"would refer to the root of the current drive.
C:\Users\ada\cleanup>del cache
C:\Users\ada\cleanup\cache\*, Are you sure (Y/N)? nWindows (PowerShell)
Remove-Item deletes files and folders. A folder with content needs -Recurse; read-only and hidden items need -Force. Its best feature is -WhatIf: run the command with it first, read the list, then run it again without:
New-Item -ItemType Directory cleanup/cache -Force | Out-Null
Set-Location cleanup
Set-Content notes.txt 'a'
Set-Content temp1.tmp 'b'
Set-Content temp2.tmp 'c'
Set-Content cache/data.bin 'd'
Remove-Item *.tmp -WhatIf
Remove-Item *.tmp
Remove-Item cache -Recurse
Get-ChildItem -NameWhat if: Performing the operation "Remove File" on target "/tmp/cleanup/temp1.tmp".
What if: Performing the operation "Remove File" on target "/tmp/cleanup/temp2.tmp".
notes.txt
Without -Recurse, removing a non-empty folder stops and asks The item at … has children … Are you sure you want to continue?, which fails in a non-interactive script. -Confirm asks before every single item.
Because PowerShell works with objects, you can filter precisely before deleting, for example "only .log files bigger than 1 MB" or "older than 30 days":
PS C:\Users\ada> Get-ChildItem C:\Temp -Recurse -File |
>> Where-Object LastWriteTime -lt (Get-Date).AddDays(-30) |
>> Remove-Item -WhatIfLinux & macOS (bash/zsh)
rm removes files, rm -r removes folders with their content, and rmdir removes only empty folders (a nice safety net):
mkdir -p cleanup/cache/old && cd cleanup
echo a > notes.txt
echo b > temp1.tmp
echo c > temp2.tmp
echo d > cache/data.bin
rm temp1.tmp
rm -v *.tmp
rmdir cache
rm -r cache
lsremoved 'temp2.tmp'
rmdir: failed to remove 'cache': Directory not empty
notes.txt
-vreports every file removed;-iasks before each one;-Iasks once before deleting more than three files.-f(force) never asks and ignores missing files.rm -rfis therefore the most dangerous command on a Unix system: it deletes a whole tree without any question.- A file name starting with
-looks like an option. Use--(end of options) or./:rm -- -draft.txtorrm ./-draft.txt.
touch -- -draft.txt keep.txt
rm -- -draft.txt
lskeep.txt
zsh adds its own guard: before running rm * or rm path/* interactively, it asks sure you want to delete all … files?. Both shells can also preview a wildcard by putting echo in front of the command:
touch report.txt draft1.tmp draft2.tmp
echo rm *.tmp
rm *.tmp
print -l *rm draft1.tmp draft2.tmp
report.txt
Because the shell expands *.tmp before the command runs, echo shows exactly what rm will receive.
Safety habits for every shell
- Look first. Run the listing with the same pattern (
dir /b *.tmp,Get-ChildItem *.tmp,ls *.tmp) before deleting. - Preview. Use
-WhatIf(PowerShell),echo rm …(bash/zsh) orzmv -n. - Avoid empty variables in paths. In bash,
rm -rf "$dir/"*with an empty$dirtargets/. Use"${dir:?}", which stops the script if the variable is empty (example below).
dir=""
rm -rf "${dir:?is empty, refusing to delete}/"*
echo "not reached"/w/main.sh: line 2: dir: is empty, refusing to delete
- Prefer the Trash for manual cleanups. Graphical file managers move files to the Recycle Bin/Trash. From a Linux terminal,
gio trash filedoes the same; on macOS and Windows install a helper (brew install trash, theRecyclemodule) if you want that safety from the command line. - Never run a delete command you copied from the internet without understanding every part of it, especially with
sudoor in an administrator window.
Learn more: rm: Remove Files · del: Delete Files · mkdir & rmdir · PowerShell Files & Folders
Test yourself
Where do files deleted with rm, del or Remove-Item go?
- The Recycle Bin / Trash
- A hidden backup folder
- Nowhere: they're deleted permanently
- The parent folder
Which CMD command deletes the folder old with all its sub-folders and files without asking?
del oldrd oldrd /s /q olddel /s old
How can you see which files rm *.log would delete, without deleting them?
rm -n *.logecho rm *.logrm --preview *.logrm *.log -WhatIf