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

linux终端删除文件命令

linux终端删除文件命令

Bash terminal on Ubuntu laptop concept
Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati 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”。

rm command with write-protected file

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
rm command in interactive mode

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
output from tree command

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目录保留原样,并且其中的文件保持不变。

rmdir command with a non empty folder

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
rmdir with --ignore-fail-on-non-empty option

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
rmdir command with remove parents option

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终端删除文件命令

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值