Mv (Unix)

Last updated
mv
Original author(s) Ken Thompson, Dennis Ritchie
(AT&T Bell Laboratories)
Developer(s) Various open-source and commercial developers
Initial releaseNovember 3, 1971;51 years ago (1971-11-03)
Operating system Unix, Unix-like, Inferno, IBM i
Platform Cross-platform
Type Command
License coreutils: GPLv3

mv is a Unix command that moves one or more files or directories from one place to another. If both filenames are on the same filesystem, this results in a simple file rename; otherwise the file content is copied to the new location and the old file is removed. Using mv requires the user to have write permission for the directories the file will move between. This is because mv changes the content of both directories (i.e., the source and the target) involved in the move. When using the mv command on files located on the same filesystem, the file's timestamp is not updated.

Contents

On UNIX implementations derived from AT&T UNIX, cp, ln and mv are implemented as a single program with hard-linked binaries. The behavior is selected from the path name argv[0]. This is a common technique by which closely related commands that have been packaged as a unit allow the user to specify the particular course of the intended action.

History

A move command that moves a directory entry to a new directory was first implemented within Multics. It can be contracted to mv. [1] Later, the mv command appeared in Version 1 Unix [2] and became part of the X/Open Portability Guide issue 2 of 1987. [3] The version of mv bundled in GNU coreutils was written by Mike Parker, David MacKenzie, and Jim Meyering. [4] The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities. [5] The mv command has also been ported to the IBM i operating system. [6]

Conflicting existing file

When a filename is moved to an existing filename, the existing file is deleted (clobbered) by default. If the existing file is not writable but is in a directory that is writable, the mv command asks for confirmation (if run from a terminal) before proceeding, unless the -f (force) option is used.

Accidental overwriting can be prevented using the GNU -n (long format: --no-clobber) flag. 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 out of the way.

A related ambiguity arises when a filename is moved to an existing directory. By default, mv would handle this as one trying to move a name inside this directory. GNU mv has a -T switch for disabling this assumption and try to overwrite the directory instead. An inverse -t makes the move-to-directory operation explicit. [4]

Moving versus copying and removing

Moving files within the same file system is generally implemented differently than copying the file and then removing the original. On platforms that do not support the rename syscall, a new link is added to the new directory and the original one is deleted. The data of the file is not accessed. All POSIX-conformant systems implement the rename call.

An actual move (effectively a rename) is dramatically faster than the circuitous copy-and-move procedure. The file's i-number (short for "inode number") does not change. No permission is required to read the file being moved insofar as—conceptually speaking—it is only cataloguing information that is being changed as a result of the "move." Since the source and target directories are being modified, to wit, entries are being created within the target directory and erased from within the source directory, "write" permission in both directories is required to complete the move. 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 ".." (which can be used in any context as an alias for the parent of the current directory) changes as a result of the rename.

Options

Most versions of mv support:

These options are a part of X/Open Portability Guidelines, later the basis of POSIX and SUS. All POSIX-compliant mv implementations must support these. [7]

Examples

