rm Command in Linux with Examples


This tutorial explains Linux “rm” command, options and its usage with examples.

rm – remove files or directories


This command is used to delete files. rm removes each specified file. By default, it does not remove directories.

If a file is unwritable, the standard input is a tty, and the -f or –force option is not given, rm prompts the user for whether to remove the file. If the response is not affirmative, the file is skipped.



Note: Join free Sanfoundry classes at Telegram or Youtube


Attempt to remove directories as well as other types of files.

Attempt to remove the files without prompting for confirmation, regardless of the file’s permissions. If the file does not exist, do not display a diagnostic message or modify the exit status to reflect an error. The -f option overrides any previous -i options.

Request confirmation before attempting to remove each file, regardless of the file’s permissions, or whether or not the standard input device is a terminal. The -i option overrides any previous -f options.

Request confirmation once if more than three files are being removed or if a directory is being recursively removed. This is a far less intrusive option than -i yet provides almost the same level of protection against mistakes.

Overwrite regular files before deleting them. Files are overwritten three times, first with the byte pattern 0xff, then 0x00, and then 0xff again, before they are deleted. Specifying this flag for a read only file will cause rm to generate an error message and exit. The file will not be removed or overwritten.

Attempt to remove the file hierarchy rooted in each file argument. The -R option implies the -d option. If the -i option is specified, the user is prompted for confirmation before each directory’s contents are processed (as well as before the attempt is made to remove the directory). If the user does not respond affirmatively, the file hierarchy rooted in that directory is skipped.

Equivalent to -R.



1. To remove the file “accounts.txt” in the current directory you would type

$ rm accounts.txt

2. To delete a directory named “cases” with all its contents you would enter

$ rm -r cases

This assumes that the directory “cases” is a subdirectory of the current directory.


3. In order to delete a file that is not in the current directory you can specify the full path. For example,

$ rm /home/newuser/info

would delete the file “info” in the directory “/home/newuser/”.

4. You can selectively delete a subset of files using the wildcard character “*”. For example,

$ rm *.txt

would remove all files that end with “.txt”.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.