bash 对目录使用 rm -rf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38932431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:02:49  来源:igfitidea点击:

Using rm -rf with a directory

bashdirectoryrm

提问by adbdkb

I tried to search on SO, but not able to find the difference between the following commands. if I have a directory named dir, how the below commands differ?

我试图搜索 SO,但无法找到以下命令之间的区别。如果我有一个名为 dir 的目录,以下命令有何不同?

  • rm -rf dir/*
  • rm -rf dir/
  • rm -rf dir
  • rm -rf dir/*
  • rm -rf dir/
  • rm -rf dir

Also how do the user permissions on the directory affect the outcome, if the id running the command is not the owner or not even in the group of the owner?

如果运行命令的 id 不是所有者或什至不在所有者组中,那么用户对目录的权限如何影响结果?

I am adding the command to do rm -rf in a shell script I am working on and need help in understanding the difference between the above commands.

我正在我正在处理的 shell 脚本中添加执行 rm -rf 的命令,需要帮助来理解上述命令之间的区别。

回答by AtomHeartFather

  • rm -rf dir/*

    Removes files within the directory (without removing the directory itself). Note, hidden fileswon't be removed.

  • rm -rf dir/

    Trailing slash indicates that diris a directory. If it was a file, it wouldn't get removed. In your case this is identical to rm -rf dir, but in general it differs (see below)

  • rm -rf dir

    In your case, identical to the one above.

  • rm -rf dir/*

    删除目录中的文件(不删除目录本身)。 注意隐藏文件不会被删除。

  • rm -rf dir/

    尾部斜杠表示这dir是一个目录。如果它是一个文件,它不会被删除。在您的情况下,这与 相同rm -rf dir,但通常不同(见下文)

  • rm -rf dir

    在您的情况下,与上述相同。

In general, tools such as rmusually follow IEEE/OpenGroup standardswhen it comes to pathname resolution, which means that dir/is equivalent to dir/.. One implication of that is that if diris a symlink to a directory rm -rf dir/will remove the content of the directory (including the hidden files) but not the link or the directory itself, whereas rm -rf dirwill only remove the symlink.

通常,在路径名解析方面,诸如此类的工具rm通常遵循IEEE/OpenGroup 标准,这意味着dir/等效于dir/.. 其中一个含义是 ifdir是指向目录的符号链接rm -rf dir/将删除目录的内容(包括隐藏文件),但不会rm -rf dir删除链接或目录本身,而只会删除符号链接。

You need to have writepermissions on a file or directory that you are removing, plus execpermissions on a directory that rmneeds to traverse to remove files. You can read more about Unix filesystem permissions here.

您需要对要write删除的文件或目录具有权限,以及execrm需要遍历以删除文件的目录的权限。您可以在此处阅读有关 Unix 文件系统权限的更多信息。