bash 是什么导致“目录非空”错误?

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

What is causing "Directory not empty" errors?

bashshellcommand-line

提问by Jeff Severns Guntzel

I've written a script that is mostlyworking for me. However, the two rmdircommands at the end are sometimes returning Directory not emptyerrors. How do I beat this? I tried adding rand then -rfwhich returned Illegal optionerrors.

我写了一个主要对我有用的脚本。但是,最后的两个rmdir命令有时会返回Directory not empty错误。我怎么打败这个?我尝试添加 r然后-rf返回Illegal option错误。

I'm a rookie, and I'd be grateful for any help. Here is the full shell script, which, again, is mostlyworking beautifully:

我是菜鸟,如果有任何帮助,我将不胜感激。这是完整的 shell 脚本,同样,它大部分工作得很好:

if [[ -z "" ]]; then
  die "FolderName Required"
fi

newDirName="DirectoryName"
newBaseDir="/Users/JSG/Desktop/DataFarm//"
/bin/mkdir -p $newBaseDir/{ProtectedOrig,Data}

echo -n "---Data Folder Setup

---Data Introduction

---Data Audit/Manipulation

---Data Queries" > $newBaseDir/Data/_DataJournal.txt

ditto NewData/ NewDataCopy
fab deploy_data_to_s3:data=*
mv NewData/ $newBaseDir/ProtectedOrig/NewData
mv NewDataCopy/ $newBaseDir/Data/NewDataCopy 
mv $newBaseDir/Data/NewDataCopy/* $newBaseDir/Data/
rmdir $newBaseDir/Data/NewDataCopy
mv $newBaseDir/ProtectedOrig/NewData/* $newBaseDir/ProtectedOrig/
rmdir $newBaseDir/ProtectedOrig/NewData
chflags -R uchg $newBaseDir/ProtectedOrig/
mkdir NewData

What am I missing? And thanks in advance!

我错过了什么?并提前致谢!

回答by Suhail Patel

For the rmdircommand, you need to add the --ignore-fail-on-non-emptyflag so it deletes the directory even if files are in there like so:

对于rmdir命令,您需要添加--ignore-fail-on-non-empty标志,以便它删除目录,即使文件在那里,如下所示:

rmdir --ignore-fail-on-non-empty $newBaseDir/Data/NewDataCopy

You could also just use rm -rtoo:

您也可以使用rm -r

rm -r $newBaseDir/Data/NewDataCopy

From the Wikipedia Entry:

来自维基百科条目

rmdir will not remove a directory if it is not empty in UNIX. The correct way to remove a directory and all its contents recursively is with the rm command.

如果目录在 UNIX 中不为空,则 rmdir 不会删除该目录。递归删除目录及其所有内容的正确方法是使用 rm 命令。

回答by Chris J

Check for any files in the directory that start with .. I note you're moving *, but if there's a file called, for example, .hello, then *will not match this file and as a result the directory will not be empty when you come to do an rmdir.

检查目录中是否有任何以.. 我注意到你正在移动*,但如果有一个名为,例如.hello,那么*将不匹配这个文件,因此当你来到做一个命令rmdir目录不会是空的。