bash 我无法使用 rmdir 删除目录,因为目录中似乎有一个无法删除的同名文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36361387/
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 14:26:58  来源:igfitidea点击:

I cannot use rmdir to remove a directory as there seems to be a file in the directory with the same name that cannot be deleted

macosbashterminalcommand

提问by JandD

Hi guys I am a complete beginner to programming and have tried to google this answer but with no joy.

大家好,我是一个完整的编程初学者,并试图用谷歌搜索这个答案,但没有任何乐趣。

I am following Zed Shaw's "Command Line Crash Course " and have hit a wall with rmdir.

我正在关注 Zed Shaw 的“命令行速成课程”并且用 rmdir 碰壁了。

I use a mac and know to delete hidden files like .DS_Storeand have done so for my directory. However, it still says the directory is not empty and ls -laseems to show a file that shares the directory name:

我使用 mac 并且知道删除隐藏文件,例如.DS_Store并且已经为我的目录这样做了。但是,它仍然说目录不为空,并且ls -la似乎显示了一个共享目录名称的文件:

See below for directory joe

目录见下方 joe

drwxr-xr-x  3 MyLaptop staff  102  1 Apr 16:52 .

drwxr-xr-x  4 MyLaptop  staff  136 31 Mar 22:32 ..

drwxr-xr-x  3 MyLaptop  staff  102  1 Apr 16:51 joe

I have tried to remove this file but it will not allow me to and I cannot remove the directory, can anyone's suggest a solution?

我曾尝试删除此文件,但它不允许我删除并且我无法删除该目录,任何人都可以提出解决方案吗?

回答by Bakuriu

That is nota file. The dat the beginning of a line tells you that it is a directory. In fact it is the exact directory you are trying to delete.

不是文件。将d在一行的开头告诉你,它是一个目录。事实上,它正是您要删除的目录。

The rmdircommand requires that the joedirectory be empty. To check if joeis empty use:

rmdir命令要求joe目录为空。要检查是否joe为空,请使用:

$ ls -la joe

Or move into that directory first:

或者先进入该目录:

$ cd joe
$ ls -la

You are instead using ls -lain the parent directory of joe, and hence joeitself is showing up in the contents.

您改为ls -la在 的父目录中使用joe,因此joe它本身显示在内容中。

So check which files are inside joeand delete them using rm. The .and ..entries don't really count as these are references to the current and parent directory.

所以检查里面有哪些文件joe并使用rm. 在...条目真的不指望,因为这些是当前和父目录的引用。



Note that rmcan also delete directories, so you could simply do rm -r joeto delete all the files inside joeandthe joedirectory itself.

请注意,rm也可以删除目录,所以你可以简单地做rm -r joe删除里面的所有文件joejoe目录本身。

回答by user6124839

You can also use

你也可以使用

$ rm -r joe

$ rm -r 乔

The -r option to the rm command will recursively delete the contents of the directory, and the directory itself.

rm 命令的 -r 选项将递归删除目录的内容和目录本身。

From the rm man page:

从 rm 手册页:

     -r, -R, --recursive
     remove directories and their contents recursively
     -r, -R, --recursive
     remove directories and their contents recursively