意外删除重要文件是许多计算机用户经历过的噩梦。幸运的是,使用Linux是有希望的,因为可以使用多种工具和技术来恢复文件。
在深入恢复过程之前,请确保立即卸载受影响的驱动器,以避免新数据覆盖已删除的文件。此步骤增加了成功恢复文件的可能性。
如果您不确定如何在Linux上恢复已删除的文件,请按照以下步骤操作。

了解Linux 文件管理(Linux File Management)
在Linux(Linux) PC上恢复已删除的文件之前,最好了解这是如何实现的。这完全取决于Linux文件系统上文件的管理方式。
Linux上的文件使用 inode,其中包含文件大小和位置等重要信息。当您删除文件时,索引节点会被标记为空闲 - 从技术上讲,数据仍然存在,但您将无法访问它。随着时间的推移,新文件将覆盖旧文件,随着时间的推移,文件恢复变得更加困难。
虽然没有内置的取消删除命令,但您可以根据您的使用情况使用TestDisk和PhotoRec等工具恢复文件。(PhotoRec)意识到文件丢失后必须迅速采取行动,因为数据可能会很快被覆盖。
然而,恢复并不能替代预防。为了避免将来出现这种情况,请确保定期备份文件(back up your files regularly)并使用清晰的文件夹结构,以尽量减少意外删除的风险。
在Linux(Linux)上恢复文件之前
在Linux(Linux)上恢复已删除的数据时,您应该采取一些重要的预防措施以确保成功恢复。
避免覆盖数据
恢复丢失文件的一个重要方面是确保您不会覆盖要恢复的数据。为此,您应该在意识到文件丢失后立即停止使用受影响的驱动器。
然后,您可以使用Live CD 或USB驱动器上的(USB)便携式 Linux 发行版(portable Linux distribution)来引导系统并执行恢复过程。您还应该确保不要将恢复的文件保存到同一驱动器 - 将它们存储在不同的驱动器上以防止进一步覆盖。
数据备份
为了保护您的数据,最好制定备份计划(have a backup plan)。拥有可靠的备份意味着您无需过多担心数据恢复过程。
制定定期备份计划(无论是每天、每周还是每月)是个好主意。您还应该尝试将备份存储在多个位置(物理位置和在线位置),以防止因硬件故障或自然灾害而导致数据丢失。
如果您做不到这一点,您应该尝试使用Dropbox 或 Google Drive(Dropbox or Google Drive)等云存储解决方案来将最重要的文件存储在远离物理位置的地方。
使用安全命令
在Linux(Linux) PC上工作时,您应该尝试遵循一些最佳实践,以避免意外覆盖或删除文件。
考虑到这一点,您应该熟悉 PC 上的 cp(复制)、mv(移动)和 df(显示文件系统)等命令。了解它们的正确用法和语法 - 如果您不确定,请在命令行或终端中键入 man 命令(例如 man cp)以了解更多信息。
一旦确定,在处理重要文件时就应该格外小心。在运行命令之前务必仔细检查它们。避免覆盖文件的一个好方法是在 cp 和 mv 命令中使用 –no-clobber 标志,这将阻止命令覆盖任何现有文件。
如果您尝试使用 rm(删除)删除文件,请考虑使用 -i(交互式)标志,该标志会在删除文件之前提示确认。
通过采取这些预防措施,您可以最大限度地减少意外丢失或覆盖重要文件的可能性——毕竟,预防比恢复要好得多。
使用 TestDisk(Linux Using TestDisk)在Linux 上恢复已删除的文件(Deleted Files)
TestDisk是一个在(TestDisk)Linux PC上恢复已删除文件的有用工具,它是一个专为数据恢复而设计的开源工具。当文件系统结构损坏或错误格式化驱动器时, TestDisk(TestDisk)可用于从硬盘驱动器恢复整个丢失的分区。
如果您想恢复单个文件,可以使用 TestDisk 的配套应用程序PhotoRec。我们在下一节中概述了如何使用它。
在基于Ubuntu和 Debian 的操作系统上,您可以通过打开终端并使用命令 sudo apt install testdisk 以正确的权限开始安装来
快速安装TestDisk 。

查找丢失的分区
安装TestDisk后,第一步是识别您正在使用的分区。通过在终端中键入 sudo testdisk启动TestDisk 。

出现提示时,选择“创建”(Create)以启动新的日志文件 - 按Enter确认。

选择您的硬盘驱动器并使用箭头键选择继续,然后按(Proceed)Enter。

出现提示时,选择分区类型并按Enter。大多数用户需要选择Intel,但请注意底部的提示 - 在大多数情况下,该提示是准确的。

此时,您已准备好继续。您可以选择“分析”(Analyse)来搜索当前分区,或按“高级”(Advanced)来访问其他文件系统恢复工具。

准备就绪后,选择“快速搜索”(Quick Search)以搜索丢失的分区,然后按Enter。

