keepcalculator

06 Numbers and Powers

Delete Files Command: Windows, Mac & Linux

Card No. 06·21143 · Updated · Standard formula, shown below

The delete files command is the fastest way to remove files from your computer without using a graphical interface. Depending on your operating system, you use del in Windows Command Prompt, rm in PowerShell, macOS Terminal, or Linux. This guide covers the exact syntax for each, how to delete folders, how to bypass the Recycle Bin, and the safety rules you must follow to avoid permanent data loss.

Why Use the Command Line to Delete Files?

Graphical file managers work well for deleting a few files, but they become slow and error-prone when you need to remove hundreds of files, clear temp folders, or delete files with unusual names. The command line gives you:

  • Speed: Delete an entire folder tree with a single command.
  • Precision: Use wildcards to target specific patterns (e.g., all .tmp files).
  • Automation: Incorporate deletion into scripts or scheduled tasks.
  • Access: Remove files that are hidden or locked in ways the GUI won’t show.

However, the command line also bypasses many safety nets. Files deleted this way often skip the Recycle Bin entirely, so knowing exactly what a command does before running it is critical.

Windows: The del and rmdir Commands

Windows has two built-in commands for deletion, and they serve different purposes:

  • del – deletes one or more files.
  • rmdir (or rd) – removes an empty directory. To remove a directory and everything inside it, use rmdir /s.

Open Command Prompt by typing cmd in the Start menu search and pressing Enter. Then use these examples:

  1. Delete a single file: del C:\Users\YourName\Documents\old.txt
  2. Delete all files with a specific extension: del *.log (in the current folder)
  3. Delete an entire folder and its contents: rmdir /s C:\Temp\OldFolder

If you use PowerShell instead, the Remove-Item cmdlet (alias del or rm) is more flexible. For example, Remove-Item C:\Temp\* -Recurse deletes everything inside the Temp folder.

macOS and Linux: The rm Command

Both macOS Terminal and Linux distributions use the rm (remove) command. The basic syntax is:

rm [options] file1 file2 ...

Common options:

  • -i – prompt before every deletion (safe mode).
  • -r or -R – recursive, deletes directories and their contents.
  • -f – force, ignores nonexistent files and never prompts.
  • -v – verbose, shows what is being deleted.

Examples:

  1. Delete a file: rm report.pdf
  2. Delete a folder and everything inside: rm -r OldProject
  3. Delete all .tmp files in the current folder: rm *.tmp

On macOS, the Trash is not used when you run rm; the file is gone immediately. There is no built-in undo, so double-check the path before pressing Enter.

Deleting Files Permanently vs. Recycle Bin

When you delete a file in Windows Explorer, it goes to the Recycle Bin. When you use del in Command Prompt, the file is permanently deleted and does not go to the Recycle Bin. The same is true for rm on macOS and Linux. This is a common misconception that leads to lost data.

If you want to send a file to the Recycle Bin from the command line, you need a third-party tool or a PowerShell script that uses the Shell.Application COM object. For most users, it is simpler to use the GUI when you want the safety net of the Recycle Bin, and reserve the command line for files you are sure you no longer need.

Using Wildcards to Delete Multiple Files

Wildcards let you target multiple files with one command. The two main wildcards are:

  • * – matches any number of characters (including none).
  • ? – matches exactly one character.

Examples:

  • del *.bak – deletes all files with a .bak extension.
  • rm file?.txt – deletes file1.txt, file2.txt, etc., but not file10.txt.
  • del *2024* – deletes any file with “2024â€� in its name.

Be careful with del *.* – it deletes every file in the current folder. On Windows, del *.* may prompt for confirmation, but on Unix-like systems, rm * will delete everything without asking if you don’t use -i.

Common Problems and How to Avoid Them

Here are the most frequent issues people run into when deleting files from the command line, and how to handle them:

  • “Access is deniedâ€� – The file may be in use by another program, or you lack permission. Close the program, or run the command as administrator (Windows) or with sudo (macOS/Linux) if you own the file.
  • “The file is read-onlyâ€� – On Windows, use del /f to force deletion. On Unix, use rm -f.
  • Accidentally deleting the wrong file – Always list the files first with dir (Windows) or ls (macOS/Linux) to verify the pattern matches what you intend to delete.
  • Deleting a folder that is not empty – On Windows, use rmdir /s; on Unix, use rm -r.
  • File names with spaces – Enclose the entire path in quotes: del "C:\My Files\old report.txt".

Safety Tips: What to Check Before You Press Enter

Because command-line deletion is permanent, adopt these habits:

  1. Use -i or /p when trying a new pattern. On Unix, rm -i *.tmp asks before each deletion. On Windows, del /p *.tmp does the same.
  2. Test with a harmless file first. Create a dummy file like test.txt and run the command on it to confirm the syntax works.
  3. Check your current directory. Run pwd (Unix) or cd (Windows) to see where you are. A stray rm -rf * in your home directory is a disaster.
  4. Back up important data. If you are deleting a large batch, move the files to a temporary folder and verify them before permanent deletion.
  5. Never use rm -rf / or rm -rf ~. These commands attempt to delete the entire system or your home directory. Even with permissions, they can render your OS unusable.

Frequently Asked Questions

Can I recover a file deleted with the command line?

If the file was deleted with del or rm, it is not in the Recycle Bin or Trash. The data may still exist on the disk until overwritten, but recovery requires specialized software and is not guaranteed. The sooner you stop using the drive, the better the chances.

What is the difference between del and rmdir on Windows?

del removes files, while rmdir removes directories. To delete a non-empty directory, you must use rmdir /s, which recursively deletes the folder and everything inside it. del cannot delete folders.

How do I delete a file with a name that starts with a dash?

On Unix-like systems, a file named -file.txt is interpreted as an option. Use rm -- -file.txt to tell the command that the dash is part of the filename. On Windows, this is not an issue because del uses / for options, not -.

Is there an undo command for rm?

No. The rm command has no built-in undo. Some file systems support snapshots, and some editors keep backup copies, but the command itself is permanent. Always use -i when in doubt.

Can I delete files from a different drive or folder without changing directory?

Yes, provide the full path. For example, del D:\OldFiles\*.tmp deletes all .tmp files in the OldFiles folder on the D: drive. On Unix, rm /tmp/*.log works the same way.

Why does rm ask for confirmation on some directories but not others?

On some systems, rm may prompt if the directory is write-protected or if you are deleting a directory with many files. The -f flag suppresses all prompts, while -i forces them.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →