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

Deleting files through a graphical interface feels intuitive — drag to trash, empty, done. But the terminal gives you something the GUI doesn't: precision, speed, and the ability to remove files that the interface won't touch. The tradeoff is that terminal deletions are immediate and, in most cases, permanent. Understanding exactly what you're doing before you run the command matters.

The Core Command: rm

On Linux and macOS, the primary command for deleting files in the terminal is rm (short for remove). On Windows, the equivalent commands live in either Command Prompt or PowerShell, with slightly different syntax.

Deleting a Single File (Linux/macOS)

rm filename.txt 

This removes the file instantly. There's no recycle bin, no confirmation prompt by default, and no undo. The file is unlinked from the filesystem.

If the file is in a different directory than your current working location, provide the full or relative path:

rm /home/user/documents/filename.txt 

Deleting Multiple Files at Once

rm file1.txt file2.txt file3.txt 

You can list as many files as you need in a single command, separated by spaces.

Using Wildcards

The * wildcard lets you match multiple files by pattern:

rm *.log 

This deletes every .log file in the current directory. Wildcards are powerful — and dangerous if you're not paying attention to where you are in the filesystem.

Useful Flags to Know

The rm command becomes more flexible and safer with flags:

FlagWhat It Does
-iInteractive mode — prompts for confirmation before each deletion
-fForce — skips prompts, even for write-protected files
-rRecursive — deletes directories and all their contents
-vVerbose — lists each file as it's deleted

Combining flags is common. For example:

rm -ri foldername/ 

This recursively deletes a directory but asks for confirmation on each file — useful when you're not 100% certain what's inside.

⚠️ The combination rm -rf is one of the most powerful — and most dangerous — commands in Unix-based systems. It forcefully and recursively deletes everything in the target path without asking. Run it on the wrong directory and there's no recovery path without a backup.

Deleting Directories

rm alone won't remove a directory. You need the -r flag (recursive):

rm -r foldername/ 

If you want to remove an empty directory only, a safer alternative is:

rmdir foldername/ 

rmdir will refuse to delete a directory that still contains files, which makes it a lower-risk option when you want to be certain you're not wiping contents accidentally.

How to Delete Files in Terminal on Windows

Windows terminal environments work differently depending on whether you're using Command Prompt or PowerShell.

Command Prompt

del filename.txt 

For directories:

rmdir /s /q foldername 

The /s flag removes subdirectories and files, and /q runs it quietly without prompting.

PowerShell

Remove-Item filename.txt 

PowerShell supports aliases like rm and del that mirror Unix behavior, but the native cmdlet is Remove-Item. For recursive deletion:

Remove-Item -Recurse -Force foldername 

PowerShell tends to be more verbose and structured than Bash, which can make it easier to read and audit commands before running them.

🗂️ What Happens to the File After Deletion?

This depends on the operating system and filesystem:

  • On Linux and macOS, rm removes the file's directory entry. The data may still exist on disk until overwritten, but it's inaccessible through normal means.
  • On Windows, del behaves similarly — the space is marked as available, but forensic recovery tools may retrieve the data until that space is reused.
  • Neither approach is equivalent to secure deletion. If you're removing sensitive data, you'd need additional tools like shred (Linux), sdelete (Windows), or disk-level wiping utilities.

This distinction matters significantly if you're handling personal data, credentials, or anything subject to compliance requirements.

Variables That Change How You Should Approach This

The right command and approach depend on several factors specific to your situation:

  • Your operating system — macOS, Linux distros, and Windows each have different default shells and command behavior
  • Shell type — Bash, Zsh, Fish, PowerShell, and cmd.exe all have subtle differences in how wildcards and flags are handled
  • File permissions — files owned by another user or marked read-only may require elevated privileges (sudo on Linux/macOS) or administrator mode on Windows
  • Whether you need recovery options — if there's any chance you'll want the file back, terminal deletion is the wrong tool without a backup strategy in place
  • What you're deleting — a single text file, a directory tree with thousands of files, or system-level files each carry different risk profiles

🔍 Users comfortable in the terminal often develop habits around rm -i or aliasing rm to always prompt — while others working in automated scripts may need the silent, forceful version. These are meaningfully different workflows that call for different defaults.

The command itself is simple. What varies is whether the defaults, flags, and environment match what your specific setup actually needs.