我有一个文件已被删除,但仍被程序打开。我使用 lsof 找到了 inode 编号。我怎样才能创建一个硬链接回到那个inode?
sun*_*256 31
您无法创建指向它的链接,但可以将其取回。我们来做一个实验:
$ echo blurfl >myfile.txt
$ tail -f myfile.txt &
$ rm myfile.txt
Run Code Online (Sandbox Code Playgroud)
myfile.txt 现在消失了,但 inode 被 tail 命令保持活动状态。要取回您的文件,请首先找到保留 inode 的进程的 PID:
$ ps auxw | grep tail
sunny 409 0.0 0.0 8532 824 pts/5 S 18:07 0:00 tail -f myfile.txt
Run Code Online (Sandbox Code Playgroud)
PID 是 409。chdir 到 /proc/409/fd/ 并列出内容:
dr-x------ 2 sunny sunny 0 2009-07-24 18:07:18 .
dr-xr-xr-x 7 sunny sunny 0 2009-07-24 18:07:17 ..
lrwx------ 1 sunny sunny 64 2009-07-24 18:07:33 0 -> /dev/pts/5
lrwx------ 1 sunny sunny 64 2009-07-24 18:07:33 1 -> /dev/pts/5
lrwx------ 1 sunny sunny 64 2009-07-24 18:07:18 2 -> /dev/pts/5
lr-x------ 1 sunny sunny 64 2009-07-24 18:07:33 3 -> /home/sunny/tmp/myfile.txt (deleted)
Run Code Online (Sandbox Code Playgroud)
/proc/[PID]/fd/ 目录包含指向进程使用的所有文件的文件描述符的符号链接。在这种情况下,符号链接“3”指向已删除的文件。因此,要恢复文件,请将内容复制到新文件中:
$ cat 3 >/home/mydir/saved_file.txt
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3890 次 |
| 最近记录: |