rm Command in Linux with Examples

rm stands for ‘remove’. rm command in Linux is used to delete files and directories. By default, it removes files but not directories. Be careful when using rm, as it permanently deletes files and their contents, and there is no easy way to recover deleted files.

The basic syntax of the rm command is as follows:

rm [OPTION]... FILE...

The FILE argument is the name of the file or directory that you want to delete. The OPTION argument is used to modify the behavior of the rm command.

rm Options:

  • -d : It is used to remove directories as well as other types of files.
  • -f : Force the deletion of files, without prompting for confirmation.
  • -i : Prompt for confirmation before deleting each file.
  • -I: Prompt for confirmation once if more than three files are being removed or if a directory is being recursively removed. This is a less intrusive option than -i, but it still provides good protection against mistakes.
  • -P : 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. This helps to prevent sensitive data from being recovered by someone else.
  • rm -r or R: Delete directories and their contents recursively.

rm Command Examples:

Example 1: Remove a Specific File using the rm command


To remove a particular file in the current directory, use the following command:

rm filename

For example, to remove the file “accounts.txt” in the current directory, type the following command:

$ rm accounts.txt

This command will delete the file “accounts.txt” from the current directory.

Note: Join free Sanfoundry classes at Telegram or Youtube

Example 2: Delete a Directory and Its Contents

To delete an entire directory along with its contents, use the -r option:

rm -r directory_name

For example, If you want to delete an entire directory named “cases” and all its contents, you should use the -r option to remove it recursively.

$ rm -r cases

Example 3: Delete Multiple Files

You can remove multiple files at once using wildcards. For instance, to delete all files with the “.txt” extension:

$ rm *.txt

Example 4: Force Deletion (-f option)


The -f option allows you to forcefully delete a file, even if it’s write-protected or if there are permission issues. For instance, to remove a file named “abc.txt”, you can use the following command:

$ rm -f abc.txt

This command will delete “abc.txt” regardless of any write protection or permission restrictions. The -f option overrides any such restrictions.

Example 5: Prompt for Confirmation (-i option)

The -i option is used to request confirmation before removing each file. For instance, to delete a file named “example.txt” while requesting confirmation, you can use the following command:

$ rm -i example.txt

This command will prompt you for confirmation before deleting the file accounts.txt. If you type y, the file will be deleted. If you type n, the file will not be deleted. This option helps prevent accidental deletions.

Example 6: Remove a File Outside the Current Directory

To remove a file outside the current directory, you can use the full path to the file. For example, to remove the file /home/user/myfile.txt, you would use the following command:

$ rm /home/user/myfile.txt

You can also use wildcards to remove multiple files at once. For example, to remove all of the files in the directory /home/user/mydirectory that end with .txt, you would use the following command:

rm /home/user/mydirectory/*.txt

Be careful when using wildcards, as they can accidentally delete files that you did not intend to delete.

Example 7: Overwrite and Remove a File (-P option)

The -P option is used to overwrite a file before deleting it. This is particularly useful when you want to ensure that the data in the file is not easily recoverable. For example, to overwrite and remove a file named “imp.txt”, you can use the following command:

$ rm -P imp.txt

With the -P option, the file “imp.txt” will be overwritten with specific byte patterns (0xff, 0x00, and 0xff) three times before it is deleted, making it more challenging to recover any sensitive data from the file.

Important tips of using rm command

Here are some important safety tips when using the rm command in Linux:

  • Be careful when using wildcards, as they can accidentally delete files that you did not intend to delete.
  • Always use the -i option when deleting important files or directories.
  • Be aware that the rm command deletes files and directories permanently. There is no easy way to recover deleted files.

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.

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.