Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

mv (Unix)

From Wikipedia, the free encyclopedia
Shell command for moving files
mv
Original authorsKen Thompson,Dennis Ritchie
(AT&T Bell Laboratories)
DevelopersVariousopen-source andcommercial developers
Initial releaseNovember 3, 1971; 54 years ago (1971-11-03)
Operating systemUnix,Unix-like,Inferno,IBM i
PlatformCross-platform
TypeCommand
Licensecoreutils:GPLv3

mv is ashellcommand for renaming and movingfiles anddirectories. If both items are on the samefile system, the command renames; otherwise items are copied to the destination and the old items are removed (more specificallyunlinked). To move between two directories, the user must have write permission for both because the command modifies the content of both. For a rename, an item's timestamp is not modified.

OnUnix implementations derived fromAT&T Unix,cp,ln andmv are implemented as a single program withhard-linked binaries.

History

[edit]

Multics provided a file move/rename command namedmove. A version with the contracted namemv[1] appeared inVersion 1 Unix[2] and became part of theX/Open Portability Guide issue 2 of 1987.[3] The version inGNU Core Utilities was written by Mike Parker, David MacKenzie, and Jim Meyering.[4] The command is available inWindows viaUnxUtils.[5] The command was ported toIBM i.[6]

Options

[edit]

Most implementations support:

  • -iinteractive; the command prompts the user to confirm moving each file that would overwrite an existing file; overrides a preceding-f option
  • -fforce overwriting existing files; overrides a preceding-i option

These options are a part of X/Open Portability Guidelines, later the basis of POSIX andSUS. A POSIX-compliant implementation must support these.[7]

Name clashing

[edit]

When a file is moved to a path that specifies an existing file, the existing file isclobbered by default. If the existing file is not writable but is in a directory that is writable, the command prompts the user to confirm overwrite (if run from a terminal), unless the-f option is included.

Accidental overwriting can be prevented using the GNU implementation-n (long format:--no-clobber) option. Alternatively,-u (--update) only overwrites destination files that are older than source files,-i (--interactive) asks for confirmation upon each name conflict, and-b (--backup) renames target files to prevent overwrite.

Ambiguity arises when a file is moved to a path that specifies an existing directory. By default,mv handles this by moving the file inside the directory. The GNU implementation has a-T switch for that tries to overwrite the directory instead. An inverse-t makes the move-to-directory operation explicit.[4]

Moving versus copying and removing

[edit]

Moving files within the samefile system is generally implemented more efficiently than copying the file and then removing the original. On platforms that do not support therename()system call, a newhard link is added to the new directory and the original one is deleted. The data of the file is not accessed. APOSIX-conformant system implementsrename().

Such an operation is significantly simpler and faster than a copy-and-move operation. The file'sinode number (i-number) does not change. No permission is required to read the file since only cataloguing information is changed. Since the source and target directories are being modified, entries are being created within the target directory and erased from within the source directory, write permission in both directories is required. Moving files from one file system to another may fail entirely or may be automatically performed as an atomic copy-and-delete action; the actual details are dependent upon the implementation.

Moving a directory from one parent to a different parent directory requires write permission in the directory being moved, in addition to permissions to modify the old and new parents. This is because the i-number for the directory entry ".." (an alias for the parent of a directory) changes as a result of the rename.

Examples

[edit]

The following renames file or directoryfoo tobar. This assumes thatbar is not an existing directory beforehand.

$ mv foo bar

The following moves the file or directoryfoo into the existing subdirectorysubdir so that the result is at pathsubdir/foo.

$ mv foo subdir

The following moves the file or directoryfoo into directorysubdir with namebar so that the resulting is pathsubdir/bar. This assumes thatsubdir/bar is not an existing directory beforehand.

$ mv foo subdir/bar

The following moves two files/directories,foo andbar, to existing directorysubdir.

$ mv foo bar subdir

Copybe.03 to thebes directory of themounted volumebkup, thenbe.03 is removed. In this example,/mnt refers to the directory (the "mount point") over which a file system is mounted.

$ mv be.03 /mnt/bkup/bes

Same as above, except each file moved out ofbe.03 is removed individually instead of all being removed at once after the entire copying is finished.

$ mv be.03/* /mnt/bkup/bes

The following takes longer than expected if/var is on a different file system, as it frequently is, since files will be copied and removed. The shell expands~ to the user'shome directory and treats* as awildcard character.

$ mv /var/log/*z ~/logs

See also

[edit]

References

[edit]
  1. ^Multics Commands
  2. ^mv(1) – FreeBSD General CommandsManual
  3. ^mv: move files – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Group
  4. ^abmv(1) – Linux General CommandsManual from ManKier.com
  5. ^"Native Win32 ports of some GNU utilities".unxutils.sourceforge.net.
  6. ^IBM."IBM System i Version 7.2 Programming Qshell"(PDF).IBM. Retrieved2020-09-05.
  7. ^Single Unix Specification#1980s: Motivation

External links

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands/File System Utilities#mv
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
File system
Text utilities
Shell utilities
Retrieved from "https://en.wikipedia.org/w/index.php?title=Mv_(Unix)&oldid=1305077213"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp