How to Delete a File in Terminal: Commands, Options, and What to Know First

Deleting files through a graphical interface — dragging to the trash, clicking delete — feels intuitive because it's forgiving. Terminal is different. When you delete a file in the command line, there's no confirmation dialog, no Recycle Bin, and often no undo. Understanding exactly what you're doing before you run any command is the difference between a clean system and a bad afternoon.

The Core Command: rm

On macOS and Linux (and any Unix-based system), the primary command for deleting files is rm, short for remove.

Basic syntax:

rm filename.txt 

That's it. Run that, and the file is gone. No prompt, no second chance by default.

For Windows, the terminal environment matters:

  • In Command Prompt (CMD), the equivalent is del
  • In PowerShell, you can use Remove-Item or its alias rm
  • In Windows Subsystem for Linux (WSL), you're running a Linux shell, so rm applies

This article focuses primarily on rm since it covers the widest range of systems and is what most people mean when they ask about deleting files in terminal.

Common rm Flags You'll Actually Use

The base rm command handles single files cleanly, but real-world situations often call for flags — modifiers that change how the command behaves.

FlagWhat It Does
-iInteractive mode — prompts for confirmation before each deletion
-fForce — skips confirmation prompts and ignores nonexistent files
-r or -RRecursive — deletes directories and all their contents
-vVerbose — prints each file name as it's deleted

Examples:

Delete a file with a confirmation prompt (safer for beginners):

rm -i filename.txt 

Delete a directory and everything inside it:

rm -r foldername/ 

Delete multiple files at once:

rm file1.txt file2.txt file3.txt 

Delete all .log files in a directory using a wildcard:

rm *.log 

⚠️ The wildcard approach is powerful and dangerous in equal measure. Always double-check your current directory with pwd before using wildcards.

Why Terminal Deletion Is Permanent (Usually)

When you delete a file through a GUI, most operating systems move it to a temporary holding area — the Trash on macOS, the Recycle Bin on Windows. rm skips that entirely. It unlinks the file from the filesystem immediately.

On most standard filesystems (ext4 on Linux, APFS on macOS), the data may still exist physically on disk until overwritten — but there's no easy, built-in recovery path. Forensic tools can sometimes recover data, but that's not something to count on.

This behavior is by design. Terminal is a tool for people who know what they want to do. The assumption is that if you typed the command, you meant it.

Deleting Files Safely: Habits Worth Building

Because rm is unforgiving, experienced users develop a few habits:

Use -i when uncertain. The interactive flag adds a one-keystroke confirmation for each file. It slows you down just enough to catch mistakes.

List before you delete. Before running rm *.txt, run ls *.txt first to see exactly what matches. What you think will match and what actually matches can differ.

Avoid rm -rf / or variations. This is the classic catastrophic command — recursively and forcefully deleting from the root directory. Modern systems have safeguards against the literal / version, but variations targeting the wrong directory can still cause serious damage.

Check your working directory. Run pwd (print working directory) before deleting anything to confirm you're where you think you are.

🗂️ Deleting Directories vs. Files

rm alone won't delete a directory. If you try, you'll get an error:

rm: foldername: is a directory 

To delete an empty directory, use:

rmdir foldername 

To delete a directory and all its contents:

rm -r foldername/ 

The distinction matters. An empty directory removal is low-stakes. A recursive deletion of a folder with thousands of files is not. The same command structure handles both, which is why confirming what's inside before running -r is always worth a moment.

Windows Terminal: Key Differences

If you're working in Command Prompt, the del command deletes files but not directories. For directories, rmdir /s foldername handles recursive deletion.

PowerShell brings more Unix-like behavior. Remove-Item (or rm as an alias) accepts -Recurse and -Force flags that map closely to Linux/macOS equivalents:

Remove-Item filename.txt Remove-Item foldername -Recurse 

PowerShell does offer a -Confirm flag for interactive deletion, which adds a layer of safety similar to rm -i.

Variables That Change the Picture

How straightforward file deletion is in terminal depends on several factors that vary by user:

  • Operating system and shell — bash, zsh, PowerShell, CMD, and fish all have different behaviors and available commands
  • File permissions — files owned by another user or protected by system permissions may require sudo (on Unix systems) or administrator privileges on Windows, which raises the stakes considerably
  • Filesystem type — some filesystems support journaling or snapshots (like ZFS or Btrfs) that may allow recovery; others don't
  • Experience level — whether you're comfortable reading error messages and understanding what flags do changes which approach is appropriate

A developer comfortable in bash on a Linux server operates very differently from someone using macOS Terminal for the first time, or a Windows user learning PowerShell. The commands overlap, but the context — and the margin for error — can be quite different.