How to Rename Files and Directories in Linux
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.
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_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
:
$ mv document.txt report.txt
Check the result:
$ ls
Prompt before replacing a file:
$ mv -i existing.txt backup.txt
Block overwriting with -n
:
$ mv -n existing.txt backup.txt
If 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 rename
Syntax
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/' *.txt
Convert all filenames to lowercase:
$ rename 'y/A-Z/a-z/' *
Preview changes before applying:
$ rename -n 's/\.txt$/.md/' *.txt
Or simulate with a loop:
$ for f in *.txt; do echo mv "$f" "${f%.txt}.md"; done
Using Bash Loops
Syntax
for f in *.txt; do mv -- "$f" "${f%.txt}.pdf"; done
Example
Batch rename .txt
files to .pdf
:
$ for f in *.txt; do mv -- "$f" "${f%.txt}.pdf"; done
Prevent overwriting:
$ for f in *.txt; do mv -n -- "$f" "${f%.txt}.pdf"; done
Renaming Directories in Linux
Using the mv Command
Syntax
mv [OPTIONS] old_directory new_directory
Example
Rename projects
to work
:
$ mv projects work
Overwrite with -T
:
$ mv -T projects work
Using 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"; done
Example
Add _backup
to all directory names:
$ for dir in */; do mv "$dir" "${dir%/}_backup"; done
Preview changes with echo
:
$ 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.