Linux mv Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux mv command to rename files, move files to a new directory, and move multiple files to a new directory. The mv command is a fundamental tool for managing files and directories in the Linux operating system. You will start by understanding the basic syntax and usage of the mv command, then practice renaming a single file and moving multiple files to a new directory. This lab covers essential file and directory operations that are commonly used in Linux-based environments.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/BasicFileOperationsGroup -.-> linux/mv("`File Moving/Renaming`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/mkdir -.-> lab-422832{{"`Linux mv Command with Practical Examples`"}} linux/ls -.-> lab-422832{{"`Linux mv Command with Practical Examples`"}} linux/cp -.-> lab-422832{{"`Linux mv Command with Practical Examples`"}} linux/mv -.-> lab-422832{{"`Linux mv Command with Practical Examples`"}} linux/rm -.-> lab-422832{{"`Linux mv Command with Practical Examples`"}} linux/touch -.-> lab-422832{{"`Linux mv Command with Practical Examples`"}} end

Understand the mv Command Syntax

In this step, you will learn the basic syntax and usage of the mv command in Linux. The mv command is used to move or rename files and directories.

The basic syntax of the mv command is:

mv [options] source destination

Here, source is the file or directory you want to move, and destination is the new location or new name for the file or directory.

Some common options for the mv command include:

  • -i: Interactive mode, prompts before overwriting
  • -f: Force mode, overwrites without prompting
  • -v: Verbose mode, shows the details of the move operation

Let's try some examples:

## Rename a file
mv file1.txt file2.txt

## Example output:
## No output, the file is renamed

## Move a file to a new directory
mv file1.txt ~/project/new_dir/

## Example output:
## No output, the file is moved

## Move multiple files to a new directory
mv file1.txt file2.txt file3.txt ~/project/new_dir/

## Example output:
## No output, the files are moved

In the above examples, we used the mv command to rename a file, move a file to a new directory, and move multiple files to a new directory.

Rename a Single File

In this step, you will learn how to use the mv command to rename a single file.

First, let's create a file in the ~/project directory:

touch ~/project/original_file.txt

Now, let's rename the file using the mv command:

mv ~/project/original_file.txt ~/project/renamed_file.txt

Example output:

No output, the file is renamed

As you can see, the mv command is used to rename the file from original_file.txt to renamed_file.txt.

Let's verify that the file has been renamed:

ls -l ~/project

Example output:

-rw-r--r-- 1 labex labex 0 Apr 12 12:34 renamed_file.txt

The output shows that the file original_file.txt has been renamed to renamed_file.txt.

Move Multiple Files to a New Directory

In this step, you will learn how to use the mv command to move multiple files to a new directory.

First, let's create some files in the ~/project directory:

touch ~/project/file1.txt ~/project/file2.txt ~/project/file3.txt

Now, let's create a new directory to move the files to:

mkdir ~/project/new_dir

To move the files to the new directory, we can use the mv command:

mv ~/project/file1.txt ~/project/file2.txt ~/project/file3.txt ~/project/new_dir/

Example output:

No output, the files are moved

As you can see, we specified the source files and the destination directory in the mv command.

Let's verify that the files have been moved:

ls -l ~/project/new_dir

Example output:

-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file1.txt
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file2.txt
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 file3.txt

The output shows that the files file1.txt, file2.txt, and file3.txt have been moved to the ~/project/new_dir directory.

Summary

In this lab, you learned the basic syntax and usage of the mv command in Linux, which is used to move or rename files and directories. You practiced renaming a single file, as well as moving multiple files to a new directory. The mv command has several useful options, such as -i for interactive mode, -f for force mode, and -v for verbose mode, which can be used to customize the behavior of the command. By understanding and practicing the mv command, you gained valuable skills for managing files and directories in a Linux environment.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like