CommandExplanation
mv myfile mynewfilenamerenames 'myfile' to 'mynewfilename'.
mv myfile subdir/myfilemoves 'myfile' to 'subdir/myfile' relative to the current directory
mv myfile subdirsame as the previous command, filename is implied to be the same
mv be.03 /mnt/bkup/bescopies 'be.03' to the 'bes' directory of the mounted volume 'bkup', then 'be.03' is removed
mv be.03/* /mnt/bkup/besSame as above, except each file moved out of be.03 is deleted individually instead of all being deleted at once after the entire copying is finished.
mv afile another mydirmoves multiple files to directory 'mydir'
mv /var/log/*z ~/logsThis takes longer than expected if '/var' is on a different file system, as it frequently is, since files will be copied and deleted. The shell expands ~ to the user's home directory and treats * as a wildcard character.

Note that, in the above example, /mnt referred to the directory (the "mount point") over which a given file system is mounted. Naming such directories /mnt is a popular convention but is by no means necessary. A "file system" can be thought of as an independent tree that is logically regarded as a unit; its root is "mounted" atop a directory of the administrator's choice. Any previous contents of that directory are invisible, but they are "restored" when the new volume is unmounted.

See also

Related Research Articles

<span class="mw-page-title-main">Unix shell</span> Command-line interpreter for Unix operating system

A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to control the execution of the system using shell scripts.

Linux has several filesystem drivers for the File Allocation Table (FAT) filesystem format. These are commonly known by the names used in the mount command to invoke particular drivers in the kernel: msdos, vfat, and umsdos.

uniq is a utility command on Unix, Plan 9, Inferno, and Unix-like operating systems which, when fed a text file or standard input, outputs the text with adjacent identical lines collapsed to one, unique line of text.

In computing, tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes. The name is derived from "tape archive", as it was originally developed to write data to sequential I/O devices with no file system of their own. The archive data sets created by tar contain various file system parameters, such as name, timestamps, ownership, file-access permissions, and directory organization. POSIX abandoned tar in favor of pax, yet tar sees continued widespread use.

ls Command to list files and directories in Unix and Unix-like operating systems

In computing, ls is a command to list computer files and directories in Unix and Unix-like operating systems. It is specified by POSIX and the Single UNIX Specification.

In computing, a symbolic link is a file whose purpose is to point to a file or directory by specifying a path thereto.

ln (Unix) Unix file management utility

The ln command is a standard Unix command utility used to create a hard link or a symbolic link (symlink) to an existing file or directory. The use of a hard link allows multiple filenames to be associated with the same file since a hard link points to the inode of a given file, the data of which is stored on disk. On the other hand, symbolic links are special files that refer to other files by name.

xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.

dd is a command-line utility for Unix, Plan 9, Inferno, and Unix-like operating systems and beyond, the primary purpose of which is to convert and copy files. On Unix, device drivers for hardware and special device files appear in the file system just like normal files; dd can also read and/or write from/to these files, provided that function is implemented in their respective driver. As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data. The dd program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.

wc (Unix) Unix command utility

wc is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. The program reads either standard input or a list of computer files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow.

pax is an archiving utility available for various operating systems and defined since 1995. Rather than sort out the incompatible options that have crept up between tar and cpio, along with their implementations across various versions of Unix, the IEEE designed new archive utility pax that could support various archive formats with useful options from both archivers. The pax command is available on Unix and Unix-like operating systems and on IBM i, and Microsoft Windows NT until Windows 2000.

cp (Unix) Unix command utility

In computing, cp is a command in various Unix and Unix-like operating systems for copying files and directories. The command has three principal modes of operation, expressed by the types of arguments presented to the program for copying a file to another file, one or more files to a directory, or for copying entire directories to another directory.

df (Unix) Standard Unix command

df is a standard Unix command used to display the amount of available disk space for file systems on which the invoking user has appropriate read access. df is typically implemented using the statfs or statvfs system calls.

In Unix-like and some other operating systems, find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each matched object.

rm (Unix) Unix command utility

rm is a basic command on Unix and Unix-like operating systems used to remove objects such as computer files, directories and symbolic links from file systems and also special files such as device nodes, pipes and sockets, similar to the del command in MS-DOS, OS/2, and Microsoft Windows. The command is also available in the EFI shell.

tail is a program available on Unix, Unix-like systems, FreeDOS and MSX-DOS used to display the tail end of a text file or piped data.

In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing.

test is a command-line utility found in Unix, Plan 9, and Unix-like operating systems that evaluates conditional expressions. test was turned into a shell builtin command in 1981 with UNIX System III and at the same time made available under the alternate name [.

In computing, which is a command for various operating systems used to identify the location of executables. The command is available in Unix and Unix-like systems, the AROS shell, for FreeDOS and for Microsoft Windows. The functionality of the which command is similar to some implementations of the type command. POSIX specifies a command named command that also covers this functionality.

pr is a command on various operating systems that is used to paginate or columnate computer files for printing. It can also be used to compare two files side by side, as an alternative to diff.

References

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