Linux Command mv Usage and 4 Options

The mv command is a fundamental tool in Linux for moving or renaming files and directories. This post will explore the basic usage of the Linux command mv, various options, and practical examples.

What is the Linux mv Command?

The Linux command mv is short for “move” and is suitable for the following scenarios:

  • Moving files and directories: Use it to move files or directories to a different location.
  • Renaming files and directories: Use it to change the name of files or directories.

Basic Usage

The basic usage of the Linux command mv is as follows:

mv [options] source target
ShellScript

Renaming Files/Directories

For example, to rename file1.txt to file2.txt, enter the original file as the first parameter and the new file name as the second parameter, as shown in line 1 of the code below. You can also rename directories as shown in line 2.

mv file1.txt file2.txt
mv dir1 dir2
ShellScript

아래의 그림은 a.txt 파일을 b.txt 파일로 이름을 변경한 경우입니다.

Figure 1. Linux Command mv: Renaming file
Figure 1. Linux Command mv: Renaming file

Moving Files

To move a file, enter the file to be moved as the first parameter and the destination path as the second parameter. In the example below, the file1.txt file from the current directory is moved to the /home/ito/Documents/ path.

mv file1.txt /home/ito/Documents/
ShellScript

When moving a directory, all files within the directory are moved as well. Therefore, unlike the cp command, there is no need to use the -r (recursive) option with the mv command.

Figure 2. Linux Command mv: Moving Directory
Figure 2. Linux Command mv: Moving Directory

Useful Options

The mv command includes several options, many of which perform similar functions to those found in the cp command. Here are a few commonly used options:

-i option (interactive): Overwrite warning

By default, the mv command overwrites the target file without any warning. To prevent accidental overwrites, use the -i (interactive) option, which prompts for confirmation before overwriting.

mv -i file1.txt file2.txt
ShellScript

If the target file does not exist, the command executes without any message. However, if the target file exists, you will see a message like “overwrite ‘filename’?” as shown below.

Figure 3. Linux Command mv -i option for overwrite confirmation
Figure 3. Linux Command mv -i option for overwrite confirmation

-n option (no clobber): Prevent overwriting

To avoid overwriting an existing target file, use the -n option.

mv -n file1.txt file2.txt
ShellScript

The example below shows an attempt to move b.txt to a.txt, but the a.txt file is not replaced because of the -n option.

Figure 4. Linux Command mv -n option for not to overwriting
Figure 4. Linux Command mv -n option for not to overwriting

-v option (verbose): Detailed output

The -v option provides detailed information about the move process, helping you identify any issues during the operation.

mv -v file1.txt file2.txt
ShellScript

The example below shows all files starting with “m” being moved to the test directory.

Figure 5. Linux Command mv -v option for detailed output
Figure 5. Linux Command mv -v option for detailed output

-u option (update): Move only newer files

The -u option moves the source file only if the target file is older. This helps reduce unnecessary file moves.

mv -u file1.txt file2.txt
ShellScript

In the example below, m1.txt is the oldest file, m2.txt is next, and m3.txt is the newest. The -u option prevents moving m1.txt to m2.txt but successfully renames m3.txt to m2.txt. When used with the -v option, it provides detailed output only for successful moves.

If you use the -u option together with the -v option as shown below, you will see that detailed output is provided only when the file move is successfully executed.

Figure 6. Linux Command mv -u option for moving only newer files
Figure 6. Linux Command mv -u option for moving only newer files

Precautions

File names with spaces

When moving or renaming files with special characters or spaces, enclose the file names in quotes to avoid unexpected results.

mv "My File.txt" "New File.txt"
ShellScript

The following example shows a case where a file name with spaces is not enclosed in quotes. A command without quotes as shown below will interpret the first parameter as “My”, the second parameter as “File.txt”, and the third parameter as “New”, thus attempting to move these three files into a directory named “File.txt”.

If you don’t enclose the file name in quotes, the command interprets it incorrectly, as shown below.

Figure 7. Linux Command mv: Incorrectly handling file names with spaces
Figure 7. Linux Command mv: Incorrectly handling file names with spaces

File permissions

If you lack the necessary permissions to move or rename a file, use the sudo command to execute the mv command with elevated privileges.

sudo mv file1.txt /root/
ShellScript

Multiple options

When using multiple options, combine them for convenience. For example, to use both -i and -v options, you can write:

mv -iv file*.txt ~/Documents/
ShellScript

Practical Uses

  • Managing backup files: Use the mv command to move working files to a backup folder.
  • Organizing log files: Regularly move log files to a different location for organization.
  • Automating file management: Combine with tools like crontab to automate file management tasks.

Summary

The Linux mv command is an essential tool for moving and renaming files and directories. With its basic usage and various options, you can efficiently manage your files. If you have any questions or need further information, feel free to ask!

We hope this guide helps you make the most of your Linux experience!

Reference

Leave a Comment