linux终端删除文件命令

Fatmawati Achmad Zaenuri/Shutterstock.comFatmawati Achmad Zaenuri / Shutterstock.com

The rm and  rmdir commands delete files and directories on Linux, macOS, and other Unix-like operating systems. They’re similar to the del and  deltree commands in Windows and DOS. These commands are very powerful and have quite a few options.

rmrmdir命令删除Linux,macOS和其他类似Unix的操作系统上的文件和目录。 它们类似于Windows和DOS中的deldeltree命令。 这些命令功能非常强大,并且有很多选择。

It is important to note that files and directories deleted using rm and rmdir do not get moved to the Trash. They are immediately removed from your computer. If you accidentally delete files using these commands, the only way you’ll be able to restore them is from a backup.

重要的是要注意,使用rmrmdir删除的文件和目录不会移至废纸.。 它们会立即从您的计算机中删除。 如果您不小心使用这些命令删除了文件,则只能通过备份来还原它们。

如何使用rm删除文件 (How to Remove Files with rm)

The simplest case is deleting a single file in the current directory. Type the rm command, a space, and then the name of the file you want to delete.

最简单的情况是删除当前目录中的单个文件。 键入rm命令,一个空格,然后输入要删除的文件的名称。

rm file_1.txt

If the file is not in the current working directory, provide a path to the file’s location.

如果该文件不在当前工作目录中,请提供该文件位置的路径。

rm ./path/to/the/file/file_1.txt

You can pass more than one filename to rm. Doing so deletes all of the specified files.

您可以将多个文件名传递给rm 。 这样做会删除所有指定的文件。

rm file_2.txt file_3.txt

Wildcards can be used to select groups of files to be deleted. The * represents multiple characters and the ? represents a single character. This command would delete all of the png image files in the current working directory.

通配符可用于选择要删除的文件组。 *表示多个字符,而? 代表一个字符。 此命令将删除当前工作目录中的所有png图像文件。

rm *.png

This command would delete all files that have a single character extension. For example, this would delete File.1 and File.2, but not File.12.

此命令将删除所有具有单个字符扩展名的文件。 例如,这将删除File.1和File.2,但不会删除File.12。

rm *.?

If a file is write-protected you will be prompted before the file is deleted. You must respond with y or n and press “Enter.”

如果文件被写保护,则在删除文件之前将提示您。 您必须输入yn并按“ Enter”。

To reduce the risk of using rm with wildcards use the -i (interactive) option. This requires you to confirm the deletion of each file.

要降低将rm与通配符一起使用的风险,请使用-i (交互式)选项。 这要求您确认每个文件的删除。

rm -i *.dat

The -f (force) option is the opposite of interactive. It does not prompt for confirmation even if files are write-protected.

-f (强制)选项与Interactive相反。 即使文件被写保护,它也不会提示您进行确认。

rm -f filename

如何使用rm删除目录 (How to Remove Directories with rm)

To remove an empty directory, use the -d (directory) option. You can use wildcards (* and ?) in directory names just as you can with filenames.

要删除空目录,请使用-d (目录)选项。 您可以在目录名称中使用通配符( *? ),就像使用文件名一样。

rm -d directory

Providing more than one directory name deletes all of the specified empty directories.

提供多个目录名将删除所有指定的空目录。

rm -d directory1 directory2 /path/to/directory3

To delete directories that are not empty, use the -r (recursive) option. To be clear, this removes the directories and all files and sub-directories contained within them.

要删除不为空的目录,请使用-r (递归)选项。 需要明确的是,这将删除目录以及其中包含的所有文件和子目录。

rm -r directory1 directory2 directory3

If a directory or a file is write-protected, you will be prompted to confirm the deletion. To delete directories that are not empty and to suppress these prompts, use the -r (recursive) and -f (force) options together.

如果目录或文件受写保护,则将提示您确认删除。 要删除不为空的目录并取消显示这些提示,请同时使用-r (递归)和-f (强制)选项。

rm -rf directory

Care is required here. Making a mistake with the rm -rf command could cause data loss or system malfunction. It’s dangerous, and caution is the best policy. To gain an understanding of the directory structure and the files that will be deleted by the rm -rf command, use the tree command.

这里需要注意。 rm -rf命令错误可能导致数据丢失或系统故障。 这很危险 ,谨慎是最好的政策。 要了解rm -rf命令将删除的目录结构和文件,请使用tree命令。

Use apt-get to install this package onto your system if you’re using Ubuntu or another Debian-based distribution. On other Linux distributions, use your Linux distribution’s package management tool instead.

如果您使用的是Ubuntu或其他基于Debian的发行版,请使用apt-get将此软件包安装到系统上。 在其他Linux发行版上,请改用Linux发行版的程序包管理工具。

sudo apt-get install tree

Running the tree command produces a simple to understand diagram of the directory structure and files beneath the directory from which it is run.

运行tree命令将生成一个简单易懂的目录结构图,并从中运行该目录下的文件。

tree

