Linux: Moving And Renaming Files

The mv command performs both file moving and file renaming, depending on how it is used. In either case, the original filename no longer exists after the operation. mv is used in much the same way as cp:
‘mv item1 item2′ is used to to move or rename file or directory “item1” to “item2” or: ‘mv item…’ directory is used to move one or more items from one directory to another.

Following are some command options with meaning:

-i, –interactive: Before overwriting an existing file, prompt the user for confirmation. If this option is not specified, mv will silently overwrite files.
-u, –update: When moving files from one directory to another, only move files that either don’t exist, or are newer than the existing corresponding files in the destination directory.
-v, –verbose: Display informative messages as the move is performed.

Following are some commands with result:

mv file1 file2: Move file1 to file2. If file2 exists, it is overwritten with the contents of file1. If file2 does not exist, it is created. In either case, file1 ceases to exist.
mv -i file1 file2: Same as above, except that if file2 exists, the user is prompted before it is overwritten.
mv file1 file2 dir1: Move file1 and file2 into directory dir1. dir1 must already exist.
mv dir1 dir2: If directory dir2 does not exist, create directory dir2 and move the contents of directory dir1 into dir2 and delete directory dir1. If directory dir2 does exist, move directory dir1 with its contents into directory dir2.

Leave a Reply