The mv command

The mv command moves files or directories to a new directory, or renames them in their existing one:

$ mv file path/to/directory
$ mv file1 file2

If the last argument is a directory, all of the arguments before it are moved into it, allowing you to move several files at once:

$ mv file1 file2 dir1 path/to/directory

Note that mv overwrites any existing file at its destination, by design. When you're using this interactively, if you don't expect to be overwriting files, the -i switch can be used to prompt you before you overwrite a file:

$ ls
customers-new  customers
$ mv -i customers-new customers
mv: overwrite 'customers'?

If you enter y at this prompt and press Enter, the overwrite will proceed. This feature is designed for user interaction, so in scripts, it may be better to test for the existence of the destination file before calling mv, and handling it an appropriate way:

#!/bin/bash
if [[ -e customers ]] ; then printf 'Customers file already exists, renaming\n' mv customers customers-old fi mv customers-new customers

The preceding script would move customers to customers-old first, if the target file already exists.