Skip to content

Linux mv Command: Moving and Renaming Files Guide

Published: at 03:35 PMSuggest Changes

The mv (move) command is used to move or rename files and directories in Linux. This guide covers all aspects of file movement and renaming operations.

Basic Syntax

mv [options] source destination

Common Options

Real-World Examples

1. Basic File Moving

# Move file to directory
$ mv file.txt Documents/

# Move multiple files
$ mv file1.txt file2.txt Documents/

# Rename file
$ mv oldname.txt newname.txt

2. Directory Operations

# Move directory
$ mv source_dir/ destination_dir/

# Rename directory
$ mv old_directory/ new_directory/

# Move contents
$ mv source_dir/* destination_dir/

3. Safe Moving

# Interactive mode
$ mv -i important.txt backup/

# Create backup of destination
$ mv -b file.txt destination/

# Move only newer files
$ mv -u *.txt destination/

Common Use Cases

  1. File Organization

    # Sort files by extension
    mv *.pdf documents/
    mv *.jpg images/
    mv *.mp3 music/
    
  2. Backup Management

    # Create dated backup
    mv file.txt file.txt.$(date +%Y%m%d)
    
    # Archive old files
    mv -t archive/ $(find . -mtime +30)
    
  3. Project Management

    # Reorganize project structure
    mv src/old-components/* src/components/
    mv config.* settings/
    

Tips and Tricks

  1. Pattern Matching

    # Move multiple file types
    mv -t destination/ *.{jpg,png,gif}
    
    # Move files with specific prefix
    mv prefix* destination/
    
  2. Safe Operations

    # Don't overwrite existing files
    mv -n source/* destination/
    
    # Create numbered backups
    mv --backup=numbered file.txt destination/
    
  3. Bulk Operations

    # Move and rename multiple files
    for f in *.txt; do mv "$f" "${f%.txt}.bak"; done
    

Best Practices

  1. Use Interactive Mode for Important Files

    mv -i critical_file.txt new_location/
    
  2. Create Backups

    # Simple backup
    mv -b important.conf /etc/
    
    # Numbered backup
    mv --backup=numbered config.ini /etc/
    
  3. Check Before Moving

    # List files first
    ls -l files_to_move*
    mv -v files_to_move* destination/
    

Common Errors and Solutions

  1. Permission Denied

    # Solution: Use sudo
    sudo mv file.txt /etc/
    
  2. Directory Not Empty

    # Solution: Merge directories
    mv -i source_dir/* dest_dir/
    rmdir source_dir
    
  3. File Exists

    # Solution: Use -i or -n
    mv -i source.txt destination/
    

Advanced Usage

1. Bulk Renaming

# Rename with pattern
for f in *.txt; do
    mv "$f" "${f%.txt}.md"
done

# Using rename command
rename 's/.txt$/.md/' *.txt

2. Moving with Conditions

# Move based on file size
find . -size +100M -exec mv {} large_files/ \;

# Move based on date
find . -mtime +30 -exec mv {} old_files/ \;

3. Safe Moving Script

#!/bin/bash
# Safe moving with verification
safe_move() {
    if [ -e "$2/$1" ]; then
        read -p "File exists. Overwrite? (y/n) " answer
        [ "$answer" = "y" ] || return 1
    fi
    mv -v "$1" "$2"
}

Special Use Cases

1. Moving Across Filesystems

# Check if on same filesystem
df -P source destination

# Use rsync for cross-filesystem moves
rsync -av --remove-source-files source/ destination/

2. Moving Large Directories

# Move with progress indication
rsync -av --progress --remove-source-files source/ destination/

3. Moving with Timestamps

# Move and append timestamp
for f in *; do
    mv "$f" "$f.$(date +%Y%m%d)"
done

Automation Examples

1. Auto-organizing Downloads

#!/bin/bash
cd ~/Downloads
mv *.pdf ~/Documents/PDFs/
mv *.{jpg,png,gif} ~/Pictures/
mv *.{mp3,wav} ~/Music/
mv *.{mp4,mkv} ~/Videos/

2. Project Cleanup

#!/bin/bash
# Move old logs to archive
find logs/ -name "*.log" -mtime +30 \
    -exec mv -t archive/ {} +

# Move backup files
find . -name "*.bak" \
    -exec mv -t backups/ {} +

Remember that mv is a powerful command that can both move and rename files. Always use it carefully, especially with wildcards, and consider using the -i option for important operations to prevent accidental overwrites.


Previous Post
Linux cat Command: Concatenate and Display File Contents
Next Post
Linux cp Command: Complete File and Directory Copy Guide