Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

mv - move files

&pagelevel(4)&pagelevel

mv is used to change the name of a file, or to move a file from one directory to another within the file tree. To be able to use mv you have to have write permission for the directory in which the file resides or the directory to which you want to move it, as appropriate.

mv does not make copies of files that are moved or renamed within the same file system. Instead, it simply modifies the appropriate entries in the parent directory, retaining any links to other files.

If a file is moved to another file system, however, mv uses the cp command. In this case the original file is first copied and then deleted, and all links to other files are lost.


Syntax


Format 1: mv[ -f][ -i] file newfile
Format 2: mv[ -f][ -i] file ... dir
Format 3: mv[ -f][ -i] dir newdir



Renaming a file
Format 1: mv[ -f][ -i] file newfile


No option specified

If you specify an existing file for newfile and do not have write permission for it, mv
displays the mode of newfile and prompts you to confirm whether it should proceed. It
only does so if your answer begins with y.


Caution!
If the standard input is not a terminal, no prompt for confirmation is displayed and newfile will not be overwritten).


options

-f 

If a file named newfile already exists, mv will overwrite the existing file even if you do not have write permission for it.

-i

(interactive)

If you specify an existing file for newfile, you will always be asked to confirm whether mv should really proceed.

If you enter more than one specification for -f or -i then no error occurs. The final option to be specified determines the behavior of mv.

file

Name of the file to be renamed.

newfile

New name for the file; must not be the same as file. If a file named newfile already exists, it is overwritten by the contents of file if you have write permission for newfile (see also -i).

If you specify an existing file for newfile and do not have write permission for it, mv will display the mode of newfile and prompt you to confirm whether it should proceed. The existing newfile is not overwritten unless you answer y. If the -f option is specified, confirmation will not be requested and newfile will be overwritten.
If the standard input is not a terminal, confirmation will not be requested and newfile will not be overwritten

If the parent directory of newfile is writable but has the sticky bit (the t bit) set, one or more of the following conditions must be satisfied:

  • the user must own the file

  • the user must own the directory

  • the user must have write permission for the file

  • the user must be a privileged user

Moving files and directories to another directory
Format 2: mv[ -f][ -i] file ... dir


option

see format 1

file

Names of files or directories to be moved to directory dir. If you name a directory as source, all the files and directories under it are moved recursively.

dir

Name of the directory to which the files and/or directories are to be moved.You needwrite permission for this target directory.

If dir is writable but has the t bit (sticky bit) set, one or more of the following conditions must be satisfied:

  • the user must own the file

  • the user must own the target directory

  • the user must have write permission for the file

  • the user must be a privileged user

Renaming a directory
Format 3: mv[ -f][ -i] dir newdir


option

see format 1

dir

Name of the directory to be renamed.

newdir

New name for the directory.

dir and newdir must belong to the same physical file system; however, they do not have to share the same parent directory.

If a directory named newdir already exists, the directory named dir is moved to the newdir directory.

If newdir/dir already exists then mv only moves the directory dir if newdir/dir is empty.

Error

mv: Cannot rename <dir> to <newdir> : Permission denied

You do not possess write permission for the directory <dir> which is to be renamed.


mv: Cannot create <file> : Permission denied

You do not possess write permission for the directory to which <file> is to be moved.

Locale

The following environment variables affect the execution of mv:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_COLLATE

Determine the locale for the behavior of ranges, equivalence classes and multicharacter collating elements used in the extended regular expressions defined for yes/no queries.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1

The file songs in the current working directory is to be renamed popsongs and moved to the directory /home/joanne/arts/music.

$ mv songs /home/joanne/arts/music/popsongs

Example 2

The files daisy, rose and violet in the current working directory are to retain their names and be moved to the directory /home/joanne/flowers.

$ mv daisy rose violet /home/joanne/flowers

See also

chmod, cp, find, ln, rm