In Linux, when you delete files using rm -r, the files are permanently removed from the filesystem and are not moved to a 'trash' or 'recycle bin' as in some graphical operating systems. This means that the recovery of these files becomes more challenging and depends on the file system and tools available.
However, the files may still physically exist on the disk until the space they occupied is overwritten by new data. This means there is a chance (although not guaranteed) to recover these files if you act quickly. Here are some steps and tools you can use to try and recover deleted files in Linux:
-
Stop Writing to the Disk: As soon as you realize you've mistakenly deleted important files, you should stop writing new data to the disk. This is to prevent the space occupied by the deleted files from being overwritten.
-
Use File Recovery Tools: There are several tools available for Linux that can help you recover deleted files. A few of them are:
- TestDisk: A powerful open-source software that can recover lost partitions or make non-booting disks bootable again. It can also recover files from FAT, NTFS, exFAT, and ext4 file systems.
- PhotoRec: Typically used to recover lost files including video, documents, and archives from hard disks and CD-ROMs. Despite its name, it's not limited to photos and is often used alongside TestDisk.
- extundelete: For ext3 and ext4 file systems, this tool can recover files that have been deleted.
- Foremost: Originally developed by the US Air Force Office of Special Investigations, Foremost can recover files based on their headers, footers, and internal data structures.
-
How to Use: Each tool has its own set of instructions. For example, to use testdisk, you would typically install it (using your Linux distribution's package manager), launch it from the terminal, and follow the on-screen instructions. Here's a very basic example of how you might install and start TestDisk on a Debian-based system:
sudo apt-get install testdisk
sudo testdisk
-
Recovery Services: If the data is critical and you're not having success with recovery tools, consider leveraging professional data recovery services. These services can be expensive, but they may have more sophisticated methods for recovering lost data.
Important Note:
Recovery success rates can vary greatly depending on how much new data has been written to the disk since deletion, the file system, and the specific recovery tool's capabilities. It's always a good practice to regularly back up important data to avoid the need for recovery.
Prevention:
- Consider using a
trash-cli or similar tool that moves files to a 'trash can' instead of immediately deleting them.
- Always make regular backups of important data.