Linux / UNIX: Delete a file

September 8, 2019

Rate this post

How do I delete a file under a Linux / UNIX / *BSD / AIX / HP-UX operating system using command line options?

To remove or delete a file or directory in Linux, FreeBSD, Solaris, macOS or Unix-like operating systems use the rm command or unlink command. This page explains how to delete given file on a Linux or Unix like system using the command line option.

Syntax: rm command to remove a file

rm (short for remove) is a Unix / Linux command which is used to delete files from a filesystem. Usually, on most filesystems, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). The syntax is as follows to delete the specified files and directories:

rm {file-name}
rm [options] {file-name}
unlink {file-name}
rm -f -r {file-name}


  • -f: Forcefully remove file
  • -r: Remove the contents of directories recursively

When rm command used just with the file names, rm deletes all given files without confirmation by the user.

Remove or delete a file example

Say you have a file named abc.txt and you want to remove it:
$ rm abc.txt

Linux delete multiple files

Delete three files named foo.mp4 bar.doc demo.txt, run:

rm foo.mp4 bar.doc demo.txt

Linux recursively delete all files

Remove all files & subdirectories from a directory (MS-DOS deltree like command), enter:
$ rm -rf mydir

Linux delete a file and prompt before every removal

To request confirmation before attempting to remove each file pass the -i option to the rm command:
$ rm -i filename
Sample outputs:

Gif 01: rm command demo

Pass the -I option to prompt only once before removing more than three files but still providing protection against many mistakes at the cli:
$ rm -I foo.conf bar.conf resume.doc cakeday.png
$ rm -I -r -f ~/olddata/

Force rm command to explain what is being done with file

Pass the -v option as follows:
$ rm -v moiz.list.txt bios-updates.doc
removed 'moiz.list.txt'
removed 'bios-updates.doc'

How to delete empty directories

To remove empty directory use rmdir command and not the rm command:
$ rmdir mydirectory
$ rmdir dirNameHere
$ rmdir docs

How to read a list of all files to delete from a text file

The rm command is often used in conjunction with xargs to supply a list of files to delete. Create a file called file.txt:
$ cat file.txt
List of to delete:


Now delete all file listed in file.txt, enter:
$ xargs rm < file.txt

How do I delete a file named -foo.txt or a directory named -bar?

To delete a file called -foo.txt:
rm -- -foo.txt
rm -- ./-foo.txt
To delete a directory called -bar:
rm -r -f -- -bar
The two -- dashes tells rm command the end of the options and rest of the part is nothing but a file or directory name begins with a dash.

Never run rm -rf / as an administrator or normal UNIX / Linux user

WARNING! These examples will delete all files on your computer if executed.

$ rm -rf /
$ rm -rf *

rm -rf (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Unix disasters. The rm -rf / variant of the command, if run by an administrator, would cause the contents of every writable mounted filesystem on the computer to be deleted. Do not try these commands.

See also:

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.