# file RENAME not behaving as expected

**URL:** <https://discourse.cmake.org/t/file-rename-not-behaving-as-expected/15443>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [January 5, 2026, 12:29pm UTC](https://discourse.cmake.org/t/file-rename-not-behaving-as-expected/15443 "2026-01-05T12:29:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7schroet](https://discourse.cmake.org/user_avatar/discourse.cmake.org/7schroet/32/5966_2.png) [@7schroet](https://discourse.cmake.org/u/7schroet)\
**Post date:** [January 5, 2026, 12:29pm UTC](https://discourse.cmake.org/t/file-rename-not-behaving-as-expected/15443/1 "2026-01-05T12:29:39Z")

</div>

Hi,

the `RENAME` functionality of the `file` command is not behaving as expected. I have a small reproducer right here:

```cmake
set(old_name "file.txt") 
set(new_name "new_file.txt") 

message("Pre rename:") 
execute_process(COMMAND ls) 

file(RENAME "${old_name}" "${new_name}" NO_REPLACE) 

message("\nPost rename:") 
execute_process(COMMAND ls) 

```

Running this via `cmake -P <script name>` produces the following output:

```auto
Pre rename: 
file.txt 
script.cmake 

Post rename: 
file.txt 
new_file.txt 
script.cmake 

```

According to the [docs](https://cmake.org/cmake/help/latest/command/file.html#rename), the file should be moved and not copied. This problem only occurs when I pass `NO_REPLACE` in the arguments. If I remove that extra argument, the command behaves as expected. Am I misunderstanding the `NO_REPLACE` option or is this a bug?

The background for even noticing this is my usage of ExternalData. Post download, the files are “moved” with `file(RENAME ... NO_REPLACE)`. But this also just leads to a copy, so I end up with duplicates of very large files on my filesystem.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [January 5, 2026, 7:42pm UTC](https://discourse.cmake.org/t/file-rename-not-behaving-as-expected/15443/2 "2026-01-05T19:42:21Z")

</div>

That’s unexpected. Please [open an issue](https://gitlab.kitware.com/cmake/cmake/-/issues/new?type=ISSUE&initialCreationContext=list-route) in the issue tracker. Be sure to mention the platform(s) you’re seeing this on, and what CMake versions exhibit the behavior.

For reference, the `NO_REPLACE` option was implemented by [MR 5877](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5877/diffs) and first appeared in CMake 3.21.

---

<div class="post-metadata">

**Author:** ![7schroet](https://discourse.cmake.org/user_avatar/discourse.cmake.org/7schroet/32/5966_2.png) [@7schroet](https://discourse.cmake.org/u/7schroet)\
**Post date:** [January 7, 2026, 11:05am UTC](https://discourse.cmake.org/t/file-rename-not-behaving-as-expected/15443/3 "2026-01-07T11:05:50Z")

</div>

Thanks for the quick reply, I opened [this issue](https://gitlab.kitware.com/cmake/cmake/-/issues/27491) as instructed. A fix is already on the way.
