How to recover deleted files in Linux?

QuestionsQuestions8 SkillsProDec, 20 2025
0170

In the Linux terminal, recovering files is quite difficult because the rm command does not move files to a Trash or Recycle Bin—it unlinks them from the file system immediately.

However, depending on the situation, here are your options:

1. Is there a "Trash" folder?

  • Desktop Interface: If you are using the Desktop UI in LabEx and deleted a file using the file manager (GUI), look for a "Trash" icon on the desktop or in the file explorer.
  • Terminal: If you used rm in the terminal, it bypassed the Trash entirely.

2. Check for Backups or Version Control

  • Git: If you are working in a directory where you have initialized Git (e.g., you see a .git folder), you can recover your file easily:
    git checkout -- filename
  • Editors: Some editors like Vim or Nano occasionally create backup files (ending in ~ or .swp). Look for hidden files using ls -a.

3. Advanced Recovery Tools (Professional Use)

In a real-world Linux system (though usually not available or practical in a temporary lab VM), professionals use tools like:

  • testdisk / photorec: Scans the hard drive for the "raw" data of deleted files.
  • extundelete: Specifically designed for recovery on ext3/ext4 file systems.

Best Practice for the Future: "Safe" Deletion

Since rm is permanent, many Linux users do one of the following to prevent accidents:

  1. Use the Interactive Flag:
    Always use -i so the system asks for confirmation:
    rm -i filename
    # The system will ask: rm: remove regular empty file 'filename'? 
    # Type 'y' for yes.
  2. Move instead of Delete:
    Instead of rm, move files to a temporary "tmp" folder:
    mkdir -p ~/tmp_trash
    mv filename ~/tmp_trash/

In this LabEx environment:
If you accidentally deleted a file required for the lab, the fastest way to "recover" is often to simply recreate it using touch filename or, if you've lost a lot of work, click the "Restart" button for the lab to reset the environment to its original state.

0 Comments

no data
Be the first to share your comment!