ShareYourGroupShareYourGroupCommands

Linux and Mac commands

rm: delete files and folders

rm removes files. There is no Recycle Bin or Trash behind it, and no undo. A file removed with rm is gone. Most stories of lost work on Linux begin with this command, so this page spends more time on caution than on options.

Danger. Before any rm, run pwd to see where you are and ls with the same pattern to see what will be deleted.

Removing a file

Linux or Mac terminal
asha@pc:~/demo$ ls
log.txt
marks.csv
notes.txt
old
photos
report.txt
asha@pc:~/demo$ rm notes.txt
asha@pc:~/demo$ ls
log.txt
marks.csv
old
photos
report.txt

Silence means success.

Ask first: -i

-i asks about each file. It is the option to use while learning, and whenever a wildcard is involved. Shown here without being run:

Linux or Mac terminal not run here
rm -i *.txt

The terminal asks rm: remove regular file 'report.txt'? for each one, and waits for y or n.

-I, a capital, asks once when more than three files or a folder are about to go. It is a good balance for everyday use.

Patterns

Linux or Mac terminal
asha@pc:~/demo$ ls *.csv
marks.csv
asha@pc:~/demo$ rm -v *.csv
removed 'marks.csv'
asha@pc:~/demo$ ls
log.txt
old
photos
report.txt

-v reports each file as it is removed. The ls beforehand is the safety check: whatever it lists is what rm will delete.

Be careful with spaces in patterns. rm *.txt deletes text files. rm * .txt, with a stray space, deletes everything in the folder and then complains that it cannot find a file called .txt.

Folders: -r

rm refuses folders unless given -r, which removes the folder and everything inside it, all the way down.

Linux or Mac terminal
asha@pc:~/demo$ rm old
rm: cannot remove 'old': Is a directory
asha@pc:~/demo$ rm -r old
asha@pc:~/demo$ ls -F
log.txt
photos/
report.txt

-f, and the famous combination

-f means force: do not ask, and do not report files that are missing. Joined with -r it gives rm -rf, which removes a folder tree without a single question.

Danger. rm -rf followed by the wrong path is the most destructive ordinary command on Linux and the Mac. rm -rf ~ removes your entire home folder. rm -rf / or rm -rf /* attempts to remove the whole system. A single mistyped character, a space in the wrong place, or an empty variable in a script is enough. Never run an rm -rf line copied from a website or a chat message. Never run it with sudo unless you could explain every character to someone else.

Classic accidents:

A safe routine

  1. pwd. Read it.
  2. ls with the exact pattern or path you are about to delete.
  3. Replace ls with rm, by pressing the up arrow and editing the first word, so that the rest is identical.
  4. Use -i or -I for anything with a wildcard.
  5. For folders, list the contents first with ls -R.
  6. In scripts, check that variables are not empty before using them in a path, and prefer rm -r -- "$dir" with quotation marks.

Move instead of delete

When in doubt, do not delete. Move things into a holding folder and delete that later.

Linux or Mac terminal not run here
mkdir -p ~/to-delete
mv oldstuff ~/to-delete/

If nothing has been missed after a week, remove the holding folder.

On a desktop system, you can also send files to the real Trash from the terminal. On a Mac, Finder's Trash can be reached with tools such as the trash command, where installed. On Linux, gio trash filename moves a file to the desktop's Trash, from which it can be restored.

Files with awkward names

"Permission denied"

You can delete a file only if you may write to the folder that holds it. System files are protected for a reason. If you find yourself reaching for sudo rm, stop and make sure of the path first.

Can a removed file be recovered?

Rarely, and not dependably. The data may remain on the disk for a while, and specialist tools can sometimes find it, more often on older hard disks than on solid-state drives. If it matters, stop using the disk at once and get expert help. The only reliable protection is a backup made before the accident.

Emptying a file without deleting it

To clear a file's contents and keep the file, for example a log that has grown too large, redirect nothing into it: > log.txt, or truncate -s 0 log.txt.

The Windows equivalent is del.

Examples shown with output were run on 12 November 2026 in a practice folder: Windows commands in cmd.exe on Windows 11, Linux commands with the GNU tools of Git Bash. The folder path and user name were replaced with made-up ones. Commands marked "not run here" are shown as you would type them.

This is an independent learning site. It is not connected with Microsoft, Apple or any Linux distribution. Windows, macOS and other names belong to their owners. Try commands first in a practice folder, never on files you cannot afford to lose.