Skip to content

mv Command: Moving and Renaming Files in Linux

Summary

The mv command is a fundamental Linux utility used for moving or renaming files and directories. It's a versatile tool for file management, allowing users to organize their data efficiently.

Introduction

The mv command, short for "move," is a command-line utility that moves files or directories from one location to another or renames them in place. When moving a file, mv essentially changes the file's location in the file system. When renaming a file, it changes the filename without altering its content or location.

Use case and Examples

Moving a File

mv file.txt /home/user/documents
This command moves file.txt from the current directory to the /home/user/documents directory.

Renaming a File

mv old_file.txt new_file.txt
This command renames old_file.txt to new_file.txt within the same directory.

Moving a Directory

mv my_directory /opt
This moves the directory my_directory from the current location to the /opt directory.

Moving Multiple Files

mv file1.txt file2.txt /home/user/backup/
This command moves both file1.txt and file2.txt to the /home/user/backup/ directory.

Renaming while Moving

mv file.txt /home/user/documents/renamed_file.txt
This command moves file.txt to /home/user/documents/ and renames it to renamed_file.txt.

Commonly used flags

Flag Description Example
-f or --force Forcefully moves or renames files, overwriting existing files without prompting. mv -f file.txt /path/to/destination
-i or --interactive Prompts the user before overwriting existing files. mv -i file.txt /path/to/destination
-n or --no-clobber Prevents overwriting existing files. If the destination file exists, mv will not overwrite it. mv -n file.txt /path/to/destination
-v or --verbose Displays verbose output, showing each file as it is moved. mv -v file.txt /path/to/destination
-u or --update Moves the file only if the source file is newer than the destination file or if the destination file does not exist. mv -u file.txt /path/to/destination


Share on Share on

Comments