Undeleting Files in Linux


  • Ext is most common Linux file system

How Linux Stores Files

  • content of file is stored in contiguous blocks
    • exact size of blocks depends on the parameters used with the command to create the partition
      • 1024, 2048, 4096 bytes
      • similar to clusters in NTFS
  • specific block size is stored in the superblock
  • entire partition is divided into an integral number of blocks, starting at 0
  • blocks are divided into groups
  • each group uses one block as a bitmap to keep track of which block inside that group is allocated
    • there can be at most 32,768 (4096 x 3) normal blocks per group
  • another block is used as a bitmap for the number of allocated inodes
    • an inode is a data structure in the file system that stores all the info about a file except its name and data
    • 128 bytes
    • stored in a table in each group
    • can refer to a file or directory
      • in both cases, it is a link to the file
  • 2 types of links
    • hard link
      • is an inode that links directly to a specific file
      • OS keeps count of references to this link
      • when references count = 0, then file is deleted
    • soft link or symbolic link
      • link is not an actual file, but a pointer to another file or directory
      • similar to shortcut in Windows
  • max of 32,768 inodes per group
    • max 1024 blocks (32768/32) in inode table of each group
  • inodes in the inode table of each group contain metadate for each type of data that the file system can store

Manual Recovery

  • uses Linux commands
    • are variations between distributions
  1. enter single-user mode
    • using wall command
    • init l
    • run levels determine at which level the OS is running
      • init command allows you to change the run level
  2. use grep to search for file text

The extundelete Utility

  • extundelete works with both Ext3 and Ext4 partitions
  • works via shell commands
  • to restore all deleted file from sda4 partition:
    • extundelete /dev/sda4 --restore-all

Scalpel

  • works on both Linux and macOS
  1. first edit configuration file
    • located in /etc/scalpel/scalpel.conf
    • uncomment specific file format you want to recover
  2. run sudo scalpel [device/directory/filename] -o [output directory]
    • output directory must be empty