Linux rename Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux rename command to batch rename files and directories based on a specified pattern. The rename command is a powerful tool that allows you to perform complex renaming operations, such as adding prefixes or suffixes, or changing the file name based on the content of the file. You will start by understanding the basic syntax and usage of the rename command, and then practice renaming individual files and batches of files using various examples.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/mv("`File Moving/Renaming`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/ls -.-> lab-422884{{"`Linux rename Command with Practical Examples`"}} linux/mv -.-> lab-422884{{"`Linux rename Command with Practical Examples`"}} linux/touch -.-> lab-422884{{"`Linux rename Command with Practical Examples`"}} end

Understand the rename Command

In this step, you will learn about the rename command in Linux. The rename command is a powerful tool that allows you to batch rename files and directories based on a specified pattern.

The basic syntax of the rename command is:

rename 'expression' files

Here, expression is a Perl-compatible regular expression that defines the pattern to match and replace. The files argument specifies the files to be renamed.

For example, to rename all files with the extension .txt to .doc, you can use the following command:

rename 's/.txt$/.doc/' *.txt

This command will replace the .txt extension with .doc for all files in the current directory.

Example output:

file1.txt -> file1.doc
file2.txt -> file2.doc
file3.txt -> file3.doc

The rename command can also be used to perform more complex renaming operations, such as adding prefixes or suffixes, or even changing the file name based on the content of the file.

Rename Files Using the rename Command

In this step, you will learn how to use the rename command to rename individual files.

First, let's create some sample files to work with:

touch file1.txt file2.txt file3.txt

To rename a single file, you can use the rename command with a simple expression:

rename 's/file1/new_file1/' file1.txt

This will rename file1.txt to new_file1.txt.

Example output:

file1.txt -> new_file1.txt

You can also use the rename command to add a prefix or suffix to a file:

rename 's/(.*)\.txt$/prefix_\1.txt/' *.txt

This will add the prefix prefix_ to all .txt files in the current directory.

Example output:

file1.txt -> prefix_file1.txt
file2.txt -> prefix_file2.txt
file3.txt -> prefix_file3.txt

The rename command supports Perl-compatible regular expressions, which allows you to perform more complex renaming operations. Experiment with different expressions to see how you can customize the file renaming process.

Batch Rename Files with the rename Command

In this step, you will learn how to use the rename command to batch rename multiple files.

First, let's create some sample files with different extensions:

touch file1.txt file2.jpg file3.pdf file4.doc

To batch rename all files with a specific extension, you can use the rename command with a regular expression:

rename 's/\.txt$/.doc/' *.txt

This will rename all .txt files to .doc extension.

Example output:

file1.txt -> file1.doc

You can also use the rename command to add a prefix or suffix to multiple files:

rename 's/(.*)\.jpg$/image_\1.jpg/' *.jpg

This will add the prefix image_ to all .jpg files.

Example output:

file2.jpg -> image_file2.jpg

The rename command is very flexible and allows you to perform complex batch renaming operations. You can use regular expressions to match and replace patterns in file names, as well as incorporate information from the file name into the new name.

Summary

In this lab, you first learned about the rename command in Linux, which is a powerful tool for batch renaming files and directories based on a specified pattern. The basic syntax of the rename command is rename 'expression' files, where expression is a Perl-compatible regular expression that defines the pattern to match and replace. You also saw an example of how to rename all files with the .txt extension to .doc.

Next, you learned how to use the rename command to rename individual files, including adding prefixes or suffixes to the file names. The rename command supports Perl-compatible regular expressions, which allows you to perform more complex renaming operations.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like