2 ways to Delete Files in Linux

Deleting a file in Linux is simple, but doing it safely takes a little know-how—because on the command line, deletion is usually permanent. In this guide we’ll cover two ways to delete files in Linux: the rm command and the unlink command, plus how to remove directories and how to avoid costly mistakes.

Method 1: Delete files with the rm command

The quickest way to delete a file is the rm command. Open a terminal and type rm followed by the file name:

rm file.txt

To delete several files at once, list them separated by spaces:

rm file1.txt file2.txt file3.txt

By default, rm removes each specified file but does not remove directories.

Quick procedure

  • rm filename — delete a specific file.
  • rm -r dirname — delete a directory and all its contents.
  • rm -f filename — force-delete a file without a confirmation prompt.
  • rm -rf dirname — force-delete a directory and everything in it, no prompt.

rm options you should know

OptionDescription
-iInteractive: prompts for confirmation before removing each file (press y).
-fForce: overrides write protection and removes files without prompting.
-rRecursive: deletes a directory and everything inside it.

Common rm commands

TaskCommand
Delete files with a confirmation promptrm -i file1.txt file2.txt
Delete files forcefully (no prompt)rm -f file1.txt file2.txt
Delete a directory and its contentsrm -r directory/
Delete a directory with no promptrm -rf directory/

With -i, you’ll see a prompt for each file:

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

Delete a directory with rm -r

rm can remove folders when you add -r (recursive). This deletes the folder and everything inside it:

rm -r foldername

Be careful: this permanently deletes the directory and all of its contents. The rm command has no undo.

Delete hidden files with the find command

Hidden files in Linux are those whose names begin with a dot (.). You can find and delete them with find:

find . -type f -name '.*' -exec rm {} \;

This searches the current directory (and its subdirectories) for regular files whose names start with a dot and removes them. Note that -name '.*' matches hidden files, not a “.hidden extension.” Because it recurses, double-check the path before running it. For safer targeting, see the find-exec-rm pattern for deleting files and how to list hidden files in Linux.

Force-delete files in Linux

The -f (force) flag removes files without prompting, even if they’re write-protected:

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

rm -f howtouselinux.txt

Combining -r and -f recursively and forcibly removes a directory and its contents with no confirmation:

rm -rf directory/

Warning: rm -rf is the most dangerous command in this article. It deletes everything under the given path with no prompt and no undo. Never run rm -rf / or rm -rf * unless you are absolutely certain of your current directory and target.

The unlink command removes a single file. Its syntax is simple:

unlink FILENAME

Let’s create a file and remove it. First:

touch test.txt

Now delete it:

unlink test.txt

If we try to read it afterward, it’s gone:

cat test.txt
cat: test.txt: No such file or directory

Two things to remember about unlink:

  • It deletes only one file at a time (no multiple files, no wildcards).
  • It cannot delete directories. Trying to do so returns an error:
unlink mydir
unlink: cannot unlink 'mydir': Is a directory

Deleting directories: rmdir vs rm -r

To delete an empty directory, use rmdir:

rmdir DIRECTORY

If the directory isn’t empty, rmdir refuses:

rmdir: failed to remove 'DIRECTORY': Directory not empty

To remove a non-empty directory and all its contents, use rm -r:

rm -r DIRECTORY

Tips before you delete files in Linux

Because deletion with rm is permanent, a few habits go a long way:

  • Double-check the file name before pressing Enter.
  • Confirm your current directory with pwd—running rm from the wrong place causes accidental loss.
  • Inspect a file first if you’re unsure. ls -lh shows its size and permissions; see understanding the ls command in Linux.
  • Back up first with cp if the file might matter later.
  • Use rm -i to be prompted before each deletion when working with important data.

Remember: once a file is removed with rm, it is gone and generally cannot be recovered.

Summary

GoalCommand
Delete a filerm file.txt
Delete multiple filesrm file1.txt file2.txt
Delete with confirmationrm -i file.txt
Force-delete a filerm -f file.txt
Delete a directory + contentsrm -r dir/
Delete a single file (one at a time)unlink file.txt
Delete an empty directoryrmdir dir/

Use rm for everyday deletions, unlink when you want to remove exactly one file, and rmdir for empty directories—and treat rm -rf with respect.

Avatar photo
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 275