Overlooked Linux Mv Option Replaces Three Commands with One itsfoss.com · 15 poi...
HN Best 200 @best
Overlooked Linux Mv Option Replaces Three Commands with One itsfoss.com · 15 points · 1 comments · HN thread
TL;DR It's FOSS explains GNU coreutils' mv --exchange option (available since coreutils 9.5), which swaps two files or directories in a single atomic operation instead of the usual three-move temporary-name dance. The article covers checking support, swapping files and directories, the -T fix for a directory mistake, and filesystem limitations.
- Classic swap needs three mv commands and a temporary name, leaving a gap where one file is missing and a risk of failing halfway.
- mv --exchange swaps two paths in one operation using the kernel's renameat2() with RENAME_EXCHANGE, so both names always exist.
- Requires GNU coreutils 9.5 or newer; check with mv --version, and mv --help | grep exchange shows whether the option is present.
- When the second argument is an existing directory, mv moves into it, causing an 'Unknown error -1'; adding -T (--no-target-directory) makes directory swaps work.
- Both paths must be on the same filesystem; exchanging with a tmpfs path like /dev/shm fails, and the filesystem must support the RENAME_EXCHANGE flag (ext4, Btrfs, XFS and tmpfs do).
- Useful for rolling out configs with an easy rollback, switching release directories, and undoing a swap by running the command again.
Discussion The comment thread is very small, with a single comment that takes issue with the article's framing of the directory fix. The tone is critical but focused on design rather than hostility.
- -T is a workaround; the behaviour should be a bug (a few): A commenter argues that when --exchange is given, mv should expect two paths to swap and not move into a directory named by the second argument. They say the current behaviour violates least surprise and should be treated as a bug rather than fixed with -T.