uutils/coreutils · error · io::Error

mv-error-directory-not-empty

Error message

mv-error-directory-not-empty

What it means

When the destination of a rename exists and is a non-empty directory, mv removes an empty target dir but otherwise cannot proceed, returning ErrorKind::DirectoryNotEmpty with this message. This normalizes behavior between Unix and Windows so mv never silently merges directories. On Unix the kernel would raise ENOTEMPTY; here it is raised explicitly.

Source

Thrown at src/uu/mv/src/mv.rs:864

            // For backup renames, we don't need to track hardlinks as we're just moving the existing file
            rename_with_fallback(to, backup_path, display_manager, false, None, None)?;
        }
    }

    // "to" may no longer exist if it was backed up
    // `symlink_metadata` does not follow symlinks, so a symlink to a directory
    // is not reported as a directory, as with the previous
    // `to.exists() && to.is_dir() && !to.is_symlink()` check
    if to
        .symlink_metadata()
        .is_ok_and(|metadata| metadata.is_dir())
    {
        // normalize behavior between *nix and windows
        if from.is_dir() {
            if is_empty_dir(to) {
                fs::remove_dir(to)?;
            } else {
                return Err(io::Error::new(
                    io::ErrorKind::DirectoryNotEmpty,
                    translate!("mv-error-directory-not-empty"),
                ));
            }
        }
    }

    #[cfg(unix)]
    {
        rename_with_fallback(
            from,
            to,
            display_manager,
            opts.verbose,
            hardlink_tracker,
            hardlink_scanner,
        )?;
    }

View on GitHub (pinned to 85295bbf78)

Solutions

  1. Empty or remove the destination first: `rm -rf dest_dir` if its contents are disposable, then re-run mv
  2. Explicitly merge with `cp -a src/. dest/ && rm -rf src` or rsync --remove-source-files if merging is intended
  3. Move into the directory instead: `mv src_dir/* dest_dir/` when merging is desired
  4. Inspect dest_dir contents before deciding which files to keep

Example fix

// before
mv newcfg/ /etc/myapp/
// after
rsync -a --remove-source-files newcfg/ /etc/myapp/ && rmdir newcfg
# or, if disposable:
rm -rf /etc/myapp && mv newcfg/ /etc/myapp/
Defensive patterns

Strategy: validation

Validate before calling

# before moving dir onto existing dir
if [ -d "$dest" ] && [ -n "$(ls -A "$dest")" ]; then
  echo "dest not empty; merge or remove first"; exit 1
fi

Try / catch

null

Prevention

When it happens

Trigger: `mv src_dir dest_dir` where dest_dir exists, is a directory, and still contains entries — the rename syscall cannot replace a non-empty directory.

Common situations: Retrying a move after a previous partial copy left files in the destination; merging project folders by accident; scripts assuming mv merges directories like `cp -r` semantics.

Related errors


AI-assisted analysis of uutils/coreutils@85295bbf78 (2026-08-31). Data as JSON: /api/errors/479cf3a03571cbaf. Report an issue: GitHub.