What is causing "Directory not empty" errors?

15,860

Solution 1

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.

Solution 2

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.

Share:
15,860

Related videos on Youtube

Jeff Severns Guntzel
Author by

Jeff Severns Guntzel

Updated on June 27, 2022

Comments

  • Jeff Severns Guntzel
    Jeff Severns Guntzel almost 2 years

    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/ 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!

  • Jeff Severns Guntzel
    Jeff Severns Guntzel over 12 years
    Added rm -r and it now the script works like a charm. However, ignore-fail-on-non-empty returned Illegal option errors. Any idea why? And thank you very, very much.
  • Suhail Patel
    Suhail Patel over 12 years
    Possibly that flag was deprecated or not used on Unix anymore. Using rm -r is the preferred way anyway for removing a directory which is empty or even non-empty.