How to Move or Rename Files and Directories in Linux

Here’s a list of practical examples of the mv command in Linux, which is used to move or rename files/directories.

Alby Andersen

The mv command in Linux stands for “move”. It is primarily used to move files or directories from one location to another within the filesystem. Additionally, it can rename files or directories by moving them to a new name in the same location. The mv command is highly versatile, supporting operations like overwriting files, preserving file attributes, and handling multiple files or directories at once. It’s a fundamental tool for organizing files, renaming them, or relocating them to different directories.

However, caution is advised, as mv can overwrite existing files without warning unless specific options are used. Whether you’re managing personal files or scripting system tasks, mv is an essential command for efficient file manipulation in Linux.


Rename a File

mv oldfile.txt newfile.txt

Renames oldfile.txt to newfile.txt in the same directory.


2. Move a File to Another Directory

mv file.txt /target/directory

Moves file.txt to /target/directory.


Move Multiple Files to a Directory

mv file1.txt file2.txt /target/directory

Moves file1.txt and file2.txt to /target/directory.


Rename a Directory

mv olddir/ newdir/

Renames the directory olddir to newdir.


Move a Directory to Another Location

mv /path/to/source/dir/ /path/to/destination/

Moves the entire dir (and its contents) to the destination.


Force Overwrite Without Prompt

mv -f source.txt destination.txt

Overwrites destination.txt if it exists (no confirmation).


Interactive Mode (Prompt Before Overwriting)

mv -i source.txt destination.txt

Asks for confirmation before overwriting existing files.


Backup Existing Files Before Overwriting

mv --backup=numbered source.txt destination.txt

Creates a numbered backup of destination.txt (e.g., destination.txt.~1~).


Move Files with Wildcards

mv *.txt /target/directory

Moves all .txt files to /target/directory.


Verbose Mode (Show Actions)

mv -v file.txt /target/directory

Prints: 'file.txt' -> '/target/directory/file.txt'.


Move Files and Preserve Timestamps

mv -p file.txt /target/directory

Retains the original file’s timestamps and permissions.


12. Move Hidden Files

mv .hiddenfile /target/directory

Moves hidden files (starting with .).


Move Files with Spaces in Names

mv "My Document.txt" /target/directory

Use quotes to handle filenames with spaces.


Rename Files with Pattern Matching

mv *.jpeg *.jpg

Renames all .jpeg files to .jpg in the current directory.


Move Files via find + xargs

find . -name "*.log" -type f -print0 | xargs -0 mv -t /target/directory

Finds all .log files and moves them to /target/directory (handles spaces in filenames).


Move Files Using Absolute Paths

mv /home/user/docs/file.txt /backup/

Moves a file using absolute paths.


Move and Create Parent Directories

mkdir -p /target/directory && mv file.txt /target/directory

Creates parent directories if they don’t exist before moving.


Move Files to a New Directory

mv file1.txt file2.txt new_directory/

Moves files into new_directory (creates it if missing).


Dry Run (Simulate Moving)

mv -n file.txt /target/directory

Simulates the move without making changes (-n = no overwrite).


Move Files with sudo (System Files)

sudo mv /tmp/config /etc/

Moves files requiring root permissions.


Key Notes:

  • Overwriting: By default, mv overwrites existing files. Use -i or --backup to avoid accidental data loss.
  • Wildcards: Use * (any characters) or ? (single character) for pattern matching.
  • Permissions: Ensure you have write access to both the source and destination directories.

The mv command is a cornerstone of Linux file management. Use it carefully! 🐧

Share This Article