diff --git a/src/uu/mv/src/mv.rs b/src/uu/mv/src/mv.rs index 6642a2eb8..1c2390f80 100644 --- a/src/uu/mv/src/mv.rs +++ b/src/uu/mv/src/mv.rs @@ -285,7 +285,23 @@ fn exec(files: &[OsString], b: &Behavior) -> UResult<()> { )); } let target_dir = paths.last().unwrap(); - move_files_into_dir(&paths[..paths.len() - 1], target_dir, b) + let sources = &paths[..paths.len() - 1]; + + // Check if we have mv dir1 dir2 dir2 + // And generate an error if this is the case + if sources.contains(target_dir) { + return Err(USimpleError::new( + 1, + format!( + "cannot move {} to a subdirectory of itself, '{}/{}'", + target_dir.quote(), + target_dir.display(), + target_dir.display() + ), + )); + } + + move_files_into_dir(sources, target_dir, b) } } } diff --git a/tests/by-util/test_mv.rs b/tests/by-util/test_mv.rs index 97e56bf6a..314fd3a7f 100644 --- a/tests/by-util/test_mv.rs +++ b/tests/by-util/test_mv.rs @@ -823,6 +823,24 @@ fn test_mv_interactive_error() { .is_empty()); } +#[test] +fn test_mv_info_self() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + let dir1 = "dir1"; + let dir2 = "dir2"; + at.mkdir(dir1); + at.mkdir(dir2); + + scene + .ucmd() + .arg(dir1) + .arg(dir2) + .arg(dir2) + .fails() + .stderr_contains("mv: cannot move 'dir2' to a subdirectory of itself, 'dir2/dir2'"); +} + // Todo: // $ at.touch a b