|Previous||Table of Contents||Next|
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:
The general formats of the mv command follow.
Only one option may be used to control how mv functions.
|-f||Force 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.|
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.|
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"|
The mv command must copy the existing file to the destination and remove the original if the files reside on different file systems. All links are lost if this action occurs.
Refer to the cp, ln, rm, chmod, chown, and ls commands described in modules 24, 76, 114, 17, 18, and 84, respectively.
The mv command is used whenever you need to rename or move a file. It allows you to organize your files as you like. For example, if you had written 15 letters with names of LTR1 through LTR15, you use mv to move them to a subdirectory named LETTERS. The command mv LTR[0-9]* LETTERS would move all of your letters to the LTRS directory.
The mv command is often used to rename a directory. To rename your LETTERS directory to LTRS you type mv LETTERS LTRS.
Another common use is to rename default filenames with a meaningful filename. For example, the C compiler creates the a.out executable file. You use can mv to rename a.out to a new filename so you know what the program does; for example, mv a.out chess.
In this activity you use the mv command to rename a file. Begin at the shell prompt.
cj> ls -x bin calendar db file1 file2 letters
cj> ls -x bin calendar db file1 letters stuff
|Previous||Table of Contents||Next|