You can also supply a path to the tree command to cause it to start the tree from another directory in the file system.

您还可以提供tree命令的路径,以使其从文件系统中的另一个目录启动树。

tree path/to/directory

The rm command also has --one-file-system, --no-preserve-root, --preserve-root options, but those are only recommended for advanced users. If you get something wrong, you could accidentally delete all your system files. Consult the command’s manual page for more information.

rm命令还具有--one-file-system, --no-preserve-root, --preserve-root选项,但仅建议高级用户使用。 如果出现问题,可能会意外删除所有系统文件。 有关更多信息,请查阅命令的手册页 。

如何使用rmdir删除目录 (How to Remove Directories with rmdir)

There is another command, called rmdir, that you can use to delete directories. The difference between rm and rmdir is that rmdir can only delete directories that are empty. It will never delete files.

还有另一个命令rmdir , 可用于删除目录。 之间的区别 rmrmdir 就是 rmdir 只能删除目录,都是空的。 它永远不会删除文件。

The simplest case is deleting a single empty directory. As with rm, you can pass multiple directory names to rmdir , or a path to a directory.

最简单的情况是删除一个空目录。 与rm ,您可以将多个目录名传递给rmdir ,也可以传递目录的路径。

Delete a single directory in the current directory by passing its name to rmdir :

将名称传递给rmdir ,以删除当前目录中的单个目录:

rmdir directory

Delete multiple directories by passing a list of names to  rmdir :

通过将名称列表传递给rmdir来删除多个目录:

rmdir directory1 directory2 directory3

Delete a directory not in the current directory by specifying the full path to that directory:

通过指定该目录的完整路径来删除不在当前目录中的目录:

rmdir /path/to/directory

If you try to delete a folder that is not empty, rmdir will give you an error message. In the following example rmdir successfully, and silently, deletes the clients directory but it refuses to delete the projects directory because it contains files. The projects directory is left exactly as it was and the files in it are untouched.

如果尝试删除不为空的文件夹, rmdir会给您一条错误消息。 在以下示例中, rmdir成功且无提示地删除了clients目录,但是由于它包含文件,它拒绝删除projects目录。 projects目录保留原样,并且其中的文件保持不变。

When rmdir gives a “Directory not empty” error, it stops processing the directories that were passed to it on the command line. If you’ve asked it to delete four directories and the first one had files in it, rmdir would give you the error message and do nothing more. You can force it to ignore these errors with the --ignore-fail-on-non-empty option so that other directories are processed.

rmdir给出“目录不为空”错误时,它将停止处理在命令行rmdir它的目录。 如果您要求它删除四个目录,并且第一个目录中包含文件,则rmdir会给您错误消息,并且什么也不做。 您可以使用--ignore-fail-on-non-empty选项强制其忽略这些错误,以便处理其他目录。

In the following example two folders have been passed to rmdir, these are work/reports and work/quotes . The --ignore-fail-on-non-empty option has been included in the command. The work/reports folder has files in it, so rmdir cannot delete it. The --ignore-fail-on-non-empty option forces rmdir to ignore the error and move on to the next folder it needs to process, which is work/quotes. This is an empty folder, and rmdir deletes it.

在以下示例中,两个文件夹已传递到rmdir ,它们是work/reportswork/quotes--ignore-fail-on-non-empty选项已包含在命令中。 work/reports文件夹中包含文件,因此rmdir无法删除它。 --ignore-fail-on-non-empty选项强制rmdir忽略该错误,并移至需要处理的下一个文件夹,即work/quotes 。 这是一个空文件夹, rmdir删除它。

This was the command used.

这是使用的命令。

rmdir --ignore-fail-on-non-empty work/reports /work/quotes

You can use the  -p (parents) option to delete a directory and to delete its parent directories too. This trick works because rmdir starts with the target directory and then back-steps to the parent. That directory should now be empty, so it can be deleted by rmdir, and the process repeats stepping back up the path that was provided to rmdir.

您可以使用-p (父级)选项删除目录并删除其父目录。 之所以rmdir ,是因为rmdir从目标目录开始,然后退回到父目录。 该目录现在应该为空,因此可以由rmdir删除,并且该过程重复一步,逐步备份提供给rmdir的路径。

In the following example the command that is passed to rmdir is:

在以下示例中,传递给rmdir的命令是:

rmdir -p work/invoices

Both the invoices and the work directories are deleted, as requested.

根据要求删除invoiceswork目录。



Whether you’re using Bash or any other shell, Linux provides flexible and powerful commands for you to delete directories and files straight from the terminal command line. Some people prefer to have a workflow that revolves around the terminal. Others may have no choice in the matter. They may be working on servers without a GUI installed or on a remote session onto a headless system such as a Raspberry Pi. These commands are perfect for that group of people.

无论您使用的是Bash还是任何其他Shell,Linux都可以为您提供灵活而强大的命令,使您可以直接从终端命令行删除目录和文件。 有些人喜欢拥有围绕终端的工作流程。 其他人可能别无选择。 他们可能在未安装GUI的服务器上工作,或者在Raspberry Pi等无头系统上的远程会话上工作。 这些命令非常适合该人群。

