The mv Command

The mv command is used to rename and move files and directories. Its general syntax is:

mv [options] argument(s)

The arguments are names of files and directories. If two file names are provided as arguments, mv renames the first as the second. If a list of arguments is provided and the final argument in the sequence is the name of an existing directory, mv moves all of the other items into that directory. If the final argument is not an existing directory and more than two arguments are provided, an error message is returned.

mv's syntax can also be expressed in a less general form as:

mv [options] source target

If the target file is located in the same directory as the source file, then the source file can only be renamed. If both are in different directories, then the source file is moved to the directory named in the target argument, in which it can keep its original name or be assigned a new name. If the target is a directory, then the source file or directory is moved into that directory and retains its original name.

Thus, for example, the following would rename a file called file1 to file2, while keeping it in the current directory (i.e., the directory in which the user is currently working):

mv file1 file2

The following would move a file named file3, without changing its name, from the current directory to an existing subdirectory of the current directory named dir1:

mv file3 dir1/file3

mv can be used to move any number of files and directories simultaneously. For example, the following command moves all files and directories, including all the contents of those directories, from the current directory to the directory /home/alice/new/:

mv * /home/alice/new/

The asterisk is a wildcard character that represents any string (i.e., sequence of characters). Thus, in the above example it represents the name of every file and directory in the current directory.

mv makes it as easy to move a file or directory up the hierarchy of directories (i.e., closer to the root directory) as down it. For example, the following would move a file named file4, which is currently located in the sub-subdirectory dir/dir/ of the user's home directory, to the top level in the user's home directory:

mv dir/dir/file4 ~

The root directory is the directory that contains all other directories on a Unix-like operating system and which is at the top of the hierarchy of directories. A user's home directory is the directory in which a user finds itself by default after logging into the system and which can be represented by the tilde (wavy horizontal line character).

By default, mv does not provide any confirmation on the display screen if its action is completed without problems. This is consistent with the rule of silence tenet of the Unix philosophy.

Thus it is wise for users new to Unix-like operating systems to always use the -i option, which makes mv interactive in the situation in which files and/or directories with the same name already exist in the destination directory. For example, the above command would be made interactive as follows:

mv -i * /home/alice/new/

Among mv's few other options are -b, which tells it to make a backup copy of each file that would otherwise be overwritten or removed, and -v, which tells it to be verbose and display the name of each file before moving it. Detailed information (including all options) about mv can be obtained by using its --help option, and information about the current version can be obtained by using its --version option.

Created May 4, 2006.
Copyright © 2006 The Linux Information Project. All Rights Reserved.