A2oz

How do I delete a file in Linux?

Published in Linux 2 mins read

You can delete a file in Linux using the rm command. This command permanently removes files from your system.

Here's how you can use it:

  1. Open a terminal: You can access the terminal by pressing Ctrl+Alt+T or searching for "Terminal" in your applications menu.
  2. Navigate to the directory containing the file: You can use the cd command to change directories. For example, to navigate to the Documents directory, you would type cd Documents and press Enter.
  3. Use the rm command: To delete a file, type rm followed by the file name. For example, to delete a file named example.txt, you would type rm example.txt and press Enter.

Example:

rm example.txt

Important Notes:

  • Be careful! Once a file is deleted with rm, it cannot be recovered unless you have a backup.
  • Use the -i flag for confirmation: If you want to be prompted before deleting each file, you can use the -i flag with the rm command. For example, rm -i example.txt will prompt you to confirm the deletion before proceeding.
  • Delete multiple files: You can delete multiple files by separating their names with spaces. For example, rm file1.txt file2.txt file3.txt.
  • Delete all files in a directory: Be extremely cautious when using the rm command with the -r flag, as this will recursively delete all files and subdirectories within a directory. For example, rm -r directory_name will delete the entire directory and its contents.

Remember: Deleting files is a permanent action. Always be sure to back up important files before deleting them.

Related Articles