Linux mren Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the mren command, a powerful tool for renaming multiple files in Linux. The mren command allows you to rename files based on patterns, making it a useful tool for batch file renaming operations. You will explore the basic usage of mren and learn how to leverage regular expressions for more advanced file renaming tasks.

The lab covers the following topics: Introduction to the mren command, Renaming Multiple Files with mren, and Advanced mren Usage with Regular Expressions. By the end of this lab, you will be able to efficiently manage and organize your files using the mren command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/BasicFileOperationsGroup -.-> linux/mv("`File Moving/Renaming`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/TextProcessingGroup -.-> linux/sed("`Stream Editing`") subgraph Lab Skills linux/echo -.-> lab-422827{{"`Linux mren Command with Practical Examples`"}} linux/ls -.-> lab-422827{{"`Linux mren Command with Practical Examples`"}} linux/cp -.-> lab-422827{{"`Linux mren Command with Practical Examples`"}} linux/mv -.-> lab-422827{{"`Linux mren Command with Practical Examples`"}} linux/grep -.-> lab-422827{{"`Linux mren Command with Practical Examples`"}} linux/sed -.-> lab-422827{{"`Linux mren Command with Practical Examples`"}} end

Introduction to the mren Command

In this step, you will learn about the mren command, which is a powerful tool for renaming multiple files in Linux. The mren command allows you to rename files based on patterns, making it a useful tool for batch file renaming operations.

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

cd ~/project
touch file1.txt file2.txt file3.txt file4.txt file5.txt

Example output:

labex@ubuntu:~/project$ ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

Now, let's use the mren command to rename these files. The basic syntax for mren is:

mren 'pattern' 'replacement' files...

Here, pattern is the search pattern to match the files, and replacement is the new name format.

For example, to rename all the files with the prefix "file" to "myfile":

mren 'file(\d+).txt' 'myfile\1.txt' *.txt

Example output:

labex@ubuntu:~/project$ ls
myfile1.txt  myfile2.txt  myfile3.txt  myfile4.txt  myfile5.txt

As you can see, the mren command has renamed all the files with the "file" prefix to "myfile" with the corresponding number.

The mren command supports regular expressions, which allows for more advanced file renaming operations. We will explore this in the next step.

Renaming Multiple Files with mren

In this step, we will explore more advanced use cases of the mren command for renaming multiple files.

Let's start by creating some sample files with different naming conventions:

cd ~/project
touch file001.txt file002.txt file003.txt
touch image01.jpg image02.jpg image03.jpg

Example output:

labex@ubuntu:~/project$ ls
file001.txt  file002.txt  file003.txt  image01.jpg  image02.jpg  image03.jpg

Now, let's say we want to rename all the "file" files to have a consistent format, like "myfile_001.txt", "myfile_002.txt", and so on. We can use the mren command with regular expressions to achieve this:

mren 'file(\d+).txt' 'myfile_\1.txt' *.txt

Example output:

labex@ubuntu:~/project$ ls
myfile_001.txt  myfile_002.txt  myfile_003.txt  image01.jpg  image02.jpg  image03.jpg

Similarly, let's rename all the image files to have a consistent format, like "image_01.jpg", "image_02.jpg", and so on:

mren 'image(\d+).jpg' 'image_\1.jpg' *.jpg

Example output:

labex@ubuntu:~/project$ ls
myfile_001.txt  myfile_002.txt  myfile_003.txt  image_01.jpg  image_02.jpg  image_03.jpg

The mren command allows you to use capture groups in the regular expression pattern to reference parts of the filename in the replacement. This makes it a powerful tool for complex file renaming operations.

Advanced mren Usage with Regular Expressions

In this final step, we will explore some more advanced use cases of the mren command, leveraging the power of regular expressions.

Let's start by creating a set of files with a more complex naming structure:

cd ~/project
touch report_2023-01-01.txt report_2023-01-02.txt report_2023-01-03.txt
touch report_2023-02-01.txt report_2023-02-02.txt report_2023-02-03.txt
touch report_2023-03-01.txt report_2023-03-02.txt report_2023-03-03.txt

Example output:

labex@ubuntu:~/project$ ls
report_2023-01-01.txt  report_2023-02-01.txt  report_2023-03-01.txt
report_2023-01-02.txt  report_2023-02-02.txt  report_2023-03-02.txt
report_2023-01-03.txt  report_2023-02-03.txt  report_2023-03-03.txt

Now, let's say we want to rename these files to have a more consistent format, like "report_2023_01_01.txt", "report_2023_02_01.txt", and so on. We can use the mren command with a more complex regular expression pattern:

mren 'report_(\d{4})-(\d{2})-(\d{2}).txt' 'report_\1_\2_\3.txt' *.txt

Example output:

labex@ubuntu:~/project$ ls
report_2023_01_01.txt  report_2023_02_01.txt  report_2023_03_01.txt
report_2023_01_02.txt  report_2023_02_02.txt  report_2023_03_02.txt
report_2023_01_03.txt  report_2023_02_03.txt  report_2023_03_03.txt

In this example, the regular expression pattern 'report_(\d{4})-(\d{2})-(\d{2}).txt' captures the year, month, and day components of the filename, and the replacement string 'report_\1_\2_\3.txt' uses these captured groups to construct the new filename format.

The mren command is a powerful tool that can handle a wide range of file renaming tasks, from simple batch renames to complex operations using regular expressions. By mastering the mren command, you can streamline your file management workflows and save time on repetitive tasks.

Summary

In this lab, you learned about the mren command, a powerful tool for renaming multiple files in Linux. You started by creating sample files and then used mren to rename them based on patterns, including the use of regular expressions for more advanced file renaming operations. The mren command allows you to efficiently batch rename files, making it a valuable tool for file management tasks.

The lab covered the basic syntax of the mren command, demonstrating how to rename files with a specific prefix or pattern. Additionally, you explored the use of regular expressions with mren, which enables more complex file renaming scenarios, such as adding a consistent format to file names.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like