> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man1/mv.1.md).

# mv.1

`mv` — 移动文件

## 名称

`mv`

## 概要

`mv [-f | -i | -n] [-hv] source target`

`mv [-f | -i | -n] [-v] source ... directory`

## 描述

在第一种形式中，`mv` 实用程序将 `source` 操作数命名的文件重命名为 `target` 操作数命名的目标路径。当最后一个操作数命名的不是已存在的目录时，假定使用此形式。

在第二种形式中，`mv` 将每个 `source` 操作数命名的文件移动到 `directory` 操作数命名的现有目录中的目标文件。每个操作数的目标路径是由最后一个操作数、斜杠和命名文件的最后路径分量连接而成的路径名。

以下选项可用：

**`-f`** 在覆盖目标路径之前不提示确认。（`-f` 选项覆盖任何先前指定的 `-i` 或 `-n` 选项。）

**`-h`** 如果 `target` 操作数是指向目录的符号链接，则不跟随它。这使 `mv` 实用程序将文件 `source` 重命名为目标路径 `target`，而非将 `source` 移动到 `target` 引用的目录中。

**`-i`** 使 `mv` 在移动会覆盖现有文件之前向标准错误输出写入提示。如果来自标准输入的响应以字符 ‘`y`’ 或 ‘`Y`’ 开头，则尝试移动。（`-i` 选项覆盖任何先前指定的 `-f` 或 `-n` 选项。）

**`-n`** 不覆盖现有文件。（`-n` 选项覆盖任何先前指定的 `-f` 或 `-i` 选项。）

**`-v`** 使 `mv` 详细输出，在文件移动后显示它们。

如果 `source` 操作数指定的是目录，而目标存在且不是目录，则是错误的。

如果目标路径的权限不允许写入，`mv` 按 `-i` 选项的规定提示用户确认。

由于 [rename(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man2/rename.2.md) 调用不能跨文件系统工作，`mv` 使用 [cp(1)](/man/man1/cp.1.md) 和 [rm(1)](/man/man1/rm.1.md) 来完成移动。其效果等价于：

```sh
rm -f destination_path && \
cp -pRP source_file destination && \
rm -rf source_file
```

## 退出状态

`mv` 实用程序成功时退出值为 0，发生错误时大于 0。

## 实例

将文件 `foo` 重命名为 `bar`，如果 `bar` 已存在则覆盖它：

```sh
$ mv -f foo bar
```

## 兼容性

`-h`、`-n` 和 `-v` 选项是非标准的，不建议在脚本中使用。

## 参见

[cp(1)](/man/man1/cp.1.md), [rm(1)](/man/man1/rm.1.md), [symlink(7)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man7/symlink.7.md)

## 标准

`mv` 实用程序预期与 IEEE Std 1003.2 ("POSIX.2") 兼容。

## 历史

`mv` 命令首次出现于 Version 1 AT\&T UNIX。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man1/mv.1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
