Linux mv Command


To see the mv command in action with output, I'll go through some examples using both -v (verbose) and -i (interactive) options, so you can see what happens in various scenarios.

Example 1: Moving a File with Verbose Output

When using mv with the -v option, it shows each move operation.

Command:

mv -v file1.txt /home/user/Documents/

Output:

'file1.txt' -> '/home/user/Documents/file1.txt'

This indicates that file1.txt has been moved from the current directory to /home/user/Documents/.

Example 2: Renaming a File

Renaming a file within the same directory can be done by specifying the current and new names.

Command:

mv -v oldname.txt newname.txt

Output:

'oldname.txt' -> 'newname.txt'

This shows that oldname.txt was renamed to newname.txt.

Example 3: Moving Multiple Files to a Directory

You can move multiple files by listing them before the destination directory.

Command:

mv -v file1.txt file2.txt file3.txt /home/user/Documents/

Output:

'file1.txt' -> '/home/user/Documents/file1.txt' 'file2.txt' -> '/home/user/Documents/file2.txt' 'file3.txt' -> '/home/user/Documents/file3.txt'

Each file is moved individually, and the output shows the source and destination paths.

Example 4: Interactive Mode with -i Option

If a file with the same name already exists in the destination, mv -i will prompt you to confirm overwriting.

Command:

mv -i file1.txt /home/user/Documents/

Output:

mv: overwrite '/home/user/Documents/file1.txt'? y

If you type y, it will overwrite the existing file. If you type n, it will cancel the move for that file.

Example 5: Force Overwrite with -f Option

If you want to move files without any prompt, use the -f (force) option.

Command:

mv -f file1.txt /home/user/Documents/

Output:

No output is shown because it forcibly overwrites the file without confirmation. If the destination file exists, it will be overwritten silently.

Example 6: Moving All Files with a Specific Extension

You can move all files of a certain type (e.g., .txt) to a directory using a wildcard (*).

Command:

mv -v *.txt /home/user/Documents/

Output:

'file1.txt' -> '/home/user/Documents/file1.txt' 'file2.txt' -> '/home/user/Documents/file2.txt' 'file3.txt' -> '/home/user/Documents/file3.txt'

This command is useful for batch operations. Each .txt file in the current directory will be moved to /home/user/Documents/.