Skip to main content

How to Delete a File in the Terminal with rm

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A file you don't need any more takes one command to delete from the terminal: rm on macOS and Linux, and Remove-Item in PowerShell. Both take several files at once, and neither asks first.

Syntax​

On macOS and Linux:

Syntax
rm <file> [<file> ...]
PartWhat it does
rmShort for "remove". Deletes the files you name.
<file>The file's name, or a path such as notes/old.txt. A name on its own means a file in the folder you're in.
[<file> ...]Optional. More files, separated by spaces. rm deletes them all.

In PowerShell:

Syntax
Remove-Item <path>[, <path> ...]
PartWhat it does
Remove-ItemDeletes the items you name, such as files.
<path>The file's name or path.
[, <path> ...]Optional. More paths, separated by commas, not spaces.

rm prints nothing when it works. Microsoft's reference says the same of Remove-Item: "This cmdlet returns no output."

The Example​

The example works in a folder called notes that holds three empty text files: draft.txt, notes.txt and old.txt. It deletes old.txt, then the other two with one command, and then tries to delete old.txt again.

Step 1: Create Three Files to Delete​

In an empty folder, create the three files:

Terminal
touch draft.txt notes.txt old.txt
ls
Output
draft.txt
notes.txt
old.txt

touch creates an empty file for each name, and ls lists what's in the folder.

Step 2: Delete One File​

Terminal
rm old.txt
ls
Output
draft.txt
notes.txt

rm itself printed nothing. The list shows that old.txt is gone and the other two are still there.

Step 3: Delete Several Files at Once​

Terminal
rm draft.txt notes.txt
ls

This time ls prints nothing at all: the folder is empty.

A macOS-style terminal window with two commented commands: rm old.txt to delete one file, and rm draft.txt notes.txt to delete two files at once.

Step 4: Delete a File That Isn't There​

Run the first delete again, now that old.txt is gone:

Terminal
rm old.txt
Output
rm: old.txt: No such file or directory

Both refuse with a message that names the missing file, and nothing else changes.

Common Mistakes​

MistakeWhat happensDo this instead
Spaces between paths in PowerShell: Remove-Item draft.txt notes.txtRemove-Item: A positional parameter cannot be found that accepts argument 'notes.txt'., and nothing is deletedSeparate the paths with a comma
A name with a space and no quotes: rm old notes.txtrm reads two names, old and notes.txt. With a file called old notes.txt and another called notes.txt in the folder, it printed rm: old: No such file or directory and deleted notes.txt, the wrong fileQuote the name: rm "old notes.txt"
Running the command in the wrong folderThe file deleted is the one in the folder you're in, if one has that nameCheck where you are with pwd first
Expecting to be asked firstNeither command asks for a normal fileCheck the name before you press Enter

Troubleshooting​

SymptomLikely causeFix
rm: old.txt: No such file or directoryThe file isn't in the folder you're in, or its name is spelled differentlyRun ls to see the names, or give the full path
rm: notes: is a directoryThe name belongs to a folder, and rm deletes filesDelete the files inside it, or check you named the right thing
Remove-Item: Cannot find path '…' because it does not exist.Same as the first rowRun Get-ChildItem -Name to see the names
Remove-Item: A positional parameter cannot be found that accepts argument '…'.Two paths separated by a spacePut a comma between them

FAQ​

Does rm delete folders?​

Not as used here. Its manual page describes rm as removing "the non-directory type files specified on the command line". Given the example's folder, it answered rm: ../notes: is a directory and left the folder alone.

Is rm the same as Remove-Item?​

On Windows, PowerShell's rm is a shorter name for Remove-Item. Microsoft lists it among the command's aliases, together with del. On macOS and Linux, typing rm in PowerShell runs the system's own rm from the first tab.

Can I delete many files that share a name pattern?​

Yes. Both commands accept a wildcard such as *.txt in place of the names, and the pattern decides which files go.

Conclusion​

rm <file> deletes a file on macOS and Linux, and rm <file> <file> deletes several. In PowerShell, Remove-Item <path> does the same, with commas between paths. Neither prints anything when it works, and both name the file when it isn't there.

Additional Resources​