Module 91 mv

Previous Table of Contents Next


Module 91
mv

DESCRIPTION

The external mv command moves a file from an existing location to a new location. It is also used as the rename command to change the name of a file. It has three formats allowing you to:

   Rename a file with a new filename.
   Move one or more files to reside under a different directory.
   Rename a directory with a new directory name.

COMMAND FORMAT

The general formats of the mv command follow.

mv [ -if ] [ - ] filename new_filename
mv [ -if ] [ - ] filename directory
mv [ -if ] [ - ] old_directory new_directory

Options

Only one option may be used to control how mv functions.

-f F orce the move to occur. The response of yes is assumed and the move is performed. No interactive response is requested .
If the standard input is not the terminal keyboard, then the -f option is assumed.
-i The command runs in interactive mode. If the destination file being moved to already exists, mv prompts you with the filename being moved followed by a question mark. If you respond with any string beginning with a y , the move is performed. Any other response causes mv to skip to the next move.
- Interpret all arguments that follow as filenames. This allows you to specify filenames and directories that begin with a hyphen (-). This can become helpful if a filename is created that begins with a hyphen.

Arguments

following list describes the arguments that may be passed to the mv command.

filename The existing file being moved or renamed .
filename_list The names of files to be moved to a new destination. The destination must be a directory.
new_filename The new filename the existing file will be given.
directory The name of the directory the files will reside in after the mv is performed.
old_directory The existing directory being renamed.
new_directory The name of a newly created directory. The destination directory.

FURTHER DISCUSSION

The first format renames a file. The name of the existing file is changed to the name of new_filename. The new_filename and the existing filename cannot be the same full pathname. They may have the same filename, providing they reside in different directories. If the new filename is an existing file and you have write permissions, its contents are lost. If the destination is a link to another file, the link is lost and the destination filename points to the new file.

The second format moves one or more files to a different directory. The filename_list is a list of pathnames being moved. The basename of each pathname is used for the destination filename in the specified directory. For example, if you wanted to move all files beginning with memo to the memodir directory, you would type mv memo* memodir.

The third format renames a directory. The name of the directory is changed to the new directory name you specify. The same restrictions apply to directory renaming as file renaming. If you are renaming directories, the two directory names reside in the same file system.

The mv command actually performs an ln and an rm . A link ( ln ) is created from the existing file to the new filename. Then the existing filename is removed ( rm ). If the new filename already exists, it is removed ( rm ) before the move attempts to link.

Before mv performs these steps it checks the destination file for write permissions. If you do not have write permissions on the destination file or directory, mv will display the mode ( chmod ; Module 17) and request a response from standard input. If the input line you type starts with a y, mv attempts to move the file to the target. If the ownership permissions allow you to write to the target, the move will occur. If you do not have permission to write to the destination, the mv will fail.

If you are moving files across file systems, the mv command performs a cp and then an rm command. The file is copied to its destination and then the existing file is removed. The new file is owned by you if you moved it.

Some common formats are:

mv letter* LTR # move all files beginning with letter to the
# LTR directory
mv pup dog # rename the file pup to dog
mv LTR LTRS # rename directory LTR to LTRS


Previous Table of Contents Next

Copyright Wordware Publishing, Inc.


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net