Friday, December 23, 2011

mv: Changes the Name of a File

The mv (move) utility can rename a file without making a copy of it. The mv command line specifies an existing file and a new filename using the same syntax as cp:

mv existing-filename new-filename


CP copies the file
$ ls
memo

$ cp memo memo.copy

$ ls
memo memo.copy


MV renames the file
$ ls
memo

$ mv memo memo.0130
$ ls
memo.0130

Moving the files from 1 directory to other


The mv utility can be used for more than changing the name of a file.

When used to move one or more files to a new directory, the mv command has this syntax:

mv existing-file-list directory
If the working directory is /home/alex, Alex can use the following command to move the files - names and temp from the working directory to the literature directory:
$ mv names temp literature

This command changes the absolute pathnames of the names and temp files from /home/alex/names and /home/alex/temp to /home/alex/literature/names and /home/alex/literature/temp, respectively. Like most Linux commands, mv accepts either absolute or relative pathnames.

Moving directories from 1 Directory to other


Just as it moves ordinary files from one directory to another, so mv can move directories. The syntax is similar except that you specify one or more directories, not ordinary files, to move:

mv existing-directory-list new-directory
If new-directory does not exist, the existing-directory-list must contain just one directory name, which mv changes to new-directory (mv renames the directory). Although directories can be renamed using mv, their contents cannot be copied with cp unless you use the – r option. Refer to the explanations of tar and cpio for other ways to copy and/or move directories.

Caution: mv can destroy a file

Just as cp can destroy a file, so can mv. Also like cp, mv has a –i (interactive) option.

No comments:

Post a Comment