What is the difference between the ‘rm’ and ‘rm -r’ commands in Unix/Linux, and when would you use each one?

1 Answers
Answered by suresh

Unix Interview Question: Difference Between 'rm' and 'rm -r' Commands

What is the difference between the 'rm' and 'rm -r' commands in Unix/Linux?

The rm command is used to delete files in Unix/Linux system. When using rm, you can only delete individual files, and if you try to delete a directory with rm, an error will be generated. On the other hand, the rm -r command is used to delete directories and their contents recursively.

When would you use each one?

  • Use rm when you want to delete individual files or empty directories.
  • Use rm -r when you want to delete directories and their contents recursively.

It is important to be cautious when using the rm -r command as it will not ask for confirmation before deleting files and directories. Make sure you are absolutely certain before using this command to avoid accidental deletion of important data.

Answer for Question: What is the difference between the ‘rm’ and ‘rm -r’ commands in Unix/Linux, and when would you use each one?