Bash Move Command

Move and rename files and directories

🚚 What is the MV Command?

The mv command (move) relocates files and directories to different locations or renames them. Unlike copy, it moves the original file without creating a duplicate, making it perfect for organizing files and changing names efficiently.


# Move a file
mv oldname.txt newname.txt
                                    

Common Move Operations

✏️

Rename Files

Change file names

mv old.txt new.txt
📂

Move Files

Relocate to different directory

mv file.txt /folder/
📁

Move Directories

Relocate entire folders

mv folder/ /new/path/
🔄

Multiple Files

Move many files together

mv *.txt /folder/

🔹 Rename a File

The simplest mv usage renames files by specifying current and new names in the same directory. For example, mv oldname.txt newname.txt changes the filename while preserving content and location. This operation is faster and more efficient than copying and deleting files, making it ideal for quick file renames and organizational updates.

# Rename a file in same directory
mv oldname.txt newname.txt

# Rename with different extension
mv document.txt document.md

# Rename multiple files (requires loop)
for file in *.txt; do mv "$file" "${file%.txt}.bak"; done

Result:

oldname.txt → newname.txt
(File renamed, original name no longer exists)

🔹 Move Files to Directory

Files can be moved between directories by specifying destination paths while preserving filenames. Commands like mv file.txt /target/directory/ relocate files, while mv *.jpg /images/ moves multiple files simultaneously. This functionality organizes file systems, relocates project assets, and maintains systematic file storage across different directory structures.

# Move single file to directory
mv file.txt /home/user/documents/

# Move and rename in one command
mv file.txt /home/user/documents/newname.txt

# Move multiple files
mv file1.txt file2.txt file3.txt /destination/

# Move all text files
mv *.txt /backup/

Result:

file.txt moved from current directory
to /home/user/documents/file.txt

🔹 Move Directories

Moving directories works identically to moving files without requiring special flags. The command mv olddir/ newlocation/ relocates entire directory structures with all contents intact. This is essential for project reorganization, storage optimization, and migrating folder hierarchies between different drives or partitions while maintaining complete structure integrity.

# Move directory to new location
mv project_folder/ /home/user/projects/

# Rename a directory
mv old_folder_name/ new_folder_name/

# Move directory and rename
mv myproject/ /backup/myproject_backup/

Important Notes:

  • No -r flag needed (unlike cp)
  • Moves all contents automatically
  • Original directory is removed
  • Fast operation (just updates file system)

🔹 Interactive Move (Safe Mode)

The -i option enables interactive mode, prompting before overwriting existing destination files. This safety feature prevents accidental data loss during file moves, particularly when updating backups or synchronizing directories. It provides confirmation opportunities for potentially destructive operations, enhancing file management safety and user confidence.

# Prompt before overwriting
mv -i file.txt /destination/

# Interactive move of multiple files
mv -i *.txt /backup/

Output:

mv: overwrite '/destination/file.txt'? y
(Type 'y' to confirm, 'n' to cancel)

🔹 Force Move

The -f option forces move operations without prompts, automatically overwriting existing files. Use this carefully as it replaces destination files without warnings. It's valuable in automated scripts requiring unconditional file replacement or when users are certain about overwrite requirements and want to avoid interactive interruptions.

# Force move without prompting
mv -f source.txt destination.txt

# Force move multiple files
mv -f *.log /archive/

⚠️ Warning:

The -f option will overwrite files without asking. Use with caution to avoid accidental data loss! This forceful operation cannot be undone and may permanently erase important files. Always verify destinations and file importance before using force options, particularly in production environments or with critical data.

🔹 Useful MV Options

The mv command provides several options controlling move behavior and safety features. Beyond -i and -f, flags like -v (verbose) provide operation feedback, while -n (no-clobber) prevents overwrites. These options enable predictable file management and prevent accidental data loss across different moving scenarios and user preferences.

Common Options:

  • -i : Interactive mode (prompt before overwrite)
  • -f : Force move (no prompts)
  • -n : No overwrite (don't replace existing files)
  • -v : Verbose (show what's being moved)
  • -u : Update (move only newer files)
  • -b : Backup (create backup of destination file)
# Verbose move
mv -v file.txt /destination/

# No overwrite
mv -n file.txt /destination/

# Create backup before overwriting
mv -b file.txt /destination/

# Combine options
mv -iv *.txt /backup/

🔹 Practical Examples

Everyday uses of head include validating data files, monitoring log heads, and creating data samples. A data scientist might run head -1000 large_dataset.csv > sample.csv to create a manageable sample for testing scripts. A developer might use head -20 application.log to check recent activity after a deployment. These scenarios highlight head's role as a gatekeeper, providing a safe, controlled way to interact with the beginning of files and streams, which is often where the most current or defining information resides.

# Organize downloads by type
mv ~/Downloads/*.pdf ~/Documents/PDFs/
mv ~/Downloads/*.jpg ~/Pictures/

# Rename with timestamp
mv report.txt report_$(date +%Y%m%d).txt

# Move old logs to archive
mv *.log /archive/logs/

# Reorganize project structure
mv src/old_module/ src/legacy/

# Move and rename config file
mv config.txt /etc/myapp/config.conf

# Clean up temporary files
mv /tmp/*.tmp /trash/

🧠 Test Your Knowledge

What happens to the original file after using mv?