rename

file managementLinux/Unix
The rename command is one of the most frequently used commands in Linux/Unix-like operating systems. rename Rename multiple files using pattern substitution

Quick Reference

Command Name:

rename

Category:

file management

Platform:

Linux/Unix

Basic Usage:

rename [options] [arguments]

Common Use Cases

    Syntax

    rename [options] expression replacement file...

    Options

    Option Description
    -v, --verbose Show which files were renamed
    -n, --no-act Do not make any changes (dry run)
    -f, --force Overwrite existing files
    -h, --help Display help message and exit
    -V, --version Output version information and exit
    -o, --no-overwrite Don't overwrite existing files (some versions)
    -i, --interactive Prompt before overwriting (some versions)
    -s, --symlink Act on symlinks instead of their targets (some versions)

    Examples

    How to Use These Examples

    The examples below show common ways to use the rename command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    Basic Examples:

    rename 's/.txt/.md/' *.txt
    Rename all .txt files to .md files.
    rename 's/IMG_/photo_/' IMG_*.jpg
    Rename all IMG_xxxx.jpg files to photo_xxxx.jpg.

    Advanced Examples:

    rename -v 's/\s+/_/g' *
    Rename all files replacing spaces with underscores, with verbose output.
    rename -n 's/^test/demo/' *.html Simulate renaming files with prefix 'test' to 'demo' (dry run). rename 'y/A-Z/a-z/' * Rename all files to lowercase (on some systems). rename 's/^(.*)$/prefix_$1/' *.txt Add 'prefix_' to all .txt files. rename 's/(\d{4})-(\d{2})-(\d{2})/$3-$2-$1/' *.csv Change date format from YYYY-MM-DD to DD-MM-YYYY in filenames. rename -v 's/\d+/sprintf("%04d", $&)/e' *.jpg Pad all numbers in filenames with leading zeros (Perl version). find . -name "*.html" -exec rename 's/.html/.htm/' {} \; Recursively rename all .html files to .htm in current directory and subdirectories. find . -type f -name "*.jpeg" | rename 's/.jpeg/.jpg/' Pipe find results to rename to convert .jpeg files to .jpg.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `rename` command is a powerful utility for batch renaming multiple files using pattern substitution, typically with Perl regular expressions. It allows system administrators and users to perform complex renaming operations efficiently without writing custom scripts. There are several versions of the `rename` command available in different Linux distributions, with the two most common being: 1. Perl version (most powerful): Found in Debian, Ubuntu, and derivatives, it uses Perl regular expressions for pattern matching and substitution. 2. Util-linux version: A simpler version found in Red Hat, Fedora, CentOS, and related distributions, with more limited pattern matching capabilities. The Perl version is significantly more powerful as it allows full use of Perl's regular expression engine, including capture groups, back-references, and even embedded Perl code for complex transformations. Key features of the `rename` command include: 1. Pattern-based substitution: Replace parts of filenames using regular expression patterns. 2. Batch processing: Apply the same renaming pattern to multiple files in a single command. 3. Dry run mode: Preview changes with the `-n` option before actually renaming files. 4. Verbose output: See which files are being renamed with the `-v` option. 5. Safety controls: Options to prevent or control overwriting existing files. Common use cases for the `rename` command include: - Changing file extensions (e.g., .txt to .md) - Standardizing filenames (removing spaces, special characters) - Adding or removing prefixes/suffixes - Reformatting dates or sequence numbers in filenames - Converting between uppercase and lowercase - Fixing naming conventions in large collections of files When using `rename`, it's recommended to first test commands with the `-n` (dry run) option to verify that the pattern produces the expected results before actually renaming files. This is particularly important when working with complex regular expressions or large sets of files. The syntax for regular expressions in `rename` follows standard Perl regular expression rules, which can be powerful but also complex for beginners. Common patterns include: - `s/pattern/replacement/` - substitute pattern with replacement - `s/pattern/replacement/g` - global substitution (all occurrences) - `s/pattern/replacement/i` - case-insensitive matching For users without extensive regular expression knowledge, starting with simple substitutions and gradually adding complexity is the recommended approach to mastering this versatile command.

    Related Commands

    These commands are frequently used alongside rename or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the rename command works in different scenarios.

    $ rename
    View All Commands