mv Command in Linux

Learn via video courses
Topics Covered

Overview

The 'mv' command is a Linux command-line utility used to move or rename files and directories. It can be used to move a file or directory from one location to another, rename a file or directory, or both move and rename a file or directory simultaneously. This article will explain the syntax, options, and usage of the 'mv' command in Linux.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Linux mv Command Syntax

The syntax for the mv command is as follows:

Where:

  • OPTIONS: The 'mv' command has several options that can be used to modify its behavior.
  • source and destination: The 'source' and 'destination' parameters can be either a file or a directory. If 'destination' is a directory, the source file or directory is moved to that directory. If 'destination' is a new filename, the source file is renamed to that name.

mv Command Options:

  • -v or --verbose: Displays a message for each file or directory that is moved or renamed.
  • -i or --interactive: Asks for confirmation before overwriting an existing file.
  • -n or --no-clobber: Prevents overwriting an existing file.
  • -u or --update: Moves only those files that are newer than the existing files in the destination directory.

Example Usages

  • Move a file to a new location:

    Explanation: This command will move the 'file.txt' file to the '/home/user/Documents/' directory. If a file with the same name already exists in the destination directory, it will be overwritten without any warning.

  • Rename a file:

    Explanation: This command will rename the 'oldname.txt' file to 'newname.txt' in the same directory. If a file with the same name already exists in the directory, it will be overwritten without any warning.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Tips

  • Be careful when using the 'mv' command to avoid accidentally overwriting or deleting files.

  • Use the '-v' option to get a confirmation message for each file or directory that is moved or renamed.

  • Use the '-i' option to ask for confirmation before overwriting an existing file.

  • Use the '-n' option to prevent overwriting an existing file.

  • Use the '-u' option to move only those files that are newer than the existing files in the destination directory.

Advanced Use Cases of mv Command in Linux

  • Move multiple files to a new location:

    Explanation: This command will move the 'file1.txt', 'file2.txt', and 'file3.txt' files to the '/home/user/Documents/' directory.

  • Move all files with a certain extension to a new location:

    Explanation: This command will move all files with a '.txt' extension to the '/home/user/Documents/' directory.

  • Move a directory and all its contents to a new location:

    Explanation: This command will move the 'directory1' directory and all its contents to the '/home/user/Documents/' directory.

Conclusion

  • The 'mv' command in Linux is a powerful tool for moving and renaming files and directories.

  • The 'source' and 'destination' parameters can be either a file or a directory.

  • Use the '-v' option to get a confirmation message for each file or directory that is moved or renamed.

  • Be careful when using the 'mv' command to avoid accidentally overwriting or deleting files.

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more