mv

mv

mv (short for move) is a Unix command that moves one or more files or directories from one place to another. Since it can "move" files from one filename to another, it is also used to rename files. Using mv requires the user to have write permission for the directories which the file will move between. This is because mv changes the file's location by editing the file list of each directory.

Contents

Conflicting existing file

When a filename is moved to an existing filename (in the same directory), the existing file is deleted. 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

move versus copy and remove

Usually moving files within the same file system is not the same as copying and then removing the original. First a new link is added to the new directory then the original link is deleted. The data of file is not accessed. This is much faster than physical copy and remove the file-content. The file still has the same inode.

When moving files to a different file system, all files are copied and then all files are removed. If the copy fails (as in not enough space) none of the original files are removed and all of the copied files remain (and the volume remains full!). If the files are on one volume, an out of space condition cannot occur.

You cannot copy a file if you do not have read permissions, but you can move it if you have write permission to its old and new directories.

If you do not have write permission to a non-empty directory, you cannot delete this directory (since you cannot delete its contents); but you can move it.

Options

Most versions Single Unix Specification#1980s: Motivation of mv support:

  • -h help by displaying additional options supported. Use man mv for details for the version on the system you are using.
  • -i interactively process, write a prompt to standard error before moving a file that would overwrite an existing file. If the response from the standard input begins with the character`y' or `Y', the move is attempted. (overrides previous -f or -n options.)
  • -n no overwriting of existing files. (overrides previous -f or -i options.)
  • -f force overwriting the destination (overrides previous -i or -n options).
  • -v verbose, shows filenames/directory names after they are moved.

Additional options (Use man mv for details):

  • -u update only when the original is newer than the destination or when the destination doesn't exist.
  • -b backup of existing destination using default ~ suffix.

Examples

 mv myfile mynewfilename     # renames 'myfile' to 'mynewfilename'.
 mv myfile ~/myfile          # moves 'myfile' from the current directory to user's home directory.
 mv myfile subdir/myfile     # moves 'myfile' to 'subdir/myfile' relative to the current directory.
 mv myfile subdir            # same as the previous command, filename is implied to be the same.
 mv myfile subdir/myfile2    # moves 'myfile' to 'subdir' named  'myfile2'.
 mv be.03 /mnt/bkup/bes      # copies 'be.03' to the mounted volume 'bkup' the 'bes' directory, 
                             # then 'be.03' is removed.
 mv afile another /home/yourdir/yourfile mydir 
                             # moves multiple files to directory 'mydir'.
 mv -v Jun* bkup/06          # displays each filename as it is moved to the subdirectory 'bkup/06'.
 mv /var/log/*z ~/logs       # takes longer than expected if '/var' is on a different file system, 
                             # as it frequently is, since files will be copied & deleted.
 
 mv --help                   # shows a concise help about the syntax of the command.
 man mv                      # displays complete manual for mv.

See also

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”