How to Rename Files and Directories in Linux

Updated on 04 July, 2025
Learn how to rename files and directories in Linux using mv, rename, and Bash loops for efficient management.
How to Rename Files and Directories in Linux header image

Renaming files and directories is a routine but essential task when managing Linux systems. Whether you're organizing documents, restructuring a project, or automating workflows, Linux provides powerful command-line tools to simplify the process.

This article explains multiple ways to rename files and directories using the mv and rename commands. You’ll also learn how to perform bulk renaming with shell loops and apply best practices to avoid overwriting files or introducing naming errors.

Prerequisites

Before you begin, you need to:

  • Have access to a Linux instance as a non-root user with sudo privileges.

Commands for Renaming Files and Directories in Linux

Linux offers multiple ways to rename files and directories using command-line tools. The most commonly used methods include:

  • mv: Renames or moves files and directories.
  • rename: Renames multiple files using Perl-compatible expressions.
  • Bash loops: Automates bulk renaming with flexible logic.

Each method serves different use cases, which the following sections cover in detail.

Rename Files in Linux

Renaming files in Linux helps organize content, improve clarity, and ensure consistent naming across your projects.

Using the mv Command

Command Syntax

mv [OPTIONS] old_filename new_filename
  • old_filename: The current name of the file.
  • new_filename: The new name to assign.
  • -i: Prompt before overwrite.
  • -n: Prevent overwriting an existing file.

Command Demonstration

  1. Rename document.txt to report.txt.

    console
    $ mv document.txt report.txt
    
  2. Verify the result.

    console
    $ ls
    

    Output.

    report.txt
  3. Prompt before overwriting.

    console
    $ mv -i existing.txt backup.txt
    
  4. Prevent overwriting with -n.

    console
    $ mv -n existing.txt backup.txt
    

    mv overwrites files silently unless you use -i or -n.

  5. If the filename includes spaces, wrap it in double quotes.

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

Using the rename Command

This article uses the Perl-based rename utility (also called prename), which is default on Debian-based distributions like Ubuntu. On other distributions, such as CentOS or Fedora, rename may refer to a different C-based implementation with different syntax.

console
$ sudo apt install rename

Command Syntax

rename 's/old_pattern/new_pattern/' files_to_rename
  • 's/old/new/': Perl-style substitution pattern.
  • files_to_rename: One or more files that match the pattern.

Command Demonstration

  • Rename .txt files to .md.

    console
    $ rename 's/\.txt$/\.md/' *.txt
    
  • Convert all filenames to lowercase.

    console
    $ rename 'y/A-Z/a-z/' *
    
  • To preview changes before renaming, use the -n flag with rename for a dry run.

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

    Or use a loop with echo to simulate:

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

Using Bash Loops

Command Syntax

for f in *.txt; do mv -- "$f" "${f%.txt}.pdf"; done
  • *.txt: All files ending in .txt.
  • mv -- "$f": Rename each file individually.
  • "${f%.txt}.pdf": Change the extension from .txt to .pdf.

Command Demonstration

  1. Batch rename all .txt files to .pdf.

    console
    $ for f in *.txt; do mv -- "$f" "${f%.txt}.pdf"; done
    
  2. Add the -n flag to prevent overwriting files if they already exist.

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

Rename Directories in Linux

Renaming directories helps improve project organization, enforce naming conventions, and maintain a clean directory structure. This section explores multiple methods to rename directories in Linux.

Using the mv Command

Command Syntax

mv [OPTIONS] old_directory new_directory
  • old_directory: The current name of the directory.
  • new_directory: The new name to assign.
  • -T: Treat the destination as a regular file or directory, not a target location.

Command Demonstration

  1. Rename the projects directory to work.

    console
    $ mv projects work
    
  2. If work already exists and you want to overwrite it directly rather than move projects inside work/, use the -T flag.

    console
    $ mv -T projects work
    

Using the rename Command

Command Syntax

rename 's/old/new/' */
  • 's/old/new/': Perl-style substitution pattern.
  • */: Targets all directories in the current path.

Command Demonstration

  1. Replace old with new in all directory names.

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

Bulk Renaming Directories with Loops

Command Syntax

for dir in */; do mv "$dir" "${dir%/}_backup"; done
  • */: Matches all directories in the current path.
  • ${dir%/}_backup: Appends _backup to the directory name after removing the trailing slash.

Command Demonstration

  1. Append _backup to all directories.

    console
    $ for dir in */; do mv "$dir" "${dir%/}_backup"; done
    
  2. Always preview loop-based renaming with echo before running destructive changes.

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

Conclusion

In this article, you learned how to rename files and directories in Linux using various command-line tools. You used the mv command for simple renames, the rename command for pattern-based bulk operations, and Bash loops for custom renaming logic. These techniques help you maintain a clean file structure, automate repetitive tasks, and manage large directories efficiently. For additional options and examples, explore the official man pages for mv and rename.

Comments

No comments yet.