The mv command in Linux is used to move or rename files and directories. It’s commonly used for organizing files and changing file names from the command line.
mv [options] source destination
mv oldname.txt newname.txtmv file.txt /home/user/Documents/mv file.txt /backup/file_backup.txtmv file1.txt file2.txt folder/-i: Prompt before overwrite-f: Force move, suppress warnings-n: Do not overwrite existing files-v: Show what is being done$ mv -v report.txt archive/
'report.txt' -> 'archive/report.txt'
-i or -n to prevent accidental overwriting.mv *.jpg images/mv dir1/ dir2/The mv command in Linux is a powerful and essential tool used for moving and renaming files and directories. Whether you're organizing files, managing system resources, or scripting routine tasks, the mv command is one of the foundational Linux commands that every user should understand. In this guide, we will dive deep into its syntax, examples, flags, use cases, and best practices.
The mv command stands for "move". It is used to move one or more files or directories from one location to another or to rename files and directories. It does not copy files; it simply changes their location or name.
mv [options] source destination
- source: This is the file or directory you want to move or rename.
- destination: This is the new location or new name for the file/directory.
mv file.txt /home/user/Documents/
This moves file.txt to the Documents folder.
mv oldname.txt newname.txt
This renames oldname.txt to newname.txt.
mv file1.txt /home/user/Documents/file2.txt
This moves file1.txt to the Documents folder and renames it to file2.txt.
mv file1.txt file2.txt file3.txt /home/user/Downloads/
This moves all three files to the Downloads directory.
-i: Prompts before overwriting a file-f: Forces the move without prompting-n: Do not overwrite an existing file-u: Move only when the source is newer or destination is missing-v: Verbose mode – shows what is being donemv -i file.txt /tmp/
Asks for confirmation if file.txt already exists in /tmp/.
mv -f file.txt /tmp/
Overwrites existing files in /tmp/ without asking.
mv -v file1.txt /tmp/
Prints: 'file1.txt' -> '/tmp/file1.txt'
The mv command moves files (deleting from source), while cp copies files (retaining source). Use mv when you want to change the file location or rename it without duplication.
mv folder1/ /home/user/Documents/
This moves the entire folder folder1 and its contents.
mv myfolder oldfolder
This renames the folder from myfolder to oldfolder.
You can use wildcards to move multiple files of a specific type.
mv *.jpg /home/user/Pictures/
This moves all JPEG files to the Pictures directory.
#!/bin/bash
for file in *.log; do
mv "$file" /var/logs/archive/
done
This script moves all .log files to the archive folder.
-i or -n to avoid accidental overwriting--backup to create backups of overwritten files (in some Linux distributions)
count=1
for f in *.txt; do
mv "$f" "document_$count.txt"
((count++))
done
This renames all .txt files to document_1.txt, document_2.txt, etc.
mkdir -p archive
find . -type f -mtime +30 -exec mv {} archive/ \;
This moves files older than 30 days to the archive folder.
By default, if the destination file exists, mv will silently overwrite it. Use -i to prompt or -n to prevent overwriting.
- mv -i is safer and prompts for user confirmation.
- mv -f is faster and used in automation pipelines.
If you're moving files between different devices or filesystems, mv may perform a copy followed by a delete in the background.
cron for regular maintenancersync for network-based move operationsThe mv command is a vital utility in every Linux user's toolkit. It offers the functionality to move and rename files or directories with precision, flexibility, and control. Whether you're scripting tasks, organizing data, or managing user files, mastering the mv command will streamline your Linux workflow. Always remember to use safety flags like -i or -n to prevent unwanted file loss, and test your commands in safe environments when working with critical data.
The mv command works with both absolute and relative paths. Understanding the difference between them helps prevent file misplacement or unintended overwriting.
mv /home/user/Desktop/report.txt /home/user/Documents/
This uses full directory paths, which are unambiguous and ideal for scripting or when working from different directories.
mv report.txt ../Documents/
This example uses a path relative to the current working directory.
The mv command can also restructure entire folders or parts of the directory tree.
mv my_project /var/www/html/
This command moves the entire my_project directory, including all its subfolders and files.
In automated systems, the mv command is commonly used in cleanup, archiving, or deployment scripts. It's ideal for scripts that rotate logs, process uploaded files, or manage temporary directories.
#!/bin/bash
timestamp=$(date +%F)
mkdir -p /var/log/archive/$timestamp
mv /var/log/*.log /var/log/archive/$timestamp/
echo "Logs moved to archive for $timestamp"
This script organizes log files into dated folders automatically.
Sometimes, you'll want to rename many files using a pattern. While mv doesn't support pattern replacement by itself, you can use it in combination with loops or other tools like rename.
for file in *.txt; do
mv "$file" "${file%.txt}.bak"
done
This renames all .txt files to .bak in the current directory.
Here’s how you can schedule file movements using cron jobs.
0 0 * * * mv /home/user/uploads/* /home/user/processed/
This cron expression moves all uploaded files to the processed folder every day at 12:00 AM.
If your filenames contain spaces or special characters like &, $, or parentheses, quote them or use escaping to prevent errors.
mv "My File (Final).docx" "Final_Document.docx"
Always use quotes when filenames have spaces.
When you move files within the same filesystem, file ownership and permissions remain unchanged. However, if you move files across filesystems (e.g., from one disk to another), the permissions and ownership may change depending on the target’s default policies.
Many users ask if mv is faster than cp followed by rm. The answer depends:
mv only updates directory entries—very fast.mv performs a copy + delete under the hood—same as manually using cp then rm.One powerful way to organize directories is by grouping files by extension.
mv *.png ~/Pictures/screenshots/
This helps clean up cluttered directories with ease.
To move files between users or restricted directories, use sudo with the mv command.
sudo mv /home/user1/report.csv /home/user2/
mv: Can move and rename but one file at a time (or basic batch)rename: Used specifically for mass renaming based on patterns or regexThere is no built-in undo for the mv command. If you’ve overwritten a file or moved it incorrectly:
git for source-managed filesextundelete for ext3/ext4 file recovery (advanced)mv commands in a safe environmentGraphical file managers like Nautilus or Dolphin offer "drag and drop" moves. But the CLI mv command is scriptable, precise, and doesn't require a GUI—perfect for server environments and automation.
mv.chown and chmod after mv if files are moved across users or devicesmv to avoid data lossThe mv command, though simple in syntax, plays a vital role in Linux file management. It supports everything from renaming a file to building powerful automation tools. With the right flags and caution, it can be a time-saving and reliable part of any Linux workflow. Mastering this command not only boosts your Linux efficiency but also empowers you to handle large-scale data and system administration tasks with ease.
mv .* target_directory/, but be cautious—this can include .. (parent directory).-i to prompt or -n to skip.mv *.pdf /docs/ moves all PDF files./mnt/usb/).