But whatever type of workflow you prefer, these commands lend themselves very well to being included in shell scripts. If a script is triggered by a cron job, it can help automate routine housekeeping tasks such as purging unwanted log files. If you investigate that use case, remember the power of these commands, test everything carefully, and always maintain a recent backup.

但是,无论您喜欢哪种工作流程,这些命令都非常适合被包含在Shell脚本中。 如果脚本是由cron作业触发的,则可以帮助自动执行日常整理任务,例如清除不需要的日志文件。 如果您调查该用例,请记住这些命令的功能,仔细测试所有内容,并始终维护最新备份。

翻译自: https://www.howtogeek.com/409115/how-to-delete-files-and-directories-in-the-linux-terminal/

linux终端删除文件命令

linux终端删除文件命令_如何在Linux终端中删除文件和目录相关推荐

  1. 在linux里复制文件命令_如何在Linux上使用“安装”命令复制文件

    在linux里复制文件命令 Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati Achmad Zaenuri / Shutterstock.com ...

  2. linux 开机自动运行命令_如何在Linux终端同时运行多个Linux命令

    在一行中运行两个或多个命令可以节省大量时间,并在Linux中提高效率.在Linux中,有三种方法可以在一行中运行多个命令: ; Command 1 ; Command 2 首先运行Command1,然 ...

  3. idea新建java文件类型_如何在IntelliJ IDEA中设置文件类型

    IntelliJ IDEA是Java语言开发的集成环境,IntelliJ在业界被公认为优秀的Java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.Ant.JUnit.CVS整合. ...

  4. 禁用删除键退回历史记录_如何在Windows 8中删除或禁用搜索超级按钮历史记录

    禁用删除键退回历史记录 When you use the Search Charm in Windows 8 it remembers everything you search for, which ...

  5. jsp在mysql中删除数据_如何在jsp页面中删除数据库中的数据

    如何在jsp页面中删除数据库中的数据 各位大虾! 我想在jsp页面上写一个按钮功能,从而动态的删除数据库中的一行数据. 不知道怎么实现 这是我写的一段代码 数据库是MySql UserBean p3= ...

  6. win12服务器文件设置只读,如何在Win10系统中更改文件夹的只读或系统属性

    正常情况下通过右键属性只能更改文件的只读属性,系统win10属性则连相应选项都没有.那么如何在Win10系统中更改文件夹的只读或系统属性呢?下面跟着学习啦小编来一起了解下吧. 在Win10系统中更改文 ...

  7. python从后面删除重复项_如何从Python列表中删除重复项

    如何从Python列表中删除重复项 了解如何从Python中的List中删除重复项技巧. 实例 从列表中删除任何重复项: mylist = ["a", "b", ...

  8. linux查找文件夹命令_如何在Linux中使用命令行查找文件和文件夹

    linux查找文件夹命令 Most people use a graphical file manager to find files in Linux, such as Nautilus in Gn ...

  9. linux删除用户所有信息_如何在Linux上删除用户(以及删除所有跟踪)

    linux删除用户所有信息 Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock Deleting ...

最新文章

  1. haystack全文检索框架
  2. Cachegrind--缓存命中检查工具及其可视化
  3. Access-Control-Allow-Origin与跨域
  4. iOS开发CoreAnimation解读之四——Layer层动画内容
  5. IBM X3650 M4服务器安装centos找不到硬盘的解决方法
  6. Bugku密码学(一)
  7. 助力全站WebP ,阿里云云上FPGA 团队发布 WebP图片解决方案
  8. 15个友好的jQuery 提示框插件
  9. 《大话设计模式》笔记(1)——创建型模式
  10. 卷积神经网络如何进行图像识别的
  11. TLV3501超高速电压比较器模块
  12. 《个人信息保护法》正式施行,拒绝隐私泄露
  13. Mac和iphone利用自带邮件客户端添加263企业邮箱
  14. do while 循环 语法结构
  15. CSDN如何删除上传的资源
  16. VSCode软件介绍
  17. untiy的http请求
  18. 计算机专业的学生怎样练习编程才能把编程学精通啊?
  19. 条形码识别(3)——译码
  20. win10安装Mysql8的两种方式(安装/卸载-图文教程)

热门文章

  1. 男大学生眼中的完美女大学生(转载)
  2. AOPlog4j2propagation的7种事务配置
  3. Cannot open url. please check this url is correct
  4. MFC Afx*.h 详解
  5. 专业计算机和游戏计算机配置要求,游戏直播电脑配置要求2019|游戏直播电脑配置单推荐(可装win7)...
  6. 如何识一个人的技术能力和水平?
  7. 那温度 已 无法保留 爱已经 冷透 冷透 我的心 愿 和你共有 一起到 尽头 尽头
  8. 微信小程序入门---01
  9. 从0开始学AI-DAY0-写在前面
  10. ubuntu9.10 添加bones7456源