开发者

What is causing "Directory not empty" errors?

开发者 https://www.devze.com 2023-04-12 09:09 出处:网络
I\'ve written a script that is mostly working for me. However, the two rmdir commands at the end are sometimes returning Directory not empty errors. How do I beat this? I tried addingr and then -rf wh

I've written a script that is mostly working for me. However, the two rmdir commands at the end are sometimes returning Directory not empty errors. How do I beat this? I tried adding r and then -rf which returned Illegal option errors.

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

if [[ -z "${1}" ]]; then
  die "FolderName Required"
fi

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

echo -n "---Data Folder Setup

---Data Introduction

---Data Audit/Manipulation

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

ditto NewData/ New开发者_JS百科DataCopy
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!


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

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

You could also just use rm -r too:

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号