To prevent accidental deletion when using the rm command, you can follow these practices:
-
Use the
-ioption: This option prompts you for confirmation before each file is deleted.rm -i filename.txt -
Use the
-Ioption: This option prompts you once before deleting more than three files or when deleting recursively.rm -I *.txt -
Use
--preserve-root: When usingrm -rf, this option prevents the accidental deletion of the root directory.rm -rf --preserve-root / -
Create aliases: You can create an alias for
rmthat includes the-ioption by adding the following line to your shell configuration file (e.g.,.bashrcor.zshrc):alias rm='rm -i' -
Use a trash command: Instead of
rm, use a command that moves files to a trash directory, such astrash-cliorgvfs-trash, which allows for easier recovery. -
Regular backups: Maintain regular backups of important files to recover them in case of accidental deletion.
By implementing these practices, you can significantly reduce the risk of accidental deletions.
