Sling Academy
Home/DevOps/4 Ways to Recover Accidentally Deleted Files in Ubuntu

4 Ways to Recover Accidentally Deleted Files in Ubuntu

Last updated: January 29, 2024

Introduction

Accidentally deleting important files can be a stressful experience, especially on a robust system like Ubuntu where the command line is often involved. In this tutorial, we’ll explore various methods to recover your lost files on a system running Ubuntu Linux.

Before panicking, it’s crucial to understand that when files are deleted, they aren’t immediately removed from your hard drive. Instead, the space they occupy is just marked as available for new data, which means quick action can often result in successful recovery.

Method 1: Use the ‘TestDisk’ Tool

TestDisk is a powerful open-source tool that can recover lost partitions and non-deleted file recovery.

Installation

sudo apt update
sudo apt install testdisk

File Recovery

sudo testdisk

Follow the on-screen instructions to select your disk and the partition table type, usually the default. Navigate to the Advanced file recovery option, select the partition containing your deleted files, and choose Undelete. Browse through the recoverable files and select the ones you want to restore.

Method 2: Use the ‘PhotoRec’ Tool

PhotoRec is often bundled with TestDisk. It focuses on recovering lost files including videos, documents, and archives from hard disks, CD-ROMs, and lost pictures from camera memory.

Recovery with PhotoRec

sudo photorec

Select the disk, then the partition. Choose the filesystem type and where recovered files should be saved. Then let PhotoRec work its magic.

Method 3: Recover from Trash

If the files were deleted from within a graphical environment, they might just be in the trash can. Open the trash and look for your files. You can right-click and select Restore to move the files back to their original location.

Method 4: Use ‘Extundelete’

For ext3 and ext4 file systems, extundelete is helpful.

Installation

sudo apt update
sudo apt install extundelete

Recovery Usage

sudo extundelete /dev/sdxn --restore-all

Replace /dev/sdxn with the appropriate partition identifier. The recovered files will be placed under the RECOVERED_FILES directory.

Final Thoughts and Best Practices

Timeliness is crucial in data recovery scenarios. It’s also vital to routinely back up your files and to use tools like rsync or Deja Dup available in Ubuntu repositories. The chances of recovery depend greatly on not writing any new data to the affected partition.

Data loss can happen to anyone, but with the proper knowledge and tools, you can recover those precious files on Ubuntu Linux.

Next Article: How to Auto-Reboot a Ubuntu Server When Running Out of Resources

Previous Article: Understanding ‘stdin’, ‘stdout’ and ‘stderr’ in Linux

Series: Linux Tutorials

DevOps

You May Also Like

  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide
  • Terraform: 3 Ways to Remove Duplicates from a List
  • Terraform: How to convert a number to a string and vice versa
  • Using bcrypt() and md5() functions in Terraform