Tutorials  /  Linux Basics

Rename Files and Directories in Linux: mv, rename, loops

Ccentron Redaktion · August 2025 ·5 min read ·Linux Basics, Tutorial

Renaming files and directories is a common but vital task in Linux system management. Whether it’s for organizing content, restructuring a project, or streamlining automated workflows, Linux offers robust command-line utilities to make the process straightforward.

This guide covers different methods for renaming files and directories using the mv and rename commands. It also demonstrates how to handle bulk renaming with shell loops and shares tips to prevent overwriting files or creating naming conflicts.

Prerequisites

  • Access to a Linux environment as a non-root user with sudo privileges.
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Available Commands for Renaming

Linux provides several command-line tools to rename files and directories. The most popular include:

  • mv: Renames or moves files and directories.
  • rename: Changes multiple filenames using Perl-compatible patterns.
  • Bash loops: Enables automated bulk renaming with custom logic.

Renaming Files in Linux

Changing filenames can help maintain order, improve clarity, and keep naming consistent across a project.

Using the mv Command

Syntax

Code
mv [OPTIONS] old_filename new_filename
  • old_filename: Current file name.
  • new_filename: Name you want to assign.
  • -i: Prompt before overwriting.
  • -n: Prevent overwriting.

Examples

Rename document.txt to report.txt:

Console
$ mv document.txt report.txt

Check the result:

Console
$ ls

Prompt before replacing a file:

Console
$ mv -i existing.txt backup.txt

Block overwriting with -n:

Console
$ mv -n existing.txt backup.txt

If the filename has spaces, use quotes:

Console
$ mv "old file.txt" "new file.txt"

Using the rename Command

This example uses the Perl-based rename utility, common in Debian-based systems like Ubuntu. On other distributions (e.g., CentOS, Fedora), rename may refer to a different version with different syntax.

Console
$ sudo apt install rename

Syntax

Code
rename 's/old_pattern/new_pattern/' files_to_rename
  • 's/old/new/': Perl-style substitution.
  • files_to_rename: Files to rename.

Examples

Change all .txt files to .md:

Console
$ rename 's/\.txt$/\.md/' *.txt

Convert all filenames to lowercase:

Console
$ rename 'y/A-Z/a-z/' *

Preview changes before applying:

Console
$ rename -n 's/\.txt$/.md/' *.txt

Or simulate with a loop:

Console
$ for f in *.txt; do echo mv "$f" "${f%.txt}.md"; done

Using Bash Loops

Syntax

Code
for f in *.txt; do mv -- "$f" "${f%.txt}.pdf"; done

Example

Batch rename .txt files to .pdf:

Console
$ for f in *.txt; do mv -- "$f" "${f%.txt}.pdf"; done

Prevent overwriting:

Console
$ for f in *.txt; do mv -n -- "$f" "${f%.txt}.pdf"; done

Renaming Directories in Linux

Using the mv Command

Syntax

Code
mv [OPTIONS] old_directory new_directory

Example

Rename projects to work:

Console
$ mv projects work

Overwrite with -T:

Console
$ mv -T projects work

Using the rename Command

Syntax

Code
rename 's/old/new/' */

Example

Replace “old” with “new” in directory names:

Console
$ rename 's/old/new/' */

Bulk Renaming Directories with Loops

Syntax

Code
for dir in */; do mv "$dir" "${dir%/}_backup"; done

Example

Add _backup to all directory names:

Console
$ for dir in */; do mv "$dir" "${dir%/}_backup"; done

Preview changes with echo:

Console
$ for dir in */; do echo mv "$dir" "${dir%/}_backup"; done

Conclusion

This guide demonstrated renaming files and directories in Linux using mv, rename, and Bash loops. By using these tools, you can keep file systems organized, avoid naming conflicts, and automate repetitive tasks. For further details, refer to the official mv and rename manual pages.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren