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.
Table of Contents
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
| Option | Description |
|---|---|
-i | Interactive: prompts for confirmation before removing each file (press y). |
-f | Force: overrides write protection and removes files without prompting. |
-r | Recursive: deletes a directory and everything inside it. |
Common rm commands
| Task | Command |
|---|---|
| Delete files with a confirmation prompt | rm -i file1.txt file2.txt |
| Delete files forcefully (no prompt) | rm -f file1.txt file2.txt |
| Delete a directory and its contents | rm -r directory/ |
| Delete a directory with no prompt | rm -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 -rfis the most dangerous command in this article. It deletes everything under the given path with no prompt and no undo. Never runrm -rf /orrm -rf *unless you are absolutely certain of your current directory and target.
Method 2: Delete files with the unlink command
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—runningrmfrom the wrong place causes accidental loss. - Inspect a file first if you’re unsure.
ls -lhshows its size and permissions; see understanding the ls command in Linux. - Back up first with
cpif the file might matter later. - Use
rm -ito 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
| Goal | Command |
|---|---|
| Delete a file | rm file.txt |
| Delete multiple files | rm file1.txt file2.txt |
| Delete with confirmation | rm -i file.txt |
| Force-delete a file | rm -f file.txt |
| Delete a directory + contents | rm -r dir/ |
| Delete a single file (one at a time) | unlink file.txt |
| Delete an empty directory | rmdir 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.



