Rm Command in Linux with Examples

Rm Command in Linux with Examples

Rm command is used to remove files and directories in Linux. The syntax of the command is as follows.

rm [OPTION] . . . [FILE] . . .

In this article, we have focused on examples of rm command. All the commands have been run on Red Hat Enterprise Linux 9.

Examples:

Following are some examples of rm command.

Delete a file

Suppose you want to delete a file named ‘test.txt’ located in your current working directory. The command will be as follows:

rm test.txt

Delete a directory

Suppose you want to remove a directory ‘sample_directory’ which is located in your current working directory. Execute the command with -r option as follows:

rm -r sample_directory

Prompt before removing a file

If you like the user to be prompted before removing a file. Use the -i option with rm command.

rm -i test.txt

Prompt before removing a directory

If you like the user to be prompted before deleting a directory, use the -I option as follows.

rm -r -I sample_directory

Print what is being done on the terminal

You can print what is being done on the terminal while deleting a file or directory.

I hope you liked this tutorial.

Cheers!!!