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
sudoprivileges.
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
mv [OPTIONS] old_filename new_filenameold_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:
$ mv document.txt report.txtCheck the result:
$ lsPrompt before replacing a file:
$ mv -i existing.txt backup.txtBlock overwriting with -n:
$ mv -n existing.txt backup.txtIf the filename has spaces, use quotes:
$ 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.
$ sudo apt install renameSyntax
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:
$ rename 's/\.txt$/\.md/' *.txtConvert all filenames to lowercase:
$ rename 'y/A-Z/a-z/' *Preview changes before applying:
$ rename -n 's/\.txt$/.md/' *.txtOr simulate with a loop:
$ for f in *.txt; do echo mv "$f" "${f%.txt}.md"; doneUsing Bash Loops
Syntax
for f in *.txt; do mv -- "$f" "${f%.txt}.pdf"; doneExample
Batch rename .txt files to .pdf:
$ for f in *.txt; do mv -- "$f" "${f%.txt}.pdf"; donePrevent overwriting:
$ for f in *.txt; do mv -n -- "$f" "${f%.txt}.pdf"; doneRenaming Directories in Linux
Using the mv Command
Syntax
mv [OPTIONS] old_directory new_directoryExample
Rename projects to work:
$ mv projects workOverwrite with -T:
$ mv -T projects workUsing the rename Command
Syntax
rename 's/old/new/' */Example
Replace “old” with “new” in directory names:
$ rename 's/old/new/' */Bulk Renaming Directories with Loops
Syntax
for dir in */; do mv "$dir" "${dir%/}_backup"; doneExample
Add _backup to all directory names:
$ for dir in */; do mv "$dir" "${dir%/}_backup"; donePreview changes with echo:
$ for dir in */; do echo mv "$dir" "${dir%/}_backup"; doneConclusion
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.
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.