使用 TestDisk 恢复文件
TestDisk将搜索丢失的分区。如果找不到它们,请选择“更深层次搜索”(Deeper Search)并允许搜索完成。一旦找到它们,您就可以尝试恢复它们(以及任何丢失的文件)。
为此,请使用箭头键选择一个分区,然后按 p 列出该分区上的文件。

如果是正确的分区,请按 q(小写)返回上一菜单,然后按键盘上的向左或向右箭头键将分区从已删除更新为标准逻辑或主分区。按 Enter 键(Press Enter)确认更改。要将更改写入分区表,请选择Write,然后Enter。

该过程完成后,您应该能够正常安装驱动器并查看文件。
使用 PhotoRec(Deleted Files Using PhotoRec)恢复已删除的文件
如果您只需要恢复少量文件(而不是整个驱动器或分区),您可能更喜欢使用PhotoRec。PhotoRec是 TestDisk 的配套恢复工具- 它(TestDisk—it)要求您先安装TestDisk才能使用它。
Photorec 支持大量文件扩展名(huge number of file extensions),从 ZIP 文件到媒体文件。
运行照片录制
使用PhotoRec,您可以选择要恢复的特定文件类型,例如图像、文档,甚至是FAT和 ext3 等文件系统。选择所需的分区后,您将可以选择文件系统。
我们将解释如何使用PhotoRec恢复 ext4 分区上的不同文件。然而,对于不同的系统,这些步骤应该保持相似。
要运行Photorec,您需要先按照上述步骤安装TestDisk。安装完成后,从Linux(Linux)终端运行以下命令:
须藤摄影

这将以管理权限启动PhotoRec — 您可能需要提供系统 root 密码才能成功运行。您将看到分区、连接的磁盘和设备的列表。
使用向上和向下箭头键导航(Navigate)菜单,选择要从中恢复文件的分区,然后使用向右箭头键选择继续(Proceed)。按 Enter(Press Enter)继续。

选择要恢复的文件类型
在下一个菜单中,选择File Opt并按Enter。

从此菜单中,使用箭头键并按Enter 键(Enter)选择您想要(或不想)搜索的任何文件类型。那些有十字架的人将被恢复。按 b(小写)保存设置,然后按 q 返回上一菜单。

配置完所有内容后,选择搜索(Search)并按Enter。

您需要选择一个位置来保存恢复的文件。选择一个目录,然后按 C(大写)。

PhotoRec将开始扫描您选择的分区并自动为您恢复指定的文件类型。该过程完成后,终端将显示结果,包括恢复了多少文件。
在 Linux 上保护您的文件
通过上述步骤,您可以尽力在Linux上恢复已删除的文件。请务必记住,快速采取行动并卸载设备可以提高成功恢复的机会。
您想更好地管理您的文件吗?您始终可以移动和重命名 Linux 文件,(move and rename your Linux files)而无需担心文件被删除的风险。如果您是Linux 新手(new to Linux),则需要确保您了解 Linux 目录结构(understand the Linux directory structure),以帮助您找到文件并将其正确保存在设备上。
How to Recover Deleted Files on Linux
Accidentally deleting crucial files is a nightmare that many computer users have experienced. Fortunately, there’s hope when using Linux, as several tools and techniques cаn be uѕed to restore fіles.
Before diving into the recovery process, make sure you unmount the affected drive right away to avoid overwriting deleted files with new data. This step increases the likelihood of successful file recovery.
If you’re unsure how to recover deleted files on Linux, follow the steps below.

Understanding Linux File Management
Before you recover deleted files on your Linux PC, it’s a good idea to understand how this is possible. It’s all down to how files are managed on a Linux file system.
Files on Linux use inodes, which contain vital info like file size and location. When you delete a file, the inode is marked as free—the data is still technically there, but you’ll lose access to it. Over time, new files will overwrite the old ones, making file recovery much harder as time goes on.
While there isn’t a built-in undelete command, you can recover files using tools like TestDisk and PhotoRec, depending on your use-case. It’s essential to act fast after realizing your file is missing, as data can be overwritten quickly.
However, recovery isn’t a substitute for prevention. To avoid being in this situation in future, make sure that you back up your files regularly and use clear folder structures to minimize the risk of accidental deletion.
Before Recovering Your Files on Linux
When it comes to recovering deleted data on Linux, there are some important precautions you should take to ensure a successful recovery.
Avoid Overwriting Data
One crucial aspect of recovering lost files is making sure you don’t overwrite the data you’re trying to recover. To do this, you should immediately stop using the affected drive after you realize files are missing.
You can then use a portable Linux distribution on a Live CD or USB drive to boot the system and perform the recovery process. You should also make sure you don’t save recovered files to the same drive—store them on a different drive to prevent further overwriting.
Data Backup
To safeguard your data, it’s a good idea to have a backup plan in place. Having a reliable backup means you won’t need to worry about data recovery processes as much.
It’s a good idea to have a regular backup schedule (whether it’s daily, weekly, or monthly). You should also try to store backups in multiple locations, both physical and online, to protect against data loss due to hardware failure or natural disasters.
If you can’t do that, you should try to use cloud storage solutions like Dropbox or Google Drive as a way to store your most important files away from your physical location.
Using Safe Commands
When working on a Linux PC, you should try to follow some best practices to avoid overwriting or deleting your files accidentally.
With that in mind, you should become familiar with commands like cp (copy), mv (move), and df (display filesystem) on your PC. Understand their proper usage and syntax—if you’re unsure, type man command (eg. man cp) in the command line or terminal to learn more.
Once you’re sure, you should be extremely cautious when handling crucial files. Always double-check your commands before running them. A good way to avoid overwriting your files is to use the –no-clobber flag with the cp and mv commands, which will stop the commands from overwriting any existing files.
If you’re trying to delete files using rm (remove), consider using the -i (interactive) flag, which prompts confirmation before deleting files.
By taking these precautions, you can minimize the chances of accidentally losing or overwriting important files—prevention is much better than recovery, after all.
Recovering Deleted Files on Linux Using TestDisk
A useful tool for recovering deleted files on a Linux PC is TestDisk, an open-source tool designed for data recovery. TestDisk can be used to recover entire lost partitions from your hard drive when your file system structure has been corrupted or if you’ve formatted your drive by mistake.
If you want to recover individual files, you can use TestDisk’s companion application PhotoRec instead. We’ve outlined how to use this in the next section.
On Ubuntu and Debian-based operating systems, you can quickly install TestDisk by opening the terminal and using the command sudo apt install testdisk to begin the installation with the correct permissions.

Finding Lost Partitions
Once you’ve installed TestDisk, the first step is to identify the partition you’re working with. Launch TestDisk by typing sudo testdisk in the terminal.

When prompted, select Create to start a new log file—press Enter to confirm.

Choose your hard drive and select Proceed with your arrow keys, then press Enter.

When prompted, pick the partition type and press Enter. Most users will need to select Intel, but take note of the hint at the bottom—in most cases, the hint will be accurate.

At this point, you’re ready to proceed. You can select Analyse to search your current partitions or press Advanced to access other file system recovery tools.

When you’re ready, select Quick Search to search for lost partitions and press Enter.

Recovering Files with TestDisk
TestDisk will search for lost partitions. If it can’t find them, select Deeper Search and allow the search to complete. Once it finds them, you can then try to recover them (and any missing files).
To do this, use the arrow keys to select a partition, then press p to list the files on it.

If it’s the right partition, press q (lowercase) to return to the previous menu, then press the left or right arrow keys on your keyboard to update the partition from deleted to a standard logical or primary partition. Press Enter to confirm the change. To write the changes to the partition table, select Write, then Enter.

Once the process is complete, you should be able to mount the drive and view the files as normal.
Recovering Deleted Files Using PhotoRec
If you only need to recover a small number of files (rather than an entire drive or partition), you might prefer to use PhotoRec instead. PhotoRec is a companion recovery tool to TestDisk—it requires you to install TestDisk first to use it.
Photorec supports a huge number of file extensions, from ZIP files to media files.
Running PhotoRec
With PhotoRec, you can choose specific file types to recover, such as images, documents, or even file systems like FAT and ext3. Once you’ve selected your desired partition, you’ll be given the option to choose the file system.
We’ll explain how to use PhotoRec to recover different files on an ext4 partition. The steps should remain similar for different systems, however.
To run Photorec, you’ll need to follow the steps above to install TestDisk first. Once it’s installed, run the following command from your Linux terminal:
sudo photorec

This will launch PhotoRec with administrative privileges—you may need to provide your system root password for it to run successfully. You’ll see a list of partitions, connected disks, and devices.
Navigate the menu using up and down arrow keys to select a partition where you want to recover files from, then use the right arrow key to select Proceed. Press Enter to proceed.

Selecting File Types to Recover
At the next menu, select File Opt and press Enter.

From this menu, select any file types that you do (or don’t) want to search for by using the arrow keys and pressing Enter. Those with a cross will be recovered. Press b (lowercase) to save your settings, then press q to return to the previous menu.

When everything’s configured, select Search and press Enter.

You’ll need to choose a location to save the files it recovers. Select a directory, then press C (uppercase).

PhotoRec will begin scanning your selected partition and recovering the specified file types for you automatically. Once the process is complete, the terminal will display the results, including how many files were recovered.
Protecting Your Files on Linux
Thanks to the steps above, you can try your best to recover deleted files on Linux. It’s important to remember that acting fast and unmounting the device can improve your chances of a successful recovery.
Do you want to manage your files better? You can always move and rename your Linux files without risking file deletion. If you’re new to Linux, you’ll need to make sure you understand the Linux directory structure to help you locate your files and save them appropriately on your device.