How to Restore files from lost+found: 名为 /lost+found 的文件夹是 fsck 将无法附加到目录树中的文件片段的位置。lost+found 目录(不是Lost+Found)是 fsck 在文件系统损坏时使用的结构。通常由于目录损坏而丢失的文件将通过 inode 编号链接到该文件系统的 lost+found 目录中。
/lost+found 是一个重要的目录,可用于恢复由于电源故障等多种原因而未正确关闭的文件。Lost+Found是系统在安装Linux 操作系统(Linux OS)时为我们创建的每个分区创建的。换句话说,我们可以说挂载的文件夹包含这个 lost+found 文件夹。此文件夹包含没有链接的文件和要恢复的文件。任何要恢复的文件都保存在此文件夹中。fsck 命令用于恢复这些文件。
如何从丢失+找到的文件中恢复文件
1.如果您无法开机并看到“继续等待”屏幕;由于 / 和 /home 分区中的文件系统错误,请按 S(Press S)跳过安装或按 M 进行手动恢复”。然后选择恢复选项。
2.fsck on both / and /home文件系统上运行 fsck。
/home清除 fsck 时遇到问题,请使用:
fsck -p /home (Automatic fixing)
"fsck -y /home" (this would select yes for all the prompts automatically).
4.现在您可以pass /home from fsck successfully.
5.如果你尝试挂载/homelost+found directory.之外,不会有任何用户文件。运行“ df -h ”,您将看到您的文件系统将使用与崩溃前相同的空间,因为所有文件都在 lost+found 目录中,我们将恢复它们。
6.现在在lost+found文件夹中,你会看到有大量没有名字的文件夹,仔细检查每个文件夹会浪费你很多时间。所以接下来我们应该运行“ file * ”来了解我们正在处理的文件类型。
#7479417: directory
#7479418: directory
#7479419: directory
#7602560: directory
#7603310: MPEG sequence, v2, program multiplex
#7603464: JPEG image data, JFIF standard 1.02
#7603542: Audio file with ID3 version 2.3.0, contains: Audio file with ID3 version 2.4.0, contains: MPEG ADTS, layer III, v1, 320 kbps, 44.1 kHz, JntStereo
#7604043: ISO 9660 CD-ROM filesystem data 'Linux Mint 9 Isadora ' (bootable)
#7604089: ISO 9660 CD-ROM filesystem data 'netbook-x86_64-201005242312 ' (bootable)
#7605425: MPEG sequence, v2, program multiplex
#7605470: MPEG sequence, v2, program multiplex
#7605484: MPEG sequence, v2, program multiplex
#7607478: gzip compressed data, from Unix, last modified: Fri May 14 07:55:35 2017
#7607788: gzip compressed data, from Unix, last modified: Tue May 4 14:16:31 2017
#7610801: PDF document, version 1.4
#7612061: ISO 9660 CD-ROM filesystem data 'Ubuntu 10.04 LTS amd64 ' (bootable)
#7613228: directory
#7613583: directory
#7613588: directory
7.现在让我们过滤掉结果,因为我们只想将目录包含在我们的结果中。所以运行“file * | grep directory > /root/list.dir ”命令。
8.接下来,编辑/root/list.dir使其成为一个脚本,以显示每个目录中的文件列表。使用 vi、vim、nano 编辑文件以使其成为脚本。将(Add)set -v 添加到第一行,并将“ ls -l ”添加到每隔一行的开头。
set -v
ls -l \#10104455
ls -l \#10104531
ls -l \#10104536
9.现在使文件可执行( file executable)然后运行它并将输出重定向到一个文件:
chmod -x /tmp/list.dir
./list.dir 1> /tmp/list.out 2>&1
10.现在在 dir.out 输出文件中(Desktop in the dir.out output file)搜索文件,例如Desktop 。结果将是这样的:
ls -l \#7733249
total 5704
drwxr-xr-x 2 1002 1002 4096 2017-06-05 13:24 Desktop
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Documents
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Downloads
-rw-r--r-- 1 1002 1002 179 2017-04-30 09:14 examples.desktop
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Music
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Pictures
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Public
-rw-r--r-- 1 1002 1002 5794003 2017-05-15 11:00 scrap001_a.ora
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Templates
-rw-r--r-- 1 1002 1002 1265 2017-05-20 14:35 tinda chuteny
drwxrwxr-x 2 1002 1002 4096 2017-05-23 14:57 Ubuntu One
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Videos
11.上面的输出指定主目录是 #7733249。现在要恢复主文件夹,只需 mv 文件夹:
mv /lost+found/#7733249 /home/yourUsername
注意:将您的用户名替换为您的Linux 安装的实际用户名。(Linux installation.)
方法二:使用脚本自动恢复文件
首先,运行“ sudo -i ”或“ sudo su – ”,然后运行以下在文件系统 /dev/sd 上运行的脚本?并输出到 /tmp/listing:
fsck -y /dev/sd??
mkdir /tmp/recover
mount /dev/sd?? /tmp/recover -o rw
cd /tmp/recover-lost+found
(
echo ‘set -v’
file * | grep directory 2>/dev/null | perl -pe ‘s/^(\#[0-9]+)\:.*$/ls -l ‘”‘”‘$1'”‘”‘/’
) | sh > /tmp/listing
就是这样,您已经成功地学习了 How to Restore files from lost+found但是如果您对本文仍有任何疑问,请随时在评论部分提出。
How to Restore files from lost+found
How to Restore files from lost+found: The folder entitled /lost+found is where fsck puts fragments of files that it hasn’t been able to attach anywhere in the directory tree. The lost+found directory (not Lost+Found) is a construct used by fsck when there is damage to the filesystem. Files that would normally be lost because of directory corruption would be linked in that filesystem’s lost+found directory by inode number.
/lost+found is an important directory which is useful for recovering files which are not properly closed due to many reasons such as power failure. Lost+Found is created by the system at the time of Linux OS installation for each partition we create. In other words, we can say the mounted folder contains this lost+found folder. This folder contains the files with no links and files to be recovered. Any file to be recovered is kept in this folder. fsck command is used to recover these files.
How to Restore files from lost+found
1.If you are unable to boot and seeing the screen “Continue to wait; Press S to skip mounting or M for manual recovery” due to file system error in / and /home partitions. Then select the recovery option.
2.Run fsck on both / and /home file systems.
3.If you are having trouble in getting fsck cleared for /home then use:
fsck -p /home (Automatic fixing)
"fsck -y /home" (this would select yes for all the prompts automatically).
4.Now you would be able to pass /home from fsck successfully.
5.If you will try mount /home there won’t be any user files excpet lost+found directory. Run “df -h” and you will see that your file system will be using the same space as before the crash because all the files are in the lost+found directory and we are going to recover them.
6.Now in the lost+found folder, you will see that there are a large number of folders without a name and scrutinizing each one will waste so much of your time. So next we should run “file *” to know which type of file we are dealing with.
#7479417: directory
#7479418: directory
#7479419: directory
#7602560: directory
#7603310: MPEG sequence, v2, program multiplex
#7603464: JPEG image data, JFIF standard 1.02
#7603542: Audio file with ID3 version 2.3.0, contains: Audio file with ID3 version 2.4.0, contains: MPEG ADTS, layer III, v1, 320 kbps, 44.1 kHz, JntStereo
#7604043: ISO 9660 CD-ROM filesystem data 'Linux Mint 9 Isadora ' (bootable)
#7604089: ISO 9660 CD-ROM filesystem data 'netbook-x86_64-201005242312 ' (bootable)
#7605425: MPEG sequence, v2, program multiplex
#7605470: MPEG sequence, v2, program multiplex
#7605484: MPEG sequence, v2, program multiplex
#7607478: gzip compressed data, from Unix, last modified: Fri May 14 07:55:35 2017
#7607788: gzip compressed data, from Unix, last modified: Tue May 4 14:16:31 2017
#7610801: PDF document, version 1.4
#7612061: ISO 9660 CD-ROM filesystem data 'Ubuntu 10.04 LTS amd64 ' (bootable)
#7613228: directory
#7613583: directory
#7613588: directory
7.Now let’s filter out the result because we just want the directory to be included in our result. So run “file * | grep directory > /root/list.dir” command.
8.Next, edit /root/list.dir to make it a script to show the list of files in each directory. Edit the file to make it a script using vi, vim, nano. Add set -v to the first line and add ‘ls -l‘ to the beginning of every other line.
set -v
ls -l \#10104455
ls -l \#10104531
ls -l \#10104536
9.Now make the file executable then run it and redirect the output to a file:
chmod -x /tmp/list.dir
./list.dir 1> /tmp/list.out 2>&1
10.Now search for the file e.g. Desktop in the dir.out output file. The result will be something like this:
ls -l \#7733249
total 5704
drwxr-xr-x 2 1002 1002 4096 2017-06-05 13:24 Desktop
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Documents
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Downloads
-rw-r--r-- 1 1002 1002 179 2017-04-30 09:14 examples.desktop
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Music
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Pictures
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Public
-rw-r--r-- 1 1002 1002 5794003 2017-05-15 11:00 scrap001_a.ora
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Templates
-rw-r--r-- 1 1002 1002 1265 2017-05-20 14:35 tinda chuteny
drwxrwxr-x 2 1002 1002 4096 2017-05-23 14:57 Ubuntu One
drwxr-xr-x 2 1002 1002 4096 2017-04-30 09:15 Videos
11.The above output specified that the home directory is #7733249. Now to restore the home folder just mv the folder:
mv /lost+found/#7733249 /home/yourUsername
Note: Replace your username with the actual username of your Linux installation.
Method 2: Use the script to automatically recover files
First, run “sudo -i” or a “sudo su –” and then run the below script which runs on filesystem /dev/sd?? and outputs to /tmp/listing:
fsck -y /dev/sd??
mkdir /tmp/recover
mount /dev/sd?? /tmp/recover -o rw
cd /tmp/recover-lost+found
(
echo ‘set -v’
file * | grep directory 2>/dev/null | perl -pe ‘s/^(\#[0-9]+)\:.*$/ls -l ‘”‘”‘$1'”‘”‘/’
) | sh > /tmp/listing
That’s it you have successfully learned How to Restore files from lost+found but if you still have any question regarding this article feel free to ask them in the comment’s section.