rm (Remove)
100%

Command Line · Lesson 13

rm (Remove)

Learn how to remove files and directories while checking targets and choosing safer rm options.

The rm command removes filesystem entries. Command-line removal normally does not send items to a desktop trash folder, and rm has no built-in undo, so confirm every target before you run it.

The basic syntax is:

rm [OPTIONS] FILE...

Removing Files

Pass one or more file pathnames to rm:

$ rm file1
$ rm notes.txt old-report.txt draft.md

Check spelling and location before pressing Enter. A backup or version-control copy is a more dependable recovery plan than filesystem-recovery tools after deletion.

After confirming the target, which command removes the file old-report.txt?

Previewing Wildcard Targets

The shell can expand a wildcard into several operands. For example, *.tmp selects matching non-hidden names in the current directory:

$ rm *.tmp

Preview the same unquoted pattern with ls before removing anything:

$ ls *.tmp
cache.tmp  test.tmp
$ rm *.tmp

The shell expands the pattern before rm starts. If the preview includes an unexpected file, correct the pattern instead of proceeding.

You plan to remove *.tmp. Which command first shows the non-hidden pathnames selected by that pattern without deleting them?

Requesting Confirmation

The -i option asks before each removal:

$ rm -i important.txt
rm: remove regular file 'important.txt'? y

The -I option is a less intrusive safeguard in GNU rm: it prompts once when a command would remove more than three files or operate recursively.

Which command asks for confirmation before removing each named file?

Ignoring Missing Files with -f

The -f option ignores missing operands and suppresses prompts:

$ rm -f old-cache.txt

This can make scripted cleanup idempotent when a generated file may already be absent. Because it removes confirmation, do not add -f merely to silence an error you have not understood.

Removing Directories

Plain rm does not remove a directory:

$ rm projects
rm: cannot remove 'projects': Is a directory

Use -r or -R only when you intend to remove a directory tree and all of its contents:

$ rm -r old-project

For an empty directory, rmdir is a narrower alternative:

$ rmdir empty-directory

rmdir fails when the directory is not empty, which protects its contents from recursive deletion.

Which command removes old-cache/ only if that directory is empty?

Checking a Recursive Removal

Recursive removal can erase a complete tree. Combining -r with -f also removes prompts, so rm -rf deserves especially careful target validation. Before any recursive removal, check:

  • Are you in the directory you think you are in? Use pwd.
  • Does ls -ld -- TARGET show the intended top-level path?
  • If a wildcard is involved, did a read-only preview match exactly what you expect?
  • Is the path absolute or relative? /tmp/cache and tmp/cache are very different.
  • Is there an accidental space? rm -rf old-project and rm -rf old project target different paths.

Use -- before a target that could begin with a hyphen so it is not interpreted as an option:

$ rm -- -old-name

Do not reach for sudo simply because rm reports a permissions error. First verify the target and determine why your account cannot modify its containing directory. Elevated recursive removal can damage the operating system or other users' data.

Use -v when you want rm to report each successful removal:

$ rm -rv old-project
removed 'old-project/notes.txt'
removed directory 'old-project'

After verifying the complete target, which command removes old-project/ and everything below it while still allowing normal prompts?

To practice removal in a controlled environment, try these hands-on labs:

  1. Linux rm Command: File Removing - Learn how to use the rm command for removing files and directories, including various options like -r and -i, and practice safe and effective file deletion.
  2. Organizing Files and Directories - Practice essential Linux file management skills, including using the rm command to clean up unnecessary directories, in a practical challenge.

Lesson complete

You finished rm (Remove)

You can now remove filesystem entries while treating every target as irreversible.

  • Confirm file pathnames before removal.

  • Preview wildcard expansions with a read-only command.

  • Ask for confirmation with -i or -I.

  • Prefer rmdir when a directory must be empty.

  • Validate an entire target before using recursive removal.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Command Line