Batch Renaming Files in Linux

1. Introduction

Batch renaming files can save a lot of time when you need to rename multiple files at once. In Linux, there are various ways to accomplish this task. In this article, we will discuss different methods to batch rename files in Linux.

2. Using the rename Command

The rename command is a powerful tool that can be used to rename files in Linux. It uses regular expressions to match and replace file names.

2.1 Basic Syntax

The basic syntax of the rename command is as follows:

rename [OPTIONS] 's/old-pattern/new-pattern/' file(s)

The old-pattern is the string that you want to replace, and the new-pattern is the string that you want to replace it with. The s/ at the beginning of the pattern indicates that it is a substitution operation.

2.2 Examples

Let's take some examples to understand the rename command.

Example 1:

To rename all .txt files in the current directory to .md, run the following command:

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

This will replace the .txt extension with .md for all files in the current directory that have .txt extension.

Example 2:

To rename all the files that have lowercase letters in their names to uppercase letters, run the following command:

rename 'y/a-z/A-Z/' *

This will transform all files whose names contain lowercase letters to uppercase letters.

3. Using the mmv Command

The mmv command is another powerful tool to rename files in Linux. It is much more flexible than the rename command as it allows you to use wildcards and regular expressions to match and rename files.

3.1 Basic Syntax

The basic syntax of the mmv command is:

mmv [-v] 'from-pattern' 'to-pattern'

The from-pattern and to-pattern are the patterns that specify the matching and renaming strings, respectively.

3.2 Examples

Let's take some examples to understand the mmv command.

Example 1:

To rename all files in the current directory that have the .txt extension to .md, run the following command:

mmv '*.txt' '#1.md'

This command will replace the .txt extension with .md for all files in the current directory that have .txt extension.

Example 2:

To rename all files in the current directory that have a numeric extension to the corresponding month name, run the following command:

mmv '*.[0-9][0-9]' '#1-$(date +%b).#2'

This command will rename all files in the current directory that have a numeric extension to the corresponding month name in the format name-month.extension.

4. Conclusion

Batch renaming files is an important task when dealing with multiple files. In this article, we discussed two methods to batch rename files in Linux, namely the rename command and the mmv command. Both these methods are powerful tools that can help you save a lot of time and effort.

免责声明:本文来自互联网,本站所有信息(包括但不限于文字、视频、音频、数据及图表),不保证该信息的准确性、真实性、完整性、有效性、及时性、原创性等,版权归属于原作者,如无意侵犯媒体或个人知识产权,请来电或致函告之,本站将在第一时间处理。猿码集站发布此文目的在于促进信息交流,此文观点与本站立场无关,不承担任何责任。

操作系统标签