如果您在Linux PC 上具有 root 访问权限,则您有权删除您喜欢的任何文件或目录。然而,这可能很危险,因为rm之类的工具允许用户不小心删除重要的系统文件。这就是为什么,如果你试图在 Linux 中删除文件或目录,你需要采取一些预防措施。
有几种方法可以删除 Linux 上的目录或文件,(Linux)使用Linux发行版的文件管理器或打开的终端(只要您有正确的权限(right permissions))。如果您想知道如何在Linux中删除文件或目录,以下是您需要做的。
在 Linux 中使用文件管理器删除文件或目录(Using a File Manager to Remove Files or Directories in Linux)
如果您是Linux 初学者(Linux beginner),您可能会对使用终端删除Linux中的文件或文件夹持谨慎态度。毕竟,一旦设置了rm off 之类的命令开始删除文件,就没有回头路了。您可以使用与Linux发行版一起打包的文件管理器来删除文件或文件夹,而不是冒这个风险。
虽然Linux文件管理器的设计确实不同,但它们应该以大致相同的方式工作。本指南探讨如何使用 Ubuntu 的文件管理器执行此操作,但其他发行版中包含的文件管理器的步骤可能与此类似。
- 首先,打开Linux发行版上的文件管理器。这应该不难找到,用户图标可能是文件夹的形状。在Ubuntu上,这个应用程序被命名为Files。
- 在发行版的文件管理器中,导航到包含您要删除的文件或子文件夹的目录。首先(First),选择您要删除的文件或文件夹。选择它们后,右键单击它们并选择Move to Trash、Move to Bin或Delete,具体取决于您的发行版和区域设置。您也可以选择键盘上的Delete键来达到相同的效果。
- 大多数发行版都有一个垃圾系统,允许您在文件被删除之前“存储”文件,让您有机会恢复它们。这通常可以在桌面上找到,作为文件管理器中的条目,或者作为可以在软件菜单中启动的应用程序。要完成删除过程,请输入垃圾文件夹,然后选择清空(Empty )或擦除(Erase)选项。这些选项可能会有所不同,具体取决于您的发行版。
- 或者,选择您的项目(或项目),然后右键单击并选择从垃圾箱(Delete from Trash)中删除以完全删除它们。
如何在 Linux 中使用 rm 命令删除文件或目录(How to Remove Files or Directories in Linux Using the rm Command)
使用发行版的文件管理器应用程序删除文件或文件夹可以正常工作,但有一些限制。例如,如果您不是文件的所有者,则需要使用终端以 root 访问权限打开文件管理器以将其删除。您也不能在无头(仅限终端)Linux安装上使用这样的应用程序。
这就是rm命令的用武之地。这个Unix命令可以追溯到 1971 年,并且仍然是在Linux上删除文件或目录的最快方法。但是请注意 - rm命令具有强大的功能,特别是如果您使用 sudo 或直接以 root 用户身份运行它,因此您需要小心使用它。
- 要使用rm(rm)删除单个文件,请打开一个新的终端窗口(或远程SSH连接)并输入rm file,将file替换为正确的文件名。如果您不在同一个目录中,则需要先使用cd移动到该目录,或者使用完整的文件路径(例如rm /path/to/file)。
- 要删除多个文件,请键入rm file1 file2,将file1和file2替换为正确的文件名和文件路径。您可以向此命令添加其他文件以删除两个以上。
- 如果要在Linux上删除空目录,请键入rm -d directory,将目录(directory )替换为正确的目录名称和路径。如果目录不为空并且有子文件夹或文件,请键入rm -r directory,将目录(directory )替换为正确的名称和路径。
- 您还可以通过键入rm -r directory1、directory2(rm -r directory1, directory2)等来一次删除多个目录。
- 如果要删除与部分文件或目录名称匹配的所有文件或目录,可以使用通配符,例如星号 ( * )。为此,请键入rm fil* 或rm -r dir*,将占位符fil或dir替换为您自己的文件名和目录路径。
- 如果文件或目录受到保护,您可能需要强制删除它。为此,请键入rm -rf path,将占位符路径(path )替换为正确的目录或文件路径。此命令具有极大的风险(carries extreme risk),因此请确保您使用的路径正确,然后再继续。
- 如果您担心使用rm -rf命令,可以强制rm在删除每个文件或目录之前要求确认。为此,请使用rm -i path,将path替换为您自己的文件名或目录路径。对于目录,请改用rm -ir 路径(rm -ir path )。对于每个条目,选择键盘上的Y键,然后选择Enter进行确认。
如果您想了解有关可能的rm选项和参数的更多信息,请在终端中键入man shred以查看完整的手册。
使用 shred 命令完全擦除文件(Completely Erasing Files Using the shred Command)
虽然 rm 和 unlink 之类的命令非常适合在Linux中删除文件,但它们确实会留下痕迹。您(或其他人)可以使用这些跟踪从您之前删除的硬盘驱动器或便携式存储设备中恢复文件。(recover files)这可能并不理想,尤其是当您不希望检索文件时。
要解决此问题,您可以先使用shred命令覆盖文件。这可以确保文件中的所有数据在被删除之前都被重复覆盖,从而确保不留下文件痕迹并防止其被检索。例如,您可能希望使用此命令擦除敏感文档。
- 为此,请打开终端窗口或使用SSH远程建立连接,然后键入shred file,将file替换为您自己的文件名和路径。如果您不想提供文件的路径,请使用cd转到包含您要首先删除的文件或子目录的目录。
- shred命令具有其他参数,您可以传递这些参数以确保安全删除文件。例如,shred -f将强制删除写保护的文件,而shred -u将确保文件在粉碎后被完全删除,确保它不再出现在文件管理器中或使用ls命令。
- 如果要覆盖文件超过标准 3 次,请使用shred -n 0,将0替换为替代数值。如果要隐藏 shred 效果,请使用shred -z强制它使用最终运行,用零覆盖文件。
- shred命令通常不会向终端输出任何结果。如果您想在运行时查看有关它在做什么的更多详细信息,请改用shred -v。
与rm一样,您可以通过在终端中键入man shred来了解更多信息,然后再使用该命令。
在 Linux 系统上保护您的文件(Protecting Your Files on Linux Systems)
当您在Linux中删除目录或文件时,您正在为真正重要的文件腾出硬盘空间,无论是重要文件还是不可替代的照片。无论您使用的是Linux Mint还是任何其他主要发行(other major distribution)版都没有关系,因为上述步骤几乎适用于您选择使用的任何Linux发行版。(Linux)
如果您担心数据丢失,可以采取一些步骤来保护您的文件。虽然 Linux 系统通常更安全,但Linux 防病毒软件(Linux antivirus)可以帮助您保护文件免受可能在本地网络中传播的恶意软件的侵害。您可能还需要考虑使用本地或云存储自动备份文件。(automating a file backup)
How to Delete a File or Directory in Linux
If you have root access on your Linux PC, yоυ have the power to delete any file or directory you like. This can be dangerous, however, with tools like rm allowing users to carelessly erase crucial system files by accident. That’s why, if you’re trying to delete files or directories in Linux, you need to take some precautions.
There are a few ways you can remove directories or files on Linux, using either your Linux distribution’s file manager or an open terminal (as long as you have the right permissions). If you want to know how to delete a file or directory in Linux, here’s what you’ll need to do.
Using a File Manager to Remove Files or Directories in Linux
If you’re a Linux beginner, you might be wary about using the terminal to delete files or folders in Linux. After all, there’s no turning back once you set a command like rm off to start deleting files. Rather than risk this, you can use the file manager packaged with your Linux distribution to delete files or folders instead.
While Linux file managers do vary in design, they should work in much the same way. This guide explores how to do this using Ubuntu’s file manager, but the steps are likely to be similar for the file manager included in other distributions.
- To start, open the file manager on your Linux distro. This shouldn’t be too hard to find, with the user icon likely in the shape of a document folder. On Ubuntu, this app is named Files.
- In your distro’s file manager, navigate to the directory containing the files or subfolders you wish to delete. First, select the files or folders you wish to delete. Once they’re selected, right-click on them and select Move to Trash, Move to Bin, or Delete, depending on your distribution and locale. You can also select the Delete key on your keyboard to achieve the same effect.
- Most distributions operate a trash system that allows you to “store” files before they’re deleted, giving you the chance to restore them. This is typically found on the desktop, as an entry in your file manager, or as an app you can launch in your software menu. To complete the deletion process, enter the trash folder, then select the Empty or Erase option. These options may vary, depending on your distribution.
- Alternatively, select your item (or items), then right-click and select Delete from Trash to completely remove them instead.
How to Remove Files or Directories in Linux Using the rm Command
Removing files or folders using your distribution’s file manager app works fine, but there are some limitations. For instance, if you’re not the owner of the file, you’ll need to open your file manager with root access using the terminal to delete it. You also can’t use an app like this on a headless (terminal-only) Linux installation.
That’s where the rm command comes in. This Unix command dates back to 1971 and remains the fastest way to delete files or directories on Linux. Be warned, however—the rm command has great power, especially if you’re running it with sudo or as the root user directly, so you’ll need to take care using it.
- To remove a single file using rm, open a new terminal window (or remote SSH connection) and type rm file, replacing file with the correct file name. If you’re not in the same directory, you’ll need to use cd to move to it first, or use the full file path (eg. rm /path/to/file) instead.
- To remove multiple files, type rm file1 file2, replacing file1 and file2 with the correct file name and file path. You can add additional files to this command to remove more than two.
- If you want to remove an empty directory on Linux, type rm -d directory, replacing directory with the correct directory name and path. If the directory isn’t empty and has sub-folders or files, type rm -r directory, replacing directory with the correct name and path.
- You can also remove multiple directories at once by typing rm -r directory1, directory2, etc.
- If you want to remove all files or directories that match a partial file or directory name, you can use wildcards, such as an asterisk (*). To do this, type rm fil* or rm -r dir*, replacing the placeholders fil or dir with your own file names and directory paths.
- If the file or directory is protected, you may need to force its deletion. To do this, type rm -rf path, replacing the placeholder path with the correct directory or file path. This command carries extreme risk, so make sure the path you’re using is correct before you proceed.
- If you’re worried about using the rm -rf command, you can force rm to ask for confirmation before each file or directory is erased. To do this, use rm -i path, replacing path with your own file name or directory path. For directories, use rm -ir path instead. For each entry, select the Y key on your keyboard, then select Enter to confirm.
If you want to learn more about the possible rm options and arguments, type man shred in the terminal to view the full manual.
Completely Erasing Files Using the shred Command
While commands like rm and unlink are great for deleting files in Linux, they do leave traces. You (or somebody else) could use these traces to recover files from a hard drive or portable storage that you’ve previously deleted. This might not be ideal, especially if you don’t want the files to be retrieved.
To get around this problem, you can overwrite files first using the shred command. This makes sure that all data within the file is overwritten repeatedly before it’s deleted, ensuring no traces of the file are left and preventing it from being retrieved. You may want to use this command to erase sensitive documents, for instance.
- To do this, open a terminal window or make a connection remotely using SSH and type shred file, replacing file with your own file name and path. If you don’t want to provide the path to the file, use cd to travel to the directory containing the file or sub-directory you wish to remove first.
- The shred command has additional parameters that you can pass to ensure your file is safely deleted. For instance, shred -f will force the deletion of write-protected files, while shred -u will ensure the file is completely removed once shredded, ensuring it no longer appears in your file manager or using the ls command.
- If you want to overwrite the file more than the standard 3 times, use shred -n 0, replacing 0 with an alternative numerical value. If you want to hide the shred effect, use shred -z to force it to use a final run that overwrites the file with zeros.
- The shred command doesn’t typically output any results to the terminal. If you want to see more detail about what it’s doing as it runs, use shred -v instead.
As with rm, you can learn more by typing man shred in the terminal before using the command.
Protecting Your Files on Linux Systems
When you remove a directory or file in Linux, you’re freeing up space on your hard drive for the files that truly matter, whether it’s important documents or irreplaceable photos. It doesn’t matter whether you’re using Linux Mint or any other major distribution, as the steps above should work for almost any Linux distro you choose to use.
If you’re worried about data loss, there are some steps you can take to protect your files. While Linux systems are typically more secure, a Linux antivirus can help you to keep your files safe from malware that could spread across your local network. You may want to consider automating a file backup using local or cloud storage, too.