Unix tip: Rescuing files from lost+found

By Sandra Henry-Stocker, ITworld.com |  Open Source, lost found, Sandra Henry-Stocker 2 comments

The lost+found directory included in (Unix) file systems is usually empty. Only used when fsck doesn't know what to do with files that have lost their place in the file system, they stand as a temporary holding place for those rare instances in which fsck can't put everything back together after file systems have become corrupt in some way. If you see files in lost+found, you can expect them to look rather unusual. Take these files found on a Solaris 9 system after a panic and a subsequent fsck:

  -rw-r--r-- 1 johndoe staff 1576 Apr 22 11:26 #00805349
  -rw-r--r-- 1 johndoe staff 4363 Apr 22 11:26 #00805350
  -rw-r--r-- 1 johndoe staff 4566 Apr 22 11:26 #00805351

The names of these files were lost before fsck tried to piece things back together. The directory that had originally contained the salvaged files was, for some reason, not recoverable. And, since filenames are stored in directory files and nowhere else on ufs file systems, what we are able to recover for these files are the various pieces of information stored in the files' inodes — the owners, groups, access permissions and pointers to the files' contents.

To examine one of these files, put quotes around the current names. These represent the inode numbers. For example, #00805349 is the file associated with inode 805349. The command shown below will page through the file contents as would more on any normal file:

  # more "#00805349"

You might also find directories in lost+found. In this case, the parent directories of those directories will also have been lost

  drwxr-xr-x 2 johndoe staff 512 Apr 21 10:26 #00804845
  drwxr-xr-x 2 johndoe staff 512 Apr 21 10:46 #00804854
  drwxr-xr-x 2 johndoe staff 512 Apr 21 10:46 #00804866
  drwxr-xr-x 2 johndoe staff 28160 Apr 22 10:45 #01514211
  drwxr-xr-x 2 johndoe staff 28160 Apr 22 10:48 #01514212
  drwxr-xr-x 2 johndoe staff 30208 Apr 22 11:26 #02383477

If you use a cd command such as this to move into one of these directories, you are not going to see any files:

  # cd "#02383477"
  # ls
  #

While these directories may appear to be empty, however, this is only partly true. The files that once occupied these directories may be among the files now represented only by their inodes. On the other hand,the filenames will still be contained in the directory files and you can extract them if this information is of any value. With no connection to the lost files, I have found directory files in lost+found to be of little value. The revelation that some unknown directory used to contain a file named "install.sh" is not likely to be of significant help in putting the lost files back in place. In the lost+found directory files that I examined, the inode numbers are generally gone and only the scattered file names are easily retrieved. The directory dump shown below, for example, shows that the now detached directory once contained four files — root-of-node, build-in-progress, bin and hdr.

  bash-2.05# od -xc "#00804845"
  0000000 000f e6cf 000c 0001 2e00 0000 0000 0003
  \0 017 æ Ï \0 \f \0 001 . \0 \0 \0 \0 \0 \0 003
  0000020 01f4 0002 2e2e 0000 0000 0000 000c 0002
  001 ô \0 002 . . \0 \0 \0 \0 \0 \0 \0 \f \0 002
  0000040 6363 0000 0000 0000 0034 000c 726f 6f74
  c c \0 \0 \0 \0 \0 \0 \0 4 \0 \f r o o t
  0000060 2d6f 662d 6e6f 6465 0000 0000 0000 0000
  - o f - n o d e \0 \0 \0 \0 \0 \0 \0 \0
  0000100 001c 0011 6275 696c 642d 696e 2d70 726f
  \0 034 \0 021 b u i l d - i n - p r o
  0000120 6772 6573 7300 0000 0000 0000 000c 0003
  g r e s s \0 \0 \0 \0 \0 \0 \0 \0 \f \0 003
  0000140 6269 6e00 0000 0000 019c 0003 6864 7200
  b i n \0 \0 \0 \0 \0 001 234 \0 003 h d r \0
  0000160 0000 0000 0000 0000 0000 0000 0000 0000
  \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
  *
  0001000

Moving the files back to their proper places in the OS and renaming them is largely an exercise in recognizing them and issuing the proper mkdir and mv commands to rebuild the contexts in which they previously resided. This can be easy or next to impossible depending on how many files have been dislodged and dumped into lost+found and how recognizable they are. In general, it is probably far less work to pull files from backup than it is to resurrect them from their lost+found orphanages. The prime factor is probably whether it's easier to notice what's been lost from your directories or to identify what's been found and saved in lost+found.

Unix commands such as the file and strings commands can be somewhat helpful in identifying file contents. The file command might tell you that some of the files are scripts or binaries. Strings might help you to recognize binary files.

2 comments

    Anonymous 3 years ago
    Most of what you wrote is highly inaccurate and very incomplete. And when someone begins looking in their lost+found folder its for a reason,not just to look around. So, you should have taken any inquiries more to heart.People looking for recovery of large groups of files that are listed here need methods of recovery not inaccurate commands that result in errors and a passe way of measuring the importance of the files that might be located here.
    Anonymous 3 years ago in reply to Anonymous
    Jim, I wish you would have clarified what was inaccurate and provided some resources and more information. I thought the article was very incomplete, to the point of not being useful to anyone even halfway command line savvy. Other than mentioning the filenames representing inode numbers, the rest of the article was fluff. It was rather disappointing considering the poster is co-author of the O'Reilly Backup and Reocvery book.The kind of information I expected to find was, "How do you piece those lost+found files back together with any precision if they are pieces of binary files?" That would have been very useful for anyone trying to dig out from a corrupt file system. Of course the real answer is, keep a current backup with tested recovery routines to reconstruct a drive after such a "crash."Please shsre what you know regarding recovery with lost+found files. Thanks!

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      Open SourceWhite Papers & Webcasts

      White Paper

      Consolidating SAP Applications to Linux on Power by IDC

      IDC studied a group of enterprises that had deployed SAP applications on IBM Power Systems servers running Linux server operating environments and had been working with those systems for several years. Learn about the results...

      White Paper

      An Interactive eGuide: Open Source

      By now, enterprises are well aware of the benefits of open-source software, which boasts a clean design, reliability, and maintainability, as well as support for standards and community values. But perhaps the biggest benefit is quality; since open-source software users have access to source code, bug fixes and enhancements come from multiple sources, often resulting in superior software.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question