UNDERSTANDING AND USING THE MV COMMAND

In the Linux environment, the mv command is used for renaming as well as moving files. This fact proves confusing for many new Linux users who are migrating from the MS-DOS environment, in which the renaming and moving functions are separated into two distinct commands.

MV COMMAND ESSENTIALS

The mv command uses the following, basic syntax. Note that brackets indicate optional portions of the command:

mv [ option ] source destination

For option, you may include any of the options discussed subsequently.

For source, indicate the name of the source file (such as mydata.dat) if it is located in the working directory. If the source file is located elsewhere, add path information (such as ~/data/mydata.dat).

For destination, indicate the destination directory (if you are moving the file) or the new file name (if you are renaming the file).

Here are some examples:

Renaming a file: mv mydata.dat yourdata.dat renames mydata.dat to yourdata.dat.

Moving a file: mv mydata.dat ~/data/old moves mydata.dat from the working directory to a directory called data/old in the current user's home directory. (Note: Make sure that the destination directory exists before trying to move a file to a directory of that name.)

Renaming and moving a file at the same time: mv mydata.dat ~/data/old/mydata.old moves mydata.dat from the working directory to a directory called data/old in the current user's home directory and renames the file to mydata.old.

USING SHELL PATTERNS

By means of shell patterns, which are roughly analogous to wildcards in MS-DOS, you can move more than one file at a time. You can use any of the following shell patterns in an mv command:

An asterisk (*) stands for one or more characters. For example, *.jpg stands for any file that has the *.jpg extension.

A question mark (?) stands for any single character. For example, file?.txt stands for any of the following: file0.txt, fileX.txt, file9.txt, and so on.

A bracketed expression containing one or more characters matches any of the specified characters within the brackets. For example, file[123].text will match file1.txt, file2.txt, and file3.txt, but it won't match file4.txt or fileX.txt.

A bracketed expression containing two characters separated by a hyphen matches a range of characters. For example, [A-a]utobiography.txt matches Autobiography.txt and autobiography